- This wiki is out of date, use the continuation of this wiki instead
Making DLLs
From FenixWiki
Revision as of 23:37, 1 October 2007 (edit) Sandman (Talk | contribs) m (→Tips & Tricks) ← Previous diff |
Revision as of 23:39, 1 October 2007 (edit) (undo) Sandman (Talk | contribs) m (→Tips & Tricks) Next diff → |
||
Line 115: | Line 115: | ||
FENIX_export ( "DLL_MYFUNC" , "II" , TYPE_DWORD , DLL_MYFUNC2 ) ; | FENIX_export ( "DLL_MYFUNC" , "II" , TYPE_DWORD , DLL_MYFUNC2 ) ; | ||
</pre> | </pre> | ||
+ | The only condition is that the number if parameters needs to be different. | ||
* Some functions are handy to use, for example gr_con_printf(). Look in the sourcecode of for example [[Network.dll]] to see how it works. Keep in mind that flooding the console isn't very nice, so leave the option open for the user of the DLL to turn it off. | * Some functions are handy to use, for example gr_con_printf(). Look in the sourcecode of for example [[Network.dll]] to see how it works. Keep in mind that flooding the console isn't very nice, so leave the option open for the user of the DLL to turn it off. |
Revision as of 23:39, 1 October 2007
Contents |
Setting up
Source
Before we start coding, download the latest Fenix Source here and the file fenixdll.def. Put the source somewhere appropriate, like Fenix\Fenix 0.92a Source\. Search for all *.c and *.h in that folder and put it into a new folder like Fenix\Fenix 0.92a Includes\, together with fenixdll.def.
Compiler
First step to coding a DLL is getting a C compiler. As I use MVC++, I'll tell you how to set it up to code a DLL for Fenix/Bennu.
MVC++
So we got MVC++ eh, let's do this then.
- Tools -> Options -> TAB: Directories. Show directories for: Include files. Add the directory you created earlier, Fenix\Fenix 0.92a Includes, to the list. Yes the includes folder.
- File -> New will bring up the New dialog box. Select Win32 Dynamic-Link Library and name your project. Click OK. A new dialog box pops up! Select An empty DLL project. and click Finish. Click OK. I myself like to have the list on the left to display the files; to do this select FileView under it.
- File -> New again, select C++ Source File and name your file. Click OK.
- Project -> Add To Project -> Files... and add fenixdll.def to your project.
Coding a DLL
The most basic code will be something like this:
#include <fxdll.h> static int DLL_MYFUNC(INSTANCE * my, int * params) { return 0; } FENIX_MainDLL RegisterFunctions (COMMON_PARAMS) { FENIX_DLLImport FENIX_export ( "DLL_MYFUNC" , "" , TYPE_DWORD , DLL_MYFUNC ) ; }
If this doesn't compile then something went wrong during setup. Let's assume it compiles, so I can explain the code.
RegisterFunctions
This is where you tell Fenix/Bennu which functions to make available for use in Fenix/Bennu. To do this, add a line like this:
FENIX_export ( <CHAR * functionname> , <CHAR * parameters> , <BASETYPE returntype> , <VOID * function> ) ;
- CHAR * functionname: This is how the function will be called in Fenix/Bennu.
- CHAR * parameters: The list of parameters, possible characters are:
I | - TYPE_DWORD |
B | - TYPE_BYTE |
W | - TYPE_WORD |
S | - TYPE_STRING |
P | - TYPE_POINTER |
F | - TYPE_FLOAT |
- BASETYPE returntype: The datatype of the returnvalue. Possible are:
TYPE_UNDEFINED | - 0 |
TYPE_INT | - 1 |
TYPE_DWORD | - 2 |
TYPE_SHORT | - 3 |
TYPE_WORD | - 4 |
TYPE_SBYTE | - 5 |
TYPE_BYTE | - 6 |
TYPE_CHAR | - 8 |
TYPE_FLOAT | - 9 |
TYPE_STRING | - 16 |
TYPE_ARRAY | - 17 |
TYPE_STRUCT | - 18 |
TYPE_POINTER | - 19 |
- VOID * function: This is the function you wish Fenix/bennu to call when the function is called. This is the name of the function, without "" and can differ from the functionname used in Fenix/Bennu.
Functions
Like stated, the most basic function is of the form:
static int DLL_MYFUNC(INSTANCE * my, int * params) { return 0; }
The first parameter is like a processID: it is the process calling this function. The second parameter is a list of parameters. For some parameters you may wish to typecast them and put them into a proper variable before using them, for example:
float my_float = (float)params[0];
The return type of this function is always static int and you should always return an int. Of course you can return other datatypes, but it needs to be typecasted to an int, like:
return (int)my_float;
Tips & Tricks
There are a few tips and tricks, but most of them come from experience. Some from a lot of experience.
- It is possible to use the same functionname multiple times, even with different properties:
FENIX_export ( "DLL_MYFUNC" , "I" , TYPE_DWORD , DLL_MYFUNC ) ; FENIX_export ( "DLL_MYFUNC" , "II" , TYPE_DWORD , DLL_MYFUNC2 ) ;
The only condition is that the number if parameters needs to be different.
- Some functions are handy to use, for example gr_con_printf(). Look in the sourcecode of for example Network.dll to see how it works. Keep in mind that flooding the console isn't very nice, so leave the option open for the user of the DLL to turn it off.
- Calling a Fenix function/process from a DLL can be handy sometimes, especially when used as a callback function. Initiating a process of a variable processtype in Fenix is also possible:
static int CALL(INSTANCE * my, int * params) { INSTANCE * proc = instance_new (procdef_get(params[0]), first_instance); return instance_go (proc); }
With this method the parameters of the called process won't be initialized.
Definition: ProcessID call( <ProcessTypeID> )
Example: proc_id = call(type MyProcName);
- I'll leave the rest up to you to find out. Feel free to ask me on Booleansoup or on IRC (irc.blitzed.org #bilge) for example.
--Sandman 14:14, 23 July 2007 (CEST)