Use DLLs without to recompile the engine
by Thomas Bang · 07/09/2008 (1:38 pm) · 15 comments
Download Code File
I thought it's a good idea to write some code so users of TGB (not the source version) can also
execute functions from a DLL. Without to recompile the engine.
This makes sense because users can not enhance the functionality of the normal version of TGB.
Also users of TGB Pro, TGE and TGEA can use it. No problem.
The usage is very simple. There are only three functions.
Example:
You have two different DLLs. The first DLL is called "FirstDLL.dll" and exports a function called
"Addition". This function expects 3 parameters.
The second DLL is called "secondDLL.dll" and exports a function called "Subtraction". This function
expects 2 parameters.
Place these two DLLs in the same folder where your game executable is.
Now we want to use these two functions in TorqueScript.
// load the DLLs you want to use
loadDLL("FirstDLL.dll", 0); // the second parameter is a handle
loadDLL("SecondDLL.dll", 1);
// now we can execute the functions
echo(executeFunc("Addition",5,7,4)); // now the console shows 16
echo(executeFunc("Subtraction",4,3)); // the console shows 1
// if you dont need the DLLs you write...
unloadDLL( 0 ); // it's the same handle number you wrote with loadDLL
unloadDLL( 1 );
"executeFunc" is a Console Function which executes the function in a DLL.
You see the handling is very easy. The source code i wrote can handle up to 10 parameters/function.
I thought it's a good idea to write some code so users of TGB (not the source version) can also
execute functions from a DLL. Without to recompile the engine.
This makes sense because users can not enhance the functionality of the normal version of TGB.
Also users of TGB Pro, TGE and TGEA can use it. No problem.
The usage is very simple. There are only three functions.
Example:
You have two different DLLs. The first DLL is called "FirstDLL.dll" and exports a function called
"Addition". This function expects 3 parameters.
The second DLL is called "secondDLL.dll" and exports a function called "Subtraction". This function
expects 2 parameters.
Place these two DLLs in the same folder where your game executable is.
Now we want to use these two functions in TorqueScript.
// load the DLLs you want to use
loadDLL("FirstDLL.dll", 0); // the second parameter is a handle
loadDLL("SecondDLL.dll", 1);
// now we can execute the functions
echo(executeFunc("Addition",5,7,4)); // now the console shows 16
echo(executeFunc("Subtraction",4,3)); // the console shows 1
// if you dont need the DLLs you write...
unloadDLL( 0 ); // it's the same handle number you wrote with loadDLL
unloadDLL( 1 );
"executeFunc" is a Console Function which executes the function in a DLL.
You see the handling is very easy. The source code i wrote can handle up to 10 parameters/function.
About the author
#2
07/09/2008 (9:29 pm)
very nice.
#3
07/10/2008 (10:26 am)
nice, and can call functions in .net, vb, delphy?
#4
But there is one thing i forgot to say. If you write your DLLs... all parameters must be const char* (C/C++) or PChar (Delphi). I did not found a way for automatic type-casting.
So if you want to pass a int or float... pass it as const char*(C/C++) or PChar(Delphi) and convert this variable to int or float or or or...
Here is a simple example (Delphi).
07/10/2008 (10:38 am)
Yes, you can. All you need is a IDE/Programming language where you can create DLLs. But there is one thing i forgot to say. If you write your DLLs... all parameters must be const char* (C/C++) or PChar (Delphi). I did not found a way for automatic type-casting.
So if you want to pass a int or float... pass it as const char*(C/C++) or PChar(Delphi) and convert this variable to int or float or or or...
Here is a simple example (Delphi).
unit Unit_Main;
interface
uses SysUtils,Unit_Helper;
function Addition(val1:PChar; val2:PChar; val3:PChar):PChar; cdecl; exports Addition;
function Multiply(val1:PChar; val2:PChar):PChar; cdecl; exports Multiply;
implementation
function Addition(val1:PChar; val2:PChar; val3:PChar):PChar;
var value1,value2,value3:real;
fs:TFormatSettings;
Begin
fs.DecimalSeparator:='.';
value1:=StrToFloat(StrPas(val1),fs); // convert from PChar to String to real/float
value2:=StrToFloat(StrPas(val2),fs);
value3:=StrToFloat(StrPas(val3),fs);
result:=PChar(FloatToStr(value1+value2+value3,fs)); // convert float/real to String to PChar
End;
function Multiply(val1:PChar; val2:PChar):PChar;
var value1,value2:single;
fs:TFormatSettings;
Begin
fs.DecimalSeparator:='.';
value1:=StrToFloat(StrPas(val1),fs); // convert from PChar to String to real/float
value2:=StrToFloat(StrPas(val2),fs);
result:=PChar(FloatToStr(value1*value2,fs)); // convert float/real to String to PChar
end;
end.
#5
07/10/2008 (4:36 pm)
good basis for a addon system for sure. bookmarked this for later reference
#6
07/11/2008 (7:17 am)
mmm, yummy geeky goodness! I will be putting this to work for sure! Awesome job Thomas, thanks a ton! :)
#7
Function testFunction()
My.Computer.FileSystem.WriteAllText("C:\Test.txt", String.Empty, False)
Return "OK"
End Function
I can't seem to run it in game.
07/11/2008 (5:19 pm)
Am I doing something wrong here?Function testFunction()
My.Computer.FileSystem.WriteAllText("C:\Test.txt", String.Empty, False)
Return "OK"
End Function
I can't seem to run it in game.
#8
- Did you wrote a DLL?
- Is the DLL in the same folder where your game executable is?
- Is "testFunction" a exported function? With cdecl?
Here is a good example how to write a DLL with Visual Basic.
www.windowsdevcenter.com/pub/a/windows/2005/04/26/create_dll.html
07/12/2008 (1:23 am)
Visual Basic? - Did you wrote a DLL?
- Is the DLL in the same folder where your game executable is?
- Is "testFunction" a exported function? With cdecl?
Here is a good example how to write a DLL with Visual Basic.
www.windowsdevcenter.com/pub/a/windows/2005/04/26/create_dll.html
#9
In this case you have to modify "loaddll.h" and replace __cdecl with __stdcall
Here is also a table where you can see the differences between C/C++-Datatypes and VB-Datatypes.
www2.f1.fhtw-berlin.de/scheibl/msvc/index.htm?./kapit21.htm
07/12/2008 (1:45 am)
OK... i found out that VB only uses stdcall and not cdecl. In this case you have to modify "loaddll.h" and replace __cdecl with __stdcall
Here is also a table where you can see the differences between C/C++-Datatypes and VB-Datatypes.
www2.f1.fhtw-berlin.de/scheibl/msvc/index.htm?./kapit21.htm
#10
07/12/2008 (9:21 am)
Hmm...still don't seem to work.
#11
07/12/2008 (9:22 am)
FYI, I'm using VB .net 2005
#12
07/12/2008 (10:29 am)
Can you send me your DLL?
#13
VB.net can not build normal Win32-DLLs (with a EntryPoint). Very bad.
07/12/2008 (10:58 am)
OK... i talked with some people in a VB-Forum. VB.net can not build normal Win32-DLLs (with a EntryPoint). Very bad.
#14
07/12/2008 (1:33 pm)
I guess C#.net might not work then either.
#15
07/12/2008 (10:52 pm)
Try it out. If you can create a Win32-ConsoleApplication (without .net) then you have a good chance.
Torque Owner XD
Default Studio Name