none
LPVoid from Visual C++ 6.0 .dll to vb.net

    Question

  • Hello,

         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)

    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

    Description:

     

    1.

    DeviceNum—Index of the device for which the product description string, serial number or full path is desired.

    2.

    DeviceString—Variable of type CP210x_DEVICE_STRING returning the NULL terminated serial number, device description or full path string.

    3.

     

    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.

    Declare

     

    Auto Sub CP210x_GetProductString Lib "CP210xManufacturing.dll" (ByVal dwDeviceNum As IntegerByRef 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 You

    Options—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 1:22 PM

Answers

  • 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").

    Pseudocode...

    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.

    MessageBox.Show(deviceString.ToString())

     

    • Marked as answer by AgentBlueFlix Wednesday, January 05, 2011 12:11 PM
    Tuesday, January 04, 2011 8:57 PM

All replies

  • 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").

    Pseudocode...

    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.

    MessageBox.Show(deviceString.ToString())

     

    • Marked as answer by AgentBlueFlix Wednesday, January 05, 2011 12:11 PM
    Tuesday, January 04, 2011 8:57 PM
  • The biggest problem is that there is not really a standardized way to handle C++ strings.  The developer does what ever he wants.

    http://msdn.microsoft.com/en-us/library/22e4dash(v=VS.100).aspx

    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. 

    http://msdn.microsoft.com/en-us/library/8877bdk6.aspx?appId=Dev10IDEF1&l=EN-US&k=k(EHSECURITY);k(TargetFrameworkMoniker-

    One obstacle is that your unmanaged method signatures do not specify a length for the string.

    OutAttribute Class (System.Runtime.InteropServices)

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Wednesday, January 05, 2011 12:56 AM
  • Thank you both for your replies,

         It appears that my problem was not specifying a dimension for my StringBuilder.

    For Example:

    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.

    Wednesday, January 05, 2011 12:11 PM