DeviceIoControl in BV.NET

Joined
Mar 3, 2003
Messages
9
Location
Norway
:confused:

Im trying to convert a VB6 example to VB.NET.
The example uses the DeviceIoControl function:

DeviceIoControl(hWD, IOCTL_WD_USB_SCAN_DEVICES, lpScan, LenB(lpScan), 0, 0, WinDriverGlobalDW, 0)

lpscan is a pointer to the Scan object.

My problem is i do not know how to make a pointer to a object.

Is it possible in VB.NET?

something like lpScan = pointer(Scan) ?????
 
It all depends on what the "Scan" object is. The Marshal class under System.Runtime.InteropServices should provide you with a method to do just what you need.

Also, you should be using the Marshal.SizeOf method instead of VB.NETs LenB function.
 
Thank you for taking the time to answer my post.
Your suggestion of using the Marshall Class is definately a step in the right direction.:)
My proggie now compiles but gives me an error message:
"Type WD_USB_SCAN_DEVICES can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed."
:confused: To explain the Scan Object/Structure I have extracted a bit of my code:

Code:
Structure WD_USB_ID
  Dim dwVendorId As Integer
  Dim dwProductId As Integer
End Structure

Structure WD_USB_HUB_GENERAL_INFO
  Dim fBusPowered As Integer
  Dim dwPorts As Integer
  Dim dwCharacteristics As Integer
  Dim dwPowerOnToPowerGood As Integer
  Dim dwHubControlCurrent As Integer
End Structure

Structure WD_USB_DEVICE_GENERAL_INFO
  Dim deviceId As WD_USB_ID
  Dim dwHubNum As Integer
  Dim dwPortNum As Integer
  Dim fHub As Integer
  Dim fFullSpeed As Integer
  Dim dwConfigurationsNum As Integer
  Dim deviceAddress As Integer
  Dim hubInfo As WD_USB_HUB_GENERAL_INFO
  Dim deviceClass As Integer
  Dim deviceSubClass As Integer
  Dim dwInterfaceNum As Integer
End Structure

Structure WD_USB_SCAN_DEVICES
  Dim searchId As WD_USB_ID
  Dim dwDevices As Integer
  <VBFixedArray(29)> Dim uniqueId() As Integer
   <VBFixedArray(29)> Dim deviceGeneralInfo() As WD_USB_DEVICE_GENERAL_INFO
   Dim dwStatus As Integer
  UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. 
  Public Sub Initialize()
     ReDim uniqueId(29)
     ReDim deviceGeneralInfo(29)
  End Sub
End Structure

Function WD_UsbScanDevice(ByRef hWD As Integer) As WD_USB_SCAN_DEVICES
  Dim Scan As WD_USB_SCAN_DEVICES
  Scan.Initialize()
  Dim memSize As Integer = System.Runtime.InteropServices.Marshal.SizeOf(Scan)
  Dim lpScan As IntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(memSize)
  Dim lpScan2 As Integer = lpScan.ToInt64()
  DeviceIoControl(hWD, IOCTL_WD_USB_SCAN_DEVICES, lpScan2, memSize, 0, 0, WinDriverGlobalDW, 0)
  Return Scan
End Function
 
Last edited by a moderator:
Try replacing the WD_USB_SCAN_DEVICES with the following declaration instead.
Code:
Structure WD_USB_SCAN_DEVICES
    Dim searchId As WD_USB_ID
    Dim dwDevices As Integer
    MarshalAs(UnmanagedType.LPArray, SizeConst:=29)>Dim uniqueId() As Integer
    MarshalAs(UnmanagedType.LPArray, SizeConst:=29)>Dim deviceGeneralInfo() As WD_USB_DEVICE_GENERAL_INFO
    Dim dwStatus As Integer
End Structure
As it stands now the SizeOf method is getting passed arrays of unfixed sizes, meaning it cant calculate the entire size of the structure.
 
Your declaration did not compile without the "<" so i Included those but unfortunately I still got the same ERROR message when trying the Marshal.SizeOf():
"Type WD_USB_SCAN_DEVICES can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed."

Code:
Structure WD_USB_SCAN_DEVICES
        Dim searchId As WD_USB_ID
        Dim dwDevices As Integer
        <MarshalAs(UnmanagedType.LPArray, SizeConst:=29)> Dim uniqueId() As Integer
        <MarshalAs(UnmanagedType.LPArray, SizeConst:=29)> Dim deviceGeneralInfo() As WD_USB_DEVICE_GENERAL_INFO
        Dim dwStatus As Integer
    End Structure
 

   Function WD_UsbScanDevice(ByRef hWD As Integer) As WD_USB_SCAN_DEVICES
        Dim Scan As WD_USB_SCAN_DEVICES
        Dim memSize As Integer = Marshal.SizeOf(Scan)
        Dim lpScan As IntPtr = Marshal.AllocHGlobal(memSize)
        Dim lpScan2 As Integer = lpScan.ToInt64()
        DeviceIoControl(hWD, IOCTL_WD_USB_SCAN_DEVICES, lpScan2, memSize, 0, 0, WinDriverGlobalDW, 0)
        Return Scan
    End Function

While searching for clues on the net I stumbeled over this article that I belive to be written by you:
http://www.elitevb.com/content/print.aspx?contentid=75
This article mentiones an update about the very core of my problem.
If you have written this update, would you give me an URL to where it is?
Do you have any other ideas of what Im doing wrong?
 
Last edited by a moderator:
Tor,

Have you managed to find a solution to this problem yet ? I have also come across this issue with Marshalling, and Im starting to tear my hair out over it... !

Im having difficulty passing in a structure to DeviceIOControl which contains arrays of other structures, which in turn contain array of other structures.....

My problem is a bit earlier on that yours, in that I have followed Dereks above advice. Unfortunately I am getting an error when attempting to get the size of the structure through Marshal.SizeOf(), and it is saying Type "TOC can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed."

Does anyone have any ideas what may be going wrong? If required I can post the relevant code here... ?

Cheers!

mogwai
 
Try applying a LayoutKind attribute, with Sequential, on the structures.
 
Originally posted by divil
Try applying a LayoutKind attribute, with Sequential, on the structures.
I have tried this, but this is not the root of the cause. The structures I am using are all complex types, containing arrays of other structures etc, etc. I have tried setting the <FieldOffset> attribute of each structure member, but still no joy. I have a few ideas up my sleeve which I need to try later today, but Im not confident theyll work...
 
Back
Top