CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Sep 2010
    Posts
    3

    problems with DeviceIoControl in VB.net

    Hi. I am trying to convert this code written in VC++ (unmanaged) to VB .NET (managed). Could anyone please help?

    // set configuration - MUST be set to do a RESET
    if( !DeviceIoControl( DevHandle, IOCTL_USBIO_SET_CONFIGURATION,
    &config, sizeof(config), NULL, 0, &dw, NULL ) )

    where config is
    typedef struct _USBIO_INTERFACE_SETTING {
    USHORT InterfaceIndex;
    USHORT AlternateSettingIndex;
    ULONG MaximumTransferSize;
    } USBIO_INTERFACE_SETTING;

    typedef struct _USBIO_SET_CONFIGURATION {
    USHORT ConfigurationIndex;
    USHORT NbOfInterfaces;
    USBIO_INTERFACE_SETTING InterfaceList[USBIO_MAX_INTERFACES];
    } USBIO_SET_CONFIGURATION;
    and
    #define IOCTL_USBIO_SET_CONFIGURATION _USBIO_IOCTL_CODE(9,METHOD_BUFFERED)

    #define FILE_DEVICE_USBIO 0x8094

    //
    // Macros to generate IOCTL codes.
    // Note that function codes 0-2047 are reserved for Microsoft, and
    // 2048-4095 are reserved for customers.
    //
    #define _USBIO_IOCTL_BASE 0x800

    #define _USBIO_IOCTL_CODE(FnCode,Method) \
    ( (ULONG)CTL_CODE( \
    (ULONG)FILE_DEVICE_USBIO, \
    (ULONG)(_USBIO_IOCTL_BASE+(FnCode)), \
    (ULONG)(Method), \
    (ULONG)FILE_ANY_ACCESS \
    ) )

    My translation of this code is below but doesn't work. I am having troubles getting a valid intPtr pointer for config with the correct size. how do we translate this code using mashal class? I tried Marshal.StructureToPtr, I tried Marshal.Copy. Nothing worked! I tried to make classes instead of stuctures and use another overloaded DeviceIOControl() from kernel32.dll. In vain...

    The device in question is a spectrometer attached to the usb port. The C++ code works.

    My translation:
    <StructLayout(LayoutKind.Sequential)> _
    Structure USBIO_INTERFACE_SETTING
    Dim InterfaceIndex As UShort 'UShort
    Dim AlternateSettingIndex As UShort 'ushort
    Dim MaximumTransferSize As ULong 'ulong
    End Structure


    <StructLayout(LayoutKind.Sequential)> _
    Structure USBIO_SET_CONFIGURATION
    Public ConfigurationIndex As UShort
    Public NbOfInterfaces As UShort
    ' <MarshalAs(UnmanagedType.LPArray, SizeConst:=32)> Dim InterfaceList() As USBIO_INTERFACE_SETTING
    Public InterfaceList() As USBIO_INTERFACE_SETTING
    End Structure

    <DllImport("kernel32.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function DeviceIoControl(ByVal hDevice As IntPtr, _
    ByVal dwIoControlCode As Int32, ByVal lpInBuffer As IntPtr, _
    ByVal nInBufferSize As Int32, ByVal lpOutBuffer As IntPtr, _
    ByVal nOutBufferSize As Int32, ByRef lpBytesReturned As Int32, _
    ByVal lpOverlapped As Overlapped) As Boolean
    End Function

    ......
    Dim config As USBIO_SET_CONFIGURATION = New USBIO_SET_CONFIGURATION
    config.ConfigurationIndex = 0
    config.NbOfInterfaces = 1
    ReDim config.InterfaceList(32)
    Dim u As New USBIO_INTERFACE_SETTING()
    u.InterfaceIndex = 0
    u.AlternateSettingIndex = 0
    u.MaximumTransferSize = 64
    config.InterfaceList(0) = u

    'config.InterfaceList(0) = New USBIO_INTERFACE_SETTING()
    'config.InterfaceList(0).InterfaceIndex = 0
    'config.InterfaceList(0).AlternateSettingIndex = 0
    'config.InterfaceList(0).MaximumTransferSize = 64

    Dim configAddress As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(config))

    'Dim configAddress As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(config))

    Dim ConfigByte() As Byte
    ReDim ConfigByte(Marshal.SizeOf(config) - 1)
    Marshal.Copy(ConfigByte, 0, configAddress, Marshal.SizeOf(config))

    'Marshal.StructureToPtr(config, configAddress, False)
    'ZeroMemory(configAddress)

    Dim usbio As usbio_i = New usbio_i()
    'set configuration - MUST be set to do a RESET
    Dim succeeded As Boolean = DeviceIoControl(devHandle, usbio.IOCTL_USBIO_SET_CONFIGURATION, configAddress, Marshal.SizeOf(config), Nothing, 0, dw, Nothing)
    If Not succeeded Then
    status = Marshal.GetLastWin32Error()
    If status <> usbioerr.USBIO_ERR_ALREADY_CONFIGURED Then

    LastError = "IOCTL_USBIO_SET_CONFIGURATION - 0x&#37;08l" & status
    CloseHandle(devHandle)
    Return False
    End If
    End If

    ......

    Public ReadOnly IOCTL_USBIO_SET_CONFIGURATION As Int32 = _
    CTL_CODE(FILE_DEVICE_USBIO, _USBIO_IOCTL_BASE + 9, METHOD_BUFFERED, FILE_ANY_ACCESS)

    Please, please anybody help? When do we use Marshal.Copy and when Marshal.StructureToPtr?
    How do we handle structures that have embedded stuctures?
    And how do we use MarshalAs in a struct?
    It is all very confusing to me and nothing I tried worked. Please advice. thank you in advance.

    Adria
    Last edited by adria15228; September 24th, 2010 at 08:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured