Calling C++ / external stuff via MAX

BlitzMax Forums/BlitzMax Programming/Calling C++ / external stuff via MAX

Newbie(Posted 2007) [#1]
I have an external library; like an SDK. It has a C++ API and an interface DLL. I also have some header files typically used in C / C++. How can I, if at all, from BlitzMAX, interface with this external library and call those functions it has from Blitz?


skidracer(Posted 2007) [#2]
It depends very much on the origin. If it is specifically for C++ developers it will need a C glue layer, if it's for both C and C++ I tend to import the C api example from blitzmax to start with to make sure I can build/run the test code from mingw/blitzmax:

Import "apiexample.c"

I then rename the main() function in the example code to apimain() and then call it from blitzmax after declaring apimain an external "C" function in blitzmax:

Extern "C"
Function apimain(args...)
End Extern

There are several million problems you may need to deal with so unless you are a skilled C/C++ programmer you may want to divulge ALL information in your posts, when it comes to C/C++ the important details will always be in the specifics. Every API is going to be different and unless they specifically mention MingW compatability you are possibly going to meet a lot of resistance.


Newbie(Posted 2007) [#3]
I just double checked; the origin is all C++ stuff. I have a bunch of header (.h files) and a DLL. + a doc describing the C++ functions. Thats about it ...


skidracer(Posted 2007) [#4]
If the headers feature some type of
extern "C"{

notation it may well be designed for C interfaces. If not and the C++ objects defined are not com based extending IUnknown/IDispatch then I would guess a BlitzMax interface is going to need a rather large amount of glue code.

Does the DLL in question have to be such a big secret?


Newbie(Posted 2007) [#5]
None of the .h files has the extern "C" ... guess I am screwed then


Brucey(Posted 2007) [#6]
guess I am screwed then

That's a bit defeatist, don't ya think?

I had thought wrapping a C++ library (with no C++ experience) was going to be a show-stopper for me too... but after hacking a couple of C libraries (which don't require much glue), it turned out that it wasn't so hard after all.

I ended up using both the tiny documented example in the Blitz help, and browsing through Skid's FLTK glue to give me a guide as to what I could do. (Thanks Skid! :-)

Essentially, I end up with a .cpp file of my own which I use Extern "C" to expose some of my own functions which can call into C++ objects etc.
Of course, you need to know a bit of C/C++, but if you intend wrapping an external library, it's to be expected (I usually learn what I need to know as I go :-p )

I would suggest taking a look at some of the already-wrapped libraries that people have done to point you in the right direction.


Newbie(Posted 2007) [#7]
Sounds like a plan! I will see if I can find Skid's FTLK glue ...


Newbie(Posted 2007) [#8]
@skidracer

Can i email you the .h files and you could "possibly" show me how I would do it, if its "doable"?


H&K(Posted 2007) [#9]
Why not just say what it is you are trying to wrap? There are some things that we know can be wrapped and in fact ppl have wrapped. And some things that we already know cannot be wrapped.


skidracer(Posted 2007) [#10]
Can i email you the .h files and you could "possibly" show me how I would do it, if its "doable"?


sure, email address is in my user info


Newbie(Posted 2007) [#11]
@skid

I sent you a personal email including some highly confidential information and files.

Thanks!!!!


Newbie(Posted 2007) [#12]
@skid
did you receive the files I sent?