Tuesday, January 04, 2011 1:22 PM
I am currently adding USB capability to my hardware application and am having a little trouble using the manufacturer provided .dll in my vb.net program.
Here is a description of theVisual C++ 6.0 API function which i am trying to call from my vb.net program:
Funtion name: CP210x_GetProductString
CP210x_GetProductString(DWORD dwDeviceNum, LPVOID lpvDeviceString, DWORD dwFlags)
// Type definitions
typedef char CP210x_DEVICE_STRING[CP210x_MAX_DEVICE_STRLEN];
// Buffer size limits
#define CP210x_MAX_DEVICE_STRLEN 256
DeviceNum—Index of the device for which the product description string, serial number or full path is desired.
DeviceString—Variable of type CP210x_DEVICE_STRING returning the NULL terminated serial number, device description or full path string.
I am able to pass the the dwDeviceNum and dwFlag using the following decleration in my vb.net program, but i am unable to return the DeviceString.
Auto Sub CP210x_GetProductString Lib "CP210xManufacturing.dll" (ByVal dwDeviceNum As Integer, ByRef DeviceString As Byte(), ByVal dwFlags As Byte)
I have tried declaring my DeviceString as a few different data type, but an unable to retreive any data. Can anyone point me in the rite direction? I have never had to mix code from Visial C++ in my VB.net application and am a little lost.
Thank YouOptions—Flag that determines if DeviceString contains the product description, serial number or full path string.This function returns a null terminated serial number (S/N) string, product description string or full path string for the device specified by an index passed in the DeviceNum parameter. The index of the first device is 0 and the index of the last device is the value (NumDevices) returned by CP210x_GetNumDevices() - 1.
Tuesday, January 04, 2011 8:57 PM
dwFlags is a DWORD which is a 32 bit integer type, so use Integer.
For a fixed length array of chars use the StringBuilder class, initialized to the correct capacity (see the msdn topic "Default marshaling for strings").
Declare Auto Sub blahblah (ByVal dwDeviceNum As Integer, ByVal deviceString As StringBuilder, ByVal flags As Integer)
Then use it like this:
Dim index As Integer = blah...
Dim flags As Integer = blah...
Dim deviceString As New StringBuilder(256)
CP210x_GetProductString(index, deviceString, flags)
If it worked then you can get the string out of the string builder now by calling ToString, e.g.
- Marked As Answer by AgentBlueFlix Wednesday, January 05, 2011 12:11 PM
Wednesday, January 05, 2011 12:56 AM
The biggest problem is that there is not really a standardized way to handle C++ strings. The developer does what ever he wants.
I would be disinclined to use StringBuilder unless the C++ assembly because objects have extra fields. Use a value type. Like a byte array, or a char array. The problem is that you must pass it by reference, which can become tricky in VB.
One obstacle is that your unmanaged method signatures do not specify a length for the string.
Mark the best replies as answers. "Fooling computers since 1971."
Wednesday, January 05, 2011 12:11 PM
Thank you both for your replies,
It appears that my problem was not specifying a dimension for my StringBuilder.
Using Dim deviceString as StringBuilder
Instead of Dim deviceString as new StringBuilder(256)
If trying to retrieve the deviceString without declaring a dimension for StringBuilder "Attempted to read or write protected memory." exception is thrown.
I am now able to retrieve the deviceString using the suggestions posted by jo0ls
Thank You everyone for your help.