This wiki is out of date, use the continuation of this wiki instead

Making DLLs

From FenixWiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 21:32, 19 November 2007 (edit)
Sandman (Talk | contribs)
m (Tips & Tricks)
← Previous diff
Revision as of 23:21, 19 November 2007 (edit) (undo)
Sandman (Talk | contribs)
m (Some handy functions)
Next diff →
Line 142: Line 142:
<big>'''void''' string_use ('''int''' stringID)</big> <big>'''void''' string_use ('''int''' stringID)</big>
-:Increase the usage counter of an string. Use this when you store the identifier of the string somewhere. So in practice this should always be called after string_new().+:Increase the usage counter of a string. Use this when you store the identifier of the string somewhere. So in practice this should always be called after string_new().
<big>'''const char*''' string_get ('''int''' stringID)</big> <big>'''const char*''' string_get ('''int''' stringID)</big>
Line 149: Line 149:
For the rest, see [[Media:Fxdll.h|fxdll.h]] in the Fenix source. If you'd like more documentation about a function or something else, let [[User:Sandman|me]] know (IRC/discussion page). For the rest, see [[Media:Fxdll.h|fxdll.h]] in the Fenix source. If you'd like more documentation about a function or something else, let [[User:Sandman|me]] know (IRC/discussion page).
 +
 +<big>'''void''' string_discard ('''int''' stringID)</big>
 +
 +:Decrease the usage counter of a string. Use this when you retrieve the identifier of the string and discard it.
---- ----
--[[User:Sandman|Sandman]] 22:27, 19 November 2007 (CET) --[[User:Sandman|Sandman]] 22:27, 19 November 2007 (CET)

Revision as of 23:21, 19 November 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. The only condition is that the number of parameters needs to be different:
FENIX_export ( "DLL_MYFUNC" , "I"  , TYPE_DWORD , DLL_MYFUNC ) ;
FENIX_export ( "DLL_MYFUNC" , "II" , TYPE_DWORD , DLL_MYFUNC2 ) ;
  • 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);

Some handy functions

void gr_con_printf (const char* fmt, ...)

Outputs the message pointed to by fmt in the Fenix console.

int string_new (const char* ptr)

Creates a new string for Fenix, a copy of the string pointed to by ptr (so you can free the passed string). Returns the stringID of the created string.

void string_use (int stringID)

Increase the usage counter of a string. Use this when you store the identifier of the string somewhere. So in practice this should always be called after string_new().

const char* string_get (int stringID)

Returns the contents of a string. This pointer is only valid as long as no other string function is called (so duplicate the string if needed).

For the rest, see fxdll.h in the Fenix source. If you'd like more documentation about a function or something else, let me know (IRC/discussion page).

void string_discard (int stringID)

Decrease the usage counter of a string. Use this when you retrieve the identifier of the string and discard it.

--Sandman 22:27, 19 November 2007 (CET)

Personal tools