C++ dll and blitzmax

BlitzMax Forums/BlitzMax Programming/C++ dll and blitzmax

plash(Posted 2008) [#1]
How do I send and receive a "const char *" variable between a dll and blitzmax?

How do I handle a value that looks like this (in the parameters of a function, inside the C++ dll) "CUserInfo &"? Is that an object pointer?

Is it possible to get the structure of "CUserInfo", so I can create the correct type in blitzmax?


Brucey(Posted 2008) [#2]
If you are calling from BlitzMax, you can either pass the String directly or first convert it to a CString.

A pseudo example...
Extern

  Function somedll_func(constcharstar:Byte Ptr)

End Extern

Local myString:String = "Hallo!"

somedll_func(myString)

Local s:Byte Ptr = myString.ToCString()

somedll_func(s)

MemFree(s)


Both are known to work. You just need to be careful if the string data needs to be around for longer than the call to the function. (eg. the function doesn't copy the string and uses the pointer to it, for use later).

:-)


plash(Posted 2008) [#3]
Why are you using extern to define the functions from the dll? (in fact, your not even loading a dll)

Here's what I've got so far:
SuperStrict

Framework brl.standardio
Import brl.stream
Import pub.win32
Import pub.stdc


Global Lib_Name:String = "EasyRPG116.dll"
Global Lib_Handle:Int = LoadLibraryA(Lib_Name)

Global l_USER_LOAD(file:Byte ptr, _CUserInfo:Byte ptr) = GetProcAddress(Lib_Handle, "?USER_Load@@YAHPBDAAVCUserInfo@@@Z")


Local file:String = "adminz_08.use", fuzz:Byte Ptr

l_USER_LOAD(file, fuzz)

DebugStop

FreeLibrary(Lib_Handle)

End

Extern "win32"
	Function FreeLibrary(hLibrary:Int)
	
End Extern


You might notice the weird function name, "?USER_Load@@YAHPBDAAVCUserInfo@@@Z", using notepad I found the actual export name for the function I want to use. In a debugger I found the parameters.

It gets the address to the function fine - no "Attempt to call uninitialized function pointer" errors.

When I try to call it, however, I get this: Unhandled Memory Exception Error at l_USER_LOAD(file, fuzz).
I'm not entirely certain I have _CUserInfo correct..

EDIT: Do I have to call DllEntryPoint, or is that done by LoadLibraryA()?


Brucey(Posted 2008) [#4]
Why are you using extern to define the functions from the dll? (in fact, your not even loading a dll)

Depends how you want to link your code to a dll really.

You can do it your way, or my way.
My way is platform agnostic. But each to their own :-)

btw, if that is a C++ API built from MSVC++ or friends, I don't hold out much hope for your interfacing with it like that from BlitzMax. Good luck anyhoo !


Azathoth(Posted 2008) [#5]
Why are you using extern to define the functions from the dll? (in fact, your not even loading a dll)
Load-time vs run-time. Run-time linking lets you check if the dll got loaded where load-time would just exit the program.

EDIT: Do I have to call DllEntryPoint, or is that done by LoadLibraryA()?
LoadLibrary does it.

When I try to call it, however, I get this: Unhandled Memory Exception Error at l_USER_LOAD(file, fuzz).
I'm not entirely certain I have _CUserInfo correct..
You're passing a null ptr, what is fuzz meant to contain?


plash(Posted 2008) [#6]
You're passing a null ptr, what is fuzz meant to contain?
A class structure, which I don't have.


Azathoth(Posted 2008) [#7]
How is the structure packed? It would probably be best to use banks.