compile c++ dll for visual basic
HI, I have a previous c++ file that has all the code I need for my current project. Normally I would just rewrite it for visual basic but it requires tons of work arounds to complete. (The code involves nodes and pointers and tons of different stuff that visual basic doesn't fully support.) I understand there is a way to compile c++ code into a dll file though and call it in visual basic. I know you have to create a header file and a def file or something but I have no idea how about going about this. Any tutorials or sample code would be much appreciated.
Thank You
Re: compile c++ dll for visual basic
You don't need a def file, you should be able to import the functions directly as long as you use __stdcall. I work with a guy who uses VB and I've written quite a few libraries for him. It doesn't even need to be Visual C++, I do it in MinGW.
Re: compile c++ dll for visual basic
Just export the required functions as __stdcall (or WINAPI) and you can very well use that C++ DLL in VB.
Re: compile c++ dll for visual basic
Quote:
and you can very well use that C++ DLL in VB
Well, it will be so until it comes to exchanging strings, arrays, collections and other VB-specific stuff, when it immediately becomes a headache.
Re: compile c++ dll for visual basic
Quote:
I understand there is a way to compile c++ code into a dll file though and call it in visual basic.
Sure. The way is called ActiveX.
Re: compile c++ dll for visual basic
moddll.cpp:
Code:
#define ABSTR BSTR
char pText2[] = "0123456789 123456789 123456789 123456789";
extern "C" __declspec(dllexport) ABSTR __stdcall PassString(LPSTR pText)
{
int pTextLen = SysStringByteLen((ABSTR)pText);
strncpy(pText, pText2, pTextLen);
return SysAllocStringByteLen(pText2, strlen(pText2));
}
ABSTR __stdcall PassStringByDEFFile(LPSTR pText)
{
int pTextLen = SysStringByteLen((ABSTR)pText);
strncpy(pText, pText2, pTextLen);
return SysAllocStringByteLen(pText2, strlen(pText2));
}
__declspec(dllexport) ABSTR __stdcall PassStringByCPP(LPSTR pText)
{
int pTextLen = SysStringByteLen((ABSTR)pText);
strncpy(pText, pText2, pTextLen);
return SysAllocStringByteLen(pText2, strlen(pText2));
}
BOOL APIENTRY DllMain( HANDLE hModule, ...
moddll.def:
Code:
; moddll.def : Declares the module parameters.
LIBRARY "moddll.DLL"
EXPORTS
DllMain @1 PRIVATE
PassStringByDEFFile
basmoddll.bas:
Code:
Private Declare Function PassString Lib ".\moddll.dll" Alias "_PassString@4" (ByVal s As String) As String
Private Declare Function PassStringByDEFFile Lib ".\moddll.dll" (ByVal s As String) As String
Private Declare Function PassStringByCPP Lib ".\moddll.dll" Alias "?PassStringByCPP@@YGPAGPAD@Z" (ByVal s As String) As String
Sub Main()
Dim s As String, s1 As String
s = "xxxxx"
s1 = PassString(s)
s = "xxx"
s1 = PassStringByDEFFile(s)
s = "ZZZZZZZ"
s1 = PassStringByCPP(s)
End Sub