Convert C++ DLL to VB.net Help
-
Hi, I am struggling to convert some functions from a C++ DLL to some function references in VB.net I dont program in C++, so I am hoping someone can help me with converting a couple of function prototypes: This are the C++ prototypes: CP2101_Open(DWORD dwDevice, HANDLE* cyHandle); CP2101_GetProductString(DWORD dwDeviceNum, LPVOID lpvDeviceString, DWORD dwFlags); CP2101_Close(HANDLE cyHandle ); This is what i have in vb.net: Public Declare Function CP2101_Open Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef Handle As Long) As Integer Public Declare Function CP2101_GetProductString Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef DeviceString As String, ByVal Options As Long) As Integer Public Declare Function CP2101_Close Lib "CP2101.dll" (ByVal DeviceNum As Long) As Integer I am not sure that I am converting the pointer data types correctly etc from C++ to Vb.net. If some could check my work. Thanks Barry
-
Hi, I am struggling to convert some functions from a C++ DLL to some function references in VB.net I dont program in C++, so I am hoping someone can help me with converting a couple of function prototypes: This are the C++ prototypes: CP2101_Open(DWORD dwDevice, HANDLE* cyHandle); CP2101_GetProductString(DWORD dwDeviceNum, LPVOID lpvDeviceString, DWORD dwFlags); CP2101_Close(HANDLE cyHandle ); This is what i have in vb.net: Public Declare Function CP2101_Open Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef Handle As Long) As Integer Public Declare Function CP2101_GetProductString Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef DeviceString As String, ByVal Options As Long) As Integer Public Declare Function CP2101_Close Lib "CP2101.dll" (ByVal DeviceNum As Long) As Integer I am not sure that I am converting the pointer data types correctly etc from C++ to Vb.net. If some could check my work. Thanks Barry
What problems are you experiencing here? The VB declarations look OK to me. :) Ant.
-
Hi, I am struggling to convert some functions from a C++ DLL to some function references in VB.net I dont program in C++, so I am hoping someone can help me with converting a couple of function prototypes: This are the C++ prototypes: CP2101_Open(DWORD dwDevice, HANDLE* cyHandle); CP2101_GetProductString(DWORD dwDeviceNum, LPVOID lpvDeviceString, DWORD dwFlags); CP2101_Close(HANDLE cyHandle ); This is what i have in vb.net: Public Declare Function CP2101_Open Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef Handle As Long) As Integer Public Declare Function CP2101_GetProductString Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef DeviceString As String, ByVal Options As Long) As Integer Public Declare Function CP2101_Close Lib "CP2101.dll" (ByVal DeviceNum As Long) As Integer I am not sure that I am converting the pointer data types correctly etc from C++ to Vb.net. If some could check my work. Thanks Barry
These nelsonbd wrote: This are the C++ prototypes: not true! These are C prototypes, else they would have very crazy export names in C++ name convention! Don't try it, just do it! ;-)
-
Hi, I am struggling to convert some functions from a C++ DLL to some function references in VB.net I dont program in C++, so I am hoping someone can help me with converting a couple of function prototypes: This are the C++ prototypes: CP2101_Open(DWORD dwDevice, HANDLE* cyHandle); CP2101_GetProductString(DWORD dwDeviceNum, LPVOID lpvDeviceString, DWORD dwFlags); CP2101_Close(HANDLE cyHandle ); This is what i have in vb.net: Public Declare Function CP2101_Open Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef Handle As Long) As Integer Public Declare Function CP2101_GetProductString Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef DeviceString As String, ByVal Options As Long) As Integer Public Declare Function CP2101_Close Lib "CP2101.dll" (ByVal DeviceNum As Long) As Integer I am not sure that I am converting the pointer data types correctly etc from C++ to Vb.net. If some could check my work. Thanks Barry
The potential problem I see is in interpreting the LPVOID DeviceString as a ByRef String. If I remember my VB right, a ByRef String is passed to a C function as a pointer to BSTR. That may not be (probably is not) what the C function is expecting. An LPVOID doesn't give very much information about what it is expecting: it could be a null-terminated string, a null-terminated wide-string, a BSTR or perhaps a pointer to BSTR. Do you have source for that C function, or a C example of its use?
-
Hi, I am struggling to convert some functions from a C++ DLL to some function references in VB.net I dont program in C++, so I am hoping someone can help me with converting a couple of function prototypes: This are the C++ prototypes: CP2101_Open(DWORD dwDevice, HANDLE* cyHandle); CP2101_GetProductString(DWORD dwDeviceNum, LPVOID lpvDeviceString, DWORD dwFlags); CP2101_Close(HANDLE cyHandle ); This is what i have in vb.net: Public Declare Function CP2101_Open Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef Handle As Long) As Integer Public Declare Function CP2101_GetProductString Lib "CP2101.dll" (ByVal DeviceNum As Long, ByRef DeviceString As String, ByVal Options As Long) As Integer Public Declare Function CP2101_Close Lib "CP2101.dll" (ByVal DeviceNum As Long) As Integer I am not sure that I am converting the pointer data types correctly etc from C++ to Vb.net. If some could check my work. Thanks Barry
Firstly, you should normally use
System.IntPtr
for opaque pointer types when using VB.NET. This will help you if you ever port to 64-bit (theIntPtr
type is 32 bit in a 32-bit process but 64 bits in a 64-bit process). As for CP2101_GetProductString, if the C/C++ code is interpreting the lpvDeviceString parameter as a string, you should usePublic Declare Function CP2101_GetProductString Lib "CP2101.dll" ( _
ByVal DeviceNum As Long, _
ByVal DeviceString As String, _
ByVal Options As Long) As IntegerByRef
indicates an in/out parameter. If the function alters this data, you should use aStringBuilder
object instead (still passedByVal
), and set itsCapacity
to the maximum size you expect the function to write. If you're gettingMissingMethodException
s, your DLL's exports may be mangled. To verify this, usedumpbin /exports
. If the output contains ? characters and no recognisable function names, the names are mangled. You have two choices. Either you can rewrite your declarations to use theDllImportAttribute
rather than Declare, and use theEntryPointName
property to indicate the mangled name, or you can specifyextern "C"
in front of the exported function declarations to tell the compiler not to mangle the names. Remember that the latter will break binary compatibility with any existing clients. Stability. What an interesting concept. -- Chris Maunder