Results 1 to 8 of 8

Thread: How to link .c or .cpp files?

  1. #1
    Join Date
    Feb 2007
    Posts
    143
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default How to link .c or .cpp files?

    i saw in the scar plugins folder a cpp test file, im sure this probably isnt a frequently asked question, but i couldnt really find another place for it

    how do you go about getting scar to recognize and interact with these files? i read the readme for developers and tried to just include the .obj file and reference the function, can someone who knows how they interact pm me?

    im guessing a devc++ .o file wouldnt work?

    thanks in advance

  2. #2
    Join Date
    Sep 2006
    Location
    New Jersey, USA
    Posts
    5,347
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    In my opinion, it's easier to compile the c/cpp right into a dll to be used by SCAR (rather than the obj/cross linking thing).

    I think you might be interested in this thread, by yakman.
    Interested in C# and Electrical Engineering? This might interest you.

  3. #3
    Join Date
    Feb 2007
    Posts
    143
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Smartzkid View Post
    In my opinion, it's easier to compile the c/cpp right into a dll to be used by SCAR (rather than the obj/cross linking thing).

    I think you might be interested in this thread, by yakman.
    alright well how do i call the dll? ill continue looking throught the scar includes but this is what i have atm

    dllmain.cpp
    Code:
    #include "my_dll.h"
    
    #define SCAR_EXPORT
    
    extern "C"
    {
       SCAR_EXPORT int Add( int a, int b )
       {
          return( a + b );
       }
    
    }
    my_dll.h
    Code:
    #ifndef _MY_DLL_H_
    #define _MY_DLL_H_
    
    #if defined SCAR_EXPORT
    #define SCAR_EXPORT _declspec(dllexport) __stdcall
    #else
    #define SCAR_EXPORT _declspec(dllexport) __stdcall
    #endif
    
    extern "C"
    {
       SCAR_EXPORT int Add( int a, int b );
    }
    
    #endif
    libtest.def
    Code:
    LIBRARY my_test_dll
    DESCRIPTION "a dll to test this crap"
    EXPORTS
       Add @1
       Function @2
    now i just need to figure out how to call it...

    this is the first time ive ever even attempted a dll, so be gentle lol

    edit: i think im defining SCAR_EXPORT incorrectly

    edit again:
    since scar is like delphi, i tried using delphi commands from scar (yea i know a shot in the dark)
    needless to say this didnt work
    Code:
    program New;
    var
    DLLHandle: THandle;
    begin
    DLLHandle := LoadLibrary('C:\Dev-Cpp\DLL\test.dll');
    writeln(add(5,5));
    end.
    like i said previously ive never made a dll and dont have the first clue about how scar interacts with them

    i also tried this.... no dice
    Code:
    program New;
    
    function add(x,y:integer):integer; external 'Add@test.dll SCAR_EXPORT';
    
    begin
    writeln(add(5,5));
    end.
    i get THESE erros ^^^ tried stdcall instead od scar_export still nothing

    Code:
    Line 5: [Error] (5:1): Invalid Calling Convention in script C:\Program Files\SCAR 3.20\Scripts\testDLL.scar
    Line 5: [Error] (5:1):  in script C:\Program Files\SCAR 3.20\Scripts\testDLL.scar
    Last edited by rogue poser; 06-11-2009 at 03:25 PM.

  4. #4
    Join Date
    Sep 2006
    Location
    New Jersey, USA
    Posts
    5,347
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    You need GetFunctionInfo/GetFunctionCount.

    Here's their delphi implementations from the SCAR sample plugin

    SCAR Code:
    function GetFunctionCount: Integer; stdcall;
    begin
      Result := High(ExportFunctions) + 1;
    end;

    function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
    begin
      Result := -1;
      if (x >= Low(ExportFunctions)) and (x <= High(ExportFunctions)) then
      begin
        ProcAddr := ExportFunctions[x].Ptr;
        StrPCopy(ProcDef, ExportFunctions[x].Name);
        Result := x;
      end;
    end;
    Interested in C# and Electrical Engineering? This might interest you.

  5. #5
    Join Date
    Feb 2007
    Posts
    143
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    i dont know if this is a stupid question or what.... how do i go about using those? i move the DLL and the .def file to the plugins folder. for the procaddress, do i need the sector that the file begins at? can i use a relative path?

    can you give an example of it being used, and maybe the folder where your dLL and .def reside?

    it would be much appreciated

  6. #6
    Join Date
    Sep 2006
    Location
    New Jersey, USA
    Posts
    5,347
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Thumbs up

    Quote Originally Posted by ScarMedia.cpp
    [includes]

    #define Export _declspec(dllexport)
    #define DelphiExport extern "C" Export

    HINSTANCE inst;

    BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID checks)
    {
    switch(reason) {
    case DLL_PROCESS_ATTACH:
    //Init
    inst = instance;
    MessageBox(NULL, "TestDll Initialized", "Advanced SCAR Plugin System", MB_OK);
    case DLL_THREAD_ATTACH:
    return true;

    case DLL_PROCESS_DETACH:
    //Cleanup
    FreeLibrary(inst);
    case DLL_THREAD_DETACH:
    return true;
    }
    return false;
    }

    DelphiExport int GetFunctionInfo(unsigned int x,void** ppfunc,char** szlppString)
    {
    switch(x)
    {
    case 0:
    strcpy(*szlppString,"procedure PlayMedia(file: PChar);");
    *ppfunc = GetProcAddress(inst, "_PlayMedia@4");
    break;
    case 1:
    strcpy(*szlppString,"function GetMediaDuration(file: PChar): integer;");
    *ppfunc = GetProcAddress(inst, "_GetMediaDuration@4");
    break;
    default:
    return -1;
    }
    return x;
    }

    DelphiExport int GetFunctionCount(void)
    {
    return 2;
    }


    DelphiExport int GetMediaDuration(char* file)
    {
    [code]
    }

    DelphiExport int PlayMedia(char* file)
    {
    [code]
    }
    Quote Originally Posted by ScarMedia.def
    EXPORTS
    GetFunctionInfo = _GetFunctionInfo@12
    GetFunctionCount = _GetFunctionCount@0
    To call the procedure in SCAR, use the name you gave it in GetFunctionInfo. All you need in the plugins folder is the DLL; SCAR calls GetFunctionInfo of all DLL's in said folder, so you don't have to do configuring, etc, to make it SCAR recognize it. Note that you must restart SCAR for it to recognize new plugins.

    It's hard to get all the settings right such that SCAR will recognize the plugin. I can send you my entire project (VS2008) for this plugin if you need it.
    Interested in C# and Electrical Engineering? This might interest you.

  7. #7
    Join Date
    Feb 2007
    Posts
    143
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    dangit im so fricking stumped...

    i tried what your doing with void** as the second param of getfunction but i cant get anything, when i define it as a farproc* i also get didley, so just skrewing around i defined it as farproc, and in scar i can access the function, but when i use it i get a violation


    Code:
    #include <windows.h>
    #define Export _declspec(dllexport)
    #define DelphiExport extern "C" Export
    
    HINSTANCE inst;
    
    DelphiExport int GetFunctionInfo(unsigned int x,FARPROC ppfunc,char** szlppString)
       {
          switch(x)
             {
    	  case 0:strcpy(*szlppString,"function aTest(x:integer): integer;");
                 ppfunc = GetProcAddress(inst, "_test@3");
                 break;
               default:return -1;
       }
       return x;
    }
    
    DelphiExport int GetFunctionCount(void)
    {
       return 1;
    }
    
    DelphiExport int test(int x)
    {
       return x;
    }

  8. #8
    Join Date
    Sep 2006
    Location
    New Jersey, USA
    Posts
    5,347
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    In that case, GetFunctionInfo & Count should be working fine. To verify this, just put a messagebox popup inside each of them. You should get two messageboxes on SCAR startup. If this works, I'd say its something in your compiler. Make sure all optimizations are disabled. I think Wizzup? has a thread about g++ SCAR plugins. Its very possible that there is some other option throwing everything off. Find that thread (C++ forum, I assume) and see if it helps, if not, PM him, hopefully he can give you some advice.
    Interested in C# and Electrical Engineering? This might interest you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •