Log in

View Full Version : How to link .c or .cpp files?



rogue poser
06-11-2009, 10:56 AM
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

Smartzkid
06-11-2009, 11:19 AM
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 (http://www.villavu.com/forum/showthread.php?t=39677) thread, by yakman.

rogue poser
06-11-2009, 12:48 PM
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 (http://www.villavu.com/forum/showthread.php?t=39677) 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

#include "my_dll.h"

#define SCAR_EXPORT

extern "C"
{
SCAR_EXPORT int Add( int a, int b )
{
return( a + b );
}

}

my_dll.h

#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

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

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


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


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

Smartzkid
06-11-2009, 10:07 PM
You need GetFunctionInfo/GetFunctionCount.

Here's their delphi implementations from the SCAR sample plugin

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;

rogue poser
06-12-2009, 05:04 AM
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

Smartzkid
06-13-2009, 01:38 AM
#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;
}

[I]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]
}


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.

rogue poser
06-14-2009, 04:43 PM
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



#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;
}

Smartzkid
06-16-2009, 03:10 AM
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.