From: Pavel A. on 16 Apr 2010 07:22 There are lots of sites where you can read on .NET marshaling. For one, try this (and other articles there) http://www.codeproject.com/KB/IP/Marshal.aspx This newsgroup is more oriented to native C language. Good luck, -- pa "santapanda" <santapanda(a)discussions.microsoft.com> wrote in message news:E0ACF6B3-751A-41D2-B9C3-9EB7F821C0FB(a)microsoft.com... > Thanks m, > > But that didn't work. After getting the same error about the buffer size, > I > altered your suggestion to use UnmanagedType.U8 for cbSize, since I'm on a > 64-bit machine. That generated a new error: > > Type 'SP_DEVINFO_LIST_DTAIL_DATA' cannot be marshaled as an unmanaged > structure; no meaningful size or offset can be computed. > > I'm going to post a new thread just for SP_DEVINFO_LIST_DETAIL_DATA. > Thanks > for all of your help so far; you were extremely helpful. > > santapanda > > "m" wrote: > >> try something like this: >> >> (Untested code) >> >> [StructLayout(LayoutKind.Sequential)] >> public struct SP_DEVINFO_LIST_DETAIL_DATA >> { >> [MarshalAs(UnmanagedType.U4)] >> public UInt32 cbSize; >> >> [MarshalAs(UnmanagedType.Struct)] >> public System.Guid classGuid; >> >> [MarshalAs(UnmanagedType.SysInt)] >> public IntPtr remoteMachineHandle; >> >> [MarshalAs(UnmanagedType.ByValArray, SizeConst = >> SP_MAX_MACHINENAME_LENGTH, >> ArraySubType = UnmanagedType.char)] >> public char[] RemoteMachineName; >> } >> >> I have never used this one before in pInvoke, so I have just >> cut-and-paste >> some def'ns that might work. >> >> "santapanda" <santapanda(a)discussions.microsoft.com> wrote in message >> news:63D60702-5930-47E2-B157-D8D8BBC3877E(a)microsoft.com... >> > Thanks M, >> > >> > Now I'm having the same issue with an SP_DEVINFO_LIST_DTAIL_DATA >> > structure. >> > >> > My structure is defined as >> > >> > [StructLayout(LayoutKind.Sequential)] >> > public struct SP_DEVINFO_LIST_DETAIL_DATA >> > { >> > public UInt32 cbSize; >> > public System.Guid classGuid; >> > public IntPtr remoteMachineHandle; >> > public string RemoteMachineName; >> > } >> > >> > The code has: >> > >> > SP_DEVINFO_LIST_DETAIL_DATA dList = new SP_DEVINFO_LIST_DETAIL_DATA(); >> > >> > dList.cbSize = (UInt32) Marshal.SizeOf(dList); >> > >> > When I call SetupDiGetDeviceInfoListDetail(deviceInfoSet, out dList), >> > I'm >> > getting the same error. This time, a run through debugger finds that >> > cbSize >> > is set to 40. I tried to apply the same logic I found on pinvoke for >> > SP_DEVINFO_DATA, that on a 64-bit computer, the size is 4 + (32-bit). >> > But >> > setting 40, 44, 32, or 28 do not work. >> > >> > How do I figure out the correct size? I'm new to Marshalling unmanaged >> > code, so I'm learning this by brute force; but I'm trying to understand >> > what >> > I'm doing rather than just mimicking what I'm being told/seen from >> > other >> > examples. >> > >> > Thanks in advance for your help! >
From: m on 16 Apr 2010 23:18 I have only done this a couple of times. I had trouble the first time because deciphering the VERY confusing docs for pInvoke in .NET 1 was not fun, but since they they have improved the docs dramatically (and after I spent time looking at the binary, everything made much more sense ;). All I can suggest is that you look at the declaration attribute classes in MSDN and understand what they do before trying to blindly write code. BTW: according to MSDN, the first member of the SP_DEVINFO_LIST_DETAIL_DATA structure is defined as DWORD http://msdn.microsoft.com/en-us/library/ff552345(VS.85).aspx The correct marshaling for DWORD is usually UnmanagedType.U4 regardless of the machine architecture (32 or 64 bit etc.). If you define the types correctly, then the same code will work correctly on ALL versions of Windows on ALL architectures currently supported. "santapanda" <santapanda(a)discussions.microsoft.com> wrote in message news:E0ACF6B3-751A-41D2-B9C3-9EB7F821C0FB(a)microsoft.com... > Thanks m, > > But that didn't work. After getting the same error about the buffer size, > I > altered your suggestion to use UnmanagedType.U8 for cbSize, since I'm on a > 64-bit machine. That generated a new error: > > Type 'SP_DEVINFO_LIST_DTAIL_DATA' cannot be marshaled as an unmanaged > structure; no meaningful size or offset can be computed. > > I'm going to post a new thread just for SP_DEVINFO_LIST_DETAIL_DATA. > Thanks > for all of your help so far; you were extremely helpful. > > santapanda > > "m" wrote: > >> try something like this: >> >> (Untested code) >> >> [StructLayout(LayoutKind.Sequential)] >> public struct SP_DEVINFO_LIST_DETAIL_DATA >> { >> [MarshalAs(UnmanagedType.U4)] >> public UInt32 cbSize; >> >> [MarshalAs(UnmanagedType.Struct)] >> public System.Guid classGuid; >> >> [MarshalAs(UnmanagedType.SysInt)] >> public IntPtr remoteMachineHandle; >> >> [MarshalAs(UnmanagedType.ByValArray, SizeConst = >> SP_MAX_MACHINENAME_LENGTH, >> ArraySubType = UnmanagedType.char)] >> public char[] RemoteMachineName; >> } >> >> I have never used this one before in pInvoke, so I have just >> cut-and-paste >> some def'ns that might work. >> >> "santapanda" <santapanda(a)discussions.microsoft.com> wrote in message >> news:63D60702-5930-47E2-B157-D8D8BBC3877E(a)microsoft.com... >> > Thanks M, >> > >> > Now I'm having the same issue with an SP_DEVINFO_LIST_DTAIL_DATA >> > structure. >> > >> > My structure is defined as >> > >> > [StructLayout(LayoutKind.Sequential)] >> > public struct SP_DEVINFO_LIST_DETAIL_DATA >> > { >> > public UInt32 cbSize; >> > public System.Guid classGuid; >> > public IntPtr remoteMachineHandle; >> > public string RemoteMachineName; >> > } >> > >> > The code has: >> > >> > SP_DEVINFO_LIST_DETAIL_DATA dList = new SP_DEVINFO_LIST_DETAIL_DATA(); >> > >> > dList.cbSize = (UInt32) Marshal.SizeOf(dList); >> > >> > When I call SetupDiGetDeviceInfoListDetail(deviceInfoSet, out dList), >> > I'm >> > getting the same error. This time, a run through debugger finds that >> > cbSize >> > is set to 40. I tried to apply the same logic I found on pinvoke for >> > SP_DEVINFO_DATA, that on a 64-bit computer, the size is 4 + (32-bit). >> > But >> > setting 40, 44, 32, or 28 do not work. >> > >> > How do I figure out the correct size? I'm new to Marshalling unmanaged >> > code, so I'm learning this by brute force; but I'm trying to understand >> > what >> > I'm doing rather than just mimicking what I'm being told/seen from >> > other >> > examples. >> > >> > Thanks in advance for your help! >
From: Chris on 21 Apr 2010 14:55 On Apr 15, 11:56 am, "Maxim S. Shatskih" <ma...(a)storagecraft.com.no.spam> wrote: > > Type 'SP_DEVINFO_LIST_DTAIL_DATA' cannot be marshaled as an unmanaged > > structure; no meaningful size or offset can be computed. > > Design a higher-level API which will not use such structures, implement it in > unmanaged DLL by calling SetupDiXxx, and pinvoke to it. Actually I would say: make a C++/.NET shim that does all this for you. No PInvoke required.
From: Pavel A. on 21 Apr 2010 17:55 "Chris" <chris.aseltine(a)gmail.com> wrote in message news:d0bd0844-4035-4572-a028-f62b0f65cd01(a)j21g2000yqh.googlegroups.com... > On Apr 15, 11:56 am, "Maxim S. Shatskih" > <ma...(a)storagecraft.com.no.spam> wrote: > >> > Type 'SP_DEVINFO_LIST_DTAIL_DATA' cannot be marshaled as an unmanaged >> > structure; no meaningful size or offset can be computed. >> >> Design a higher-level API which will not use such structures, implement >> it in >> unmanaged DLL by calling SetupDiXxx, and pinvoke to it. > > Actually I would say: make a C++/.NET shim that does all this for > you. No PInvoke required. But this will render the code "unsafe" (whatever it's worth). -- pa
From: Maxim S. Shatskih on 22 Apr 2010 08:39 > But this will render the code "unsafe" Why? -- Maxim S. Shatskih Windows DDK MVP maxim(a)storagecraft.com http://www.storagecraft.com
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Replacing USBSTOR.sys Next: Can DIFx be used to install NDIS protocol driver? |