Tor Henrik Hovi Posted March 5, 2003 Posted March 5, 2003 :confused: I'm 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) ????? Quote
*Gurus* Derek Stone Posted March 5, 2003 *Gurus* Posted March 5, 2003 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.NET's LenB function. Quote Posting Guidelines
Tor Henrik Hovi Posted March 6, 2003 Author Posted March 6, 2003 (edited) 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: 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 Edited March 6, 2003 by divil Quote
*Gurus* Derek Stone Posted March 6, 2003 *Gurus* Posted March 6, 2003 Try replacing the WD_USB_SCAN_DEVICES with the following declaration instead. 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 can't calculate the entire size of the structure. Quote Posting Guidelines
Tor Henrik Hovi Posted March 11, 2003 Author Posted March 11, 2003 (edited) 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." 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 I'm doing wrong? Edited March 11, 2003 by divil Quote
mogwai Posted May 18, 2003 Posted May 18, 2003 Tor, Have you managed to find a solution to this problem yet ? I have also come across this issue with Marshalling, and I'm starting to tear my hair out over it... ! I'm 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 Derek's 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 Quote
*Gurus* divil Posted May 18, 2003 *Gurus* Posted May 18, 2003 Try applying a LayoutKind attribute, with Sequential, on the structures. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
mogwai Posted May 19, 2003 Posted May 19, 2003 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 I'm not confident they'll work... Quote
Recommended Posts