Code archives/Miscellaneous/Blitz3D Function Pointers (Hack)

This code has been declared by its author to be Public Domain code.

Download source code

Blitz3D Function Pointers (Hack) by MCP2006
Admittedly, this is a shoddy work around but it does work and stable (apparently)!! This info is aimed at those who prefer to write their own userlibs DLLs in C++ and would like the option of having the DLL call functions in the Blitz3D executable. Forgive any spelling mistakes, I've just returned home from a 14 hour shift!!!
*** The DLL part...

// a function pointer declaration for calling Blitz3D code
void (__stdcall *ABlitz3DFunction)(void);

// standard DLL function declaration for Blitz3D userlibs
MYDLLNAME_API void BBCALL SetBlitz3DFunction(void)
{
    UINT stackpos,adrs;
    UINT FAR *func;
    __asm
    {
        mov stackpos,esp
        mov esp,ebp
        add esp,4
        pop adrs
        mov esp,stackpos
     }
     func=(UINT FAR *)&ABlitz3DFunction;
    *func=adrs;
}

// extra DLL function for test purposes only - your DLL would call ABlitz3DFunction() directly!
MYDLLNAME_API void BBCALL CallBlitz3DFunction(void)
{
    ABlitz3DFunction();
}

*** The user libs part  (decls file) ....

.lib "mydllname.dll"

SetBlitz3DFunction():"_SetBlitz3DFunction@0"
CallBlitz3DFunction():"_CallBlitz3DFunction@0"

*** Blitz3D part....

;*** set function test

Global SETUP_FUNCS%=1
Gosub test_function
SETUP_FUNCS=0

CallBlitz3DFunction()     ;<-- testing DLL access only

WaitKey
End

.test_function:
                SetBlitz3DFunction()
If SETUP_FUNCS=0
	; *** function body goes here!!!
	Print "This works!!!!"
EndIf
	Return

*** that's it folks!  your DLL will call any code setup like this correctly and without problems even with the
*** blitz debugger running and you CAN place Stop commands and step through your Blitz3D function as normal.

Anyway, I hope this is of help to someone.
If anyone has a better way of doing this,  then please let me know.  Cheers :)

Comments

MCP2006
Damn - change bmfSetFunction() in the Blitz3D part to SetBlitz3DFunction()


BlitzSupport2006
I haven't tried this, but it sounds very clever.


Damn - change bmfSetFunction() in the Blitz3D part to SetBlitz3DFunction()



You should be able to edit and make that correction above--there should be an Edit button at the top-right of the code box.


MCP2006
Thanks James. The code's fixed too! :)


Difference2006
Sounds interesting, but I don't get it.

Could you post an example where you call a Blitz3D function with parameters from the a dll? Graphics3D() or something like that...

I'd be very interested if it was possible to make BlitzMax control Blitz3D this way.


MCP2006
Hi Peter, thanks for showing an interest.
The above code is'nt really designed for what you have in mind unfortunately! It was only written to show a possible way of solving Blitz3D's lack of function pointers, which as I'm sure you know, can be extremely useful.
In short, a typical blitz3d program would contain all those functions required of the application in mind, then presented to a DLL that has been written specifically for that application, which would hold an array of function pointers to those functions in the Blitz3D executable, allowing the programmer to write any extra code that's more convenient to write in a C++ environment.
This means that the DLL is intrinsically bound to the executable as much as the executable is bound to the DLL and cannot be used as a "stand alone" DLL.

Anyway, the technique is far from perfect, as with all "hacks" and there are pitfalls that come with such territory; accidently calling a NULL function pointer, avoiding infinate loops and so on...
That said it can be a powerful tool to use with careful programming and a bit of thought.

I'll write a small demo when I have some free time, which will show the technique in action, but you'll need MS Visual Studio 6 to be able to compile the DLL.

All the best, Roy


Panno2006
interesting ...

we will wait for a demo....


OJay2006
hm...but since you're using subs for this, there won't be any parameters? that makes this pretty limited...

otherwise it would be very interesting, if used e.g. with the LUA-userlib posted some months ago!


ZJP2009
Hi,

3 years !!!. Waiting for a demo ;-)

JP


Code Archives Forum