Click to See Complete Forum and Search --> : Calling a C DLL routine from Visual Basic
mce16
March 20th, 2000, 02:41 AM
Hi,
I have a C DLL routine which looks like this:
const char* C_routine(const char* msg, int length, const char* msg2);
How to call this routine from VB?
here is what i have done
Private Declare Function MyC Lib "test.dll" (ByVal msg As String, ByVal length As Integer, ByVal msg2 As String) As String
Private Sub Class_Initialize()
Dim test As String
test$ = C_routine("message", 7, "msg2") '//error message at this line!! Bad DLL calling convention
End Sub
The C_routine is executed as it supposed to, but at the end of execution, i got an error message saying "Bad DLL calling convention"!! what have i done wrong? Pls help.. Does the C_routine need to modify the parameters type in order to match VB type? if so, how??
Ravi Kiran
March 20th, 2000, 02:59 AM
Your C dll should be declared with _stdcall calling convention.
_stdcall const char* C_routine(const char* msg, int length, const char* msg2);
Even this will not WORK, because there is no char * equivalent in VB.
please remember that this kind of char * return may give troubles with VB as there are differences in the memory handling between the two. and if you dont free the memory then memory leaks/lockup.
Better would be typical windows way: pass in a allocated string , and also tell the size, the routine will fill in the buffer not exceeding the specified size: offhand example is GetPrivateProfileString fn.
/* Retuns the no. of chars filled in the buffer */
_stdcall long C_routine(const char* msg, int length, const char* msg2 , char * lpBuf, long buflen);
private Declare Function MyC Lib "test.dll" Alias "C_routine" (byval msg as string, byval length as Integer, byval msg2 as string, byval lpBuf as string , byval BufLen as Long ) as Long
private Sub Class_Initialize()
Dim test as string
'test$ = C_routine("message", 7, "msg2") '//error message at this line!! Bad DLL calling convention
test = space(256)
rtn = MyC("message",7,"msg2",test,256)
if rtn > 0 then
test = left$(Test,rtn)
end if
End Sub
Also you should define the C_Routine in a .Def file, otherwise you will get a "Dll Function not found" error
RK
Sigal Laniado
March 20th, 2000, 03:13 AM
You have to recieve the string as a BSTR.
const char* C_routine(BSTR msg, int length, BSTR msg2);
then in order to get char*
you have to declare
USES_CONVENSION in the begining of the routine and
char charMsg[256];
strcpy(charMsg, OLE2A(msg)
Lothar Haensler
March 20th, 2000, 04:08 AM
just one more comment:
a C "int" is a "Long" in VB!
Even in 32 bit VB versions, an "Integer" is only 2 Bytes, but a C int is 4 bytes.
FSt
March 20th, 2000, 07:47 AM
If you want to use LPCTSTR in your C/C++ code insert the following code in your stdafx.h
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
this code applies to MS VC++ ver 5.0 and higher
Mike010
March 20th, 2000, 10:58 AM
Here is the code for an .DLL I wrote to try and figure things out:
#include <windows.h>
void __stdcall GetMyComputerName( LPSTR szComputerName, LPSTR szErrorMsg, HWND hWnd )
{
MessageBox( hWnd, szComputerName, szErrorMsg, MB_OK );
}
It works fine in VB. Here is how I declared it in VB:
Declare Sub GetMyComputerName Lib "vb_dll.dll" (byval ComputerName as string, byval strErrorMsg as string, byval window as Long)
It works for me.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.