Page 2 of 2

Posted: Tue Jan 04, 2005 9:02 pm
by dracflamloc
So a C++ function exposed in a DLL can be called using CallFunction after Loading the DLL?

[edit]NM, thanks for you help I got it!

Posted: Tue Jan 04, 2005 9:23 pm
by olejr
I really dont know..
But if you can do something like this it should work:
(Don't copy/paste this.. :wink:)

Code: Select all

If (lib = OpenLibrary(#PB_Any, "SomeLib.dll")) = 0
  End
EndIf
If(ExamineLibraryFunctions(lib))
  While(NextLibraryFunction)
     Debug LibraryFunctionName()
   Wend
Else
  Debug "No Functions found"
EndIf
That is.. If You get some FunctionNames in the debugwindow...

Posted: Tue Jan 04, 2005 9:26 pm
by KarLKoX
dracflamloc wrote:So a C++ function exposed in a DLL can be called using CallFunction after Loading the DLL?
Yes, i am actually writing C++ code in a DLL for a PureBasic soundeditor, there are no limitations using this method :)

Posted: Tue Jan 04, 2005 9:37 pm
by dracflamloc
Yea I just called a simple void function I made in a DLL. Awesome!

The only thing I wonder about now is passing variables and getting returned values. But one step at a time! I'm sure I can figure it out easy enough.

Posted: Tue Jan 04, 2005 10:12 pm
by KarLKoX

Code: Select all

#ifdef	__cplusplus
	extern "C" {
#endif 

void DoAll()
{
  /* [...] */
}

#ifdef __cplusplus
}
#endif
Passing and returning values works the same way for C, just keep attention to not return double or > (mmx, sse reg ...) values ;)

Posted: Tue Jan 04, 2005 10:49 pm
by dracflamloc
Okay, great! Now to get back to business >=)

Posted: Wed Jan 05, 2005 3:07 am
by dracflamloc
Crap I spoke too soon. I get loads of errors trying to read values back from the DLL call. I'm just trying to return an integer.

Here's an example:

Code: Select all

ProcedureDLL.l i3D_Initialize3D(width.l,height.l,bits.l,isFullscreen.l) 
  OpenLibrary(20,".\PB_Irrlicht.dll")
  If IsLibrary(20)
    tmp.l=CallFunction(20,"?pb_Initialize3D@@YAHHHHH@Z",width,height,bits,isFullscreen)
    ProcedureReturn tmp
  Else
    ProcedureReturn 0
  EndIf
EndProcedure


//heres the C++ function
IRRLICHT_API int IRRCALLCONV pb_Initialize3D(int width,int height,int bits,int isFullscreen)
{
	pb_device = irr::createDevice(video::EDT_OPENGL,dimension2d<s32>(width,height),bits,pb_intToBool(isFullscreen),true,false);
	
	if (pb_device==0)
		return 0;

	//get video driver and scene manager and gui
	pb_driver = pb_device->getVideoDriver();
	pb_scene = pb_device->getSceneManager();
	pb_gui = pb_device->getGUIEnvironment();
	pb_scene->addCameraSceneNode(0,vector3df(0,0,0),vector3df(0,0,0));
	
	return 1;
}
When I try:
retval.l = i3D_Initialize3D(640,480,32,0)
it crashes when i do ProcedureReturn retval
with the error: "The instruction at 0x7blahblah has tried to read the data at 0x00000001 and caused an error: The memory could not be read."

What do I need to do to get this darn thing to work right?

Posted: Wed Jan 05, 2005 3:12 am
by FloHimself

Code: Select all

#define IRRLICHT_API __declspec(dllexport)
try calling the function with CallCFunction() as it seems to be cdecl.

Posted: Wed Jan 05, 2005 3:13 am
by dracflamloc
Oh shesh thanks I'm a moron! 8O

Could you give me a quick show of how to pass strings to functions and how to return a string? I have an idea of how so I'm gonna try it out, but any help would be nice as always. :wink:

Posted: Wed Jan 05, 2005 5:24 am
by KarLKoX

Posted: Wed Jan 05, 2005 8:00 am
by dracflamloc
Thanks, I love the PB community. I woulda been flamed out 10 times over anywhere else 8O