I was just wondering how I would go about writing a DLL in C++ for VB6. I know how to make basic DLLs, but I need to be able to do complicated stuff, such as OpenGL API.
Printable View
I was just wondering how I would go about writing a DLL in C++ for VB6. I know how to make basic DLLs, but I need to be able to do complicated stuff, such as OpenGL API.
You need to press the Alt-Gorilla key :cool:
You make a DLL in C++ for VB exactly as you would make a DLL in C++ for anything else. C++ has no idea how you are going to use the DLL and doesn't care. The issue is how do you get VB to get at the functions you put in the DLL, and that's a bit messy.
You have to identify the name of the function in the DLL as it exists after C++ mangles it, and then you have to look up the "alias" statement in VB and figure out how to alias your local VB function name to the mangled C++ (DLL) name.
That's all there is to it. You do, of course, have to be very careful that you pass the right parameters of the right type and in the right order else the alias won't work. An BE CAREFUL about int vs long when talking from VB to C++, and watch out for pass by ref vs pass by val. It is possible to get a valid linkage between VB and the DLL but get weird results if you're not careful.
Have fun
The other method, if you want an API that behaves about like the DX8 VB API, is to learn about Automation. It is a weird concept for very advanced programmers that involves a lot of COM, IDispatch and VARIANTs. But it's the only way to get objects from a DLL to any VB version prior to 6.0 (6.0 understands normal COM objects as well, so you could learn those, which is required anyway if you want to learn about automation).
yeah.. unfortunately i haven't figure out how to export classes.
anyone have a clue?
Thanks
Exporting classes is different to creating a COM DLL, since C++ links to them differently (and a LOT more efficiently, you just don't get some of the distributed benefits though).
so would it be possible for me to use a c++ DLL in my VB app?
I've been trying for days without success. Any help would be appreciated.
THANKS!
Sadly not, C++ DLLs are for C++ only.
If you want to learn about COM, its best to look at the documentation in the Microsoft Platform SDK about COM, and ATL. You have a lot of reading to do ;)
actually i meant to type CLASS instead of DLL.
linking c++ dlls to VB is easy.
Public Declare Function TestFunc Lib "testDLL" (byval sSomeVar as string) as Long
where testDLL.dll is in the same folder with a function TestFunc that takes a string as a paramater.. compiled with c++.
and in vb you just
Call TestFunc(sSomeStr)
at any rate.. after exporting functions and classes from c++ DLLs i have only managed to successfully import functions.. not classes.
i've heard it can't be done, but if anyone knows for sure, let me know.
thanks
That's what I meant.
Normal functions (non-class) can easily be exported because it's just a case of calling a function which VB can do. What VB can't do is to interpret the binary layout of a C++ class, along with all the name mangling that is required.
Either way, you can't use a C++ class from VB unless you're using .NET in which case the whole playing field's been rebuilt :D
word.. .NET ehh? hmm... onwards and upwards i suppose. hehe.
Actually it is theoretically possible to export classes to VB - just not as classes:
Code:class ex {
long l1, l2;
void DoSomething();
};
VB Code:
Private Type classex l1, l2 As Long End Type Declare Sub ex_Construct Alias "ex::ex mangled" Lib "whatever.dll" (pThis As classes) Declare Sub ex_DoSomething Alias "ex::DoSomething mangled" Lib "whatever.dll" (pThis As classex) Declare Sub ex_Destruct Alias "ex::~ex mangled" Lib "whatever.dll" (pThis As classes)
Should work, because machine code doesn't know classes...
I think I'll leave you the amusing job of deciphering MS's mangling strategy and writing an auto-converter for VB (templates as well, lets not skimp :D).
Yeah, sure. I already know that C++ mangles always start with ?
I think I'm nearly done. :rolleyes: