Hi,
Im a VC++ guru and like to do some things in VB(A) sometimes too, so maybe I
might be of your help..
"vbMental" wrote:
I have a solution written entirely in visual basic.net and I am trying to
interface it with an "old school" program. This old school program expects a
"c style dll" and a function exported from that dll for it to call.
I attempted to do this by creating a managed c++ project, exporting a
function with extern "C" _dllspec(export). In the function, I call to my
vb.net class library.
The problem is that I am not a c++ guru (and never was). I understand the
syntax but it's not my thing and something about this project just does not
seem right. Is there a right way of doing this: "bridging an c style dll to
managed code written in vb.net"?
If the dll is written and compile in c++ then you need to have following
things :
DLL must be a "regular DLL" (using MFC either as static/shared libraries).
Export things using (example) :
in header-/ and source-file (h/cpp-files) :
extern "C" void WINAPI MySub(LPCTSTR param1, LPTSTR param2);
extern "C" BOOL WINAPI MyFunc(long param1, int param2);
in definition-file (def) :
MySub
MyFunc
In (either) VB or VBA :
' for a procedure...
Declare Sub MySub Lib "MyC_Dll" ( _
ByVal param1 As String, _
ByVal param2 As String, _
)
' for a function...
Declare Function MyFunc Lib "MyC_DLL" ( _
ByVal param1 As Long, _
ByVal param2 As Integer, _
) As Long ' a C++ BOOL is either 0 for FALSE or anything else for TRUE
when transporting a VB-string to C++ it has to be converted to MultiByte
characters (while VB uses the BString type/ aka StringVariant).
when transporting vb-strings to a c-dll (either as LPCTSTR or LPTSTR), do
the following :
dim myString as String
myString = String$(260, vbNullChar) ' c's MAXPATH=260chars
' if you'd want to give a c-NULL pointer to a c-function then call it as
follow:
call MySub(myString, vbNullString) ' in C/C++ it's like "MySub(s, NULL);"
you cannot pass a vb-string to get it filled by a c-dll, first fill a
stringvariable with vbNullChar characters (see above, as I did with
'myString') then pass it as a parameter to the c-function.
When the c-function has finished, do the following to get back a vb-string :
Dim X As Integer, newString as String
X = InStr(myString, vbNullChar)
If X > 0 Then
newString = Left$(myString, X - 1)
Else
newString = myString
End If
Hope I helped you a bit
....