typedef struct _SP_DEVINFO_DATA { 
 DWORD cbSize; 
 GUID ClassGuid;
  DWORD DevInst; 
 ULONG_PTR Reserved;
} SP_DEVINFO_DATA, *PSP_DEVINFO_DATA;
在vc中sizeof取得结构的大小是28。转换到c#中
[StructLayout(LayoutKind.Sequential)]
        public class SP_DEVINFO_DATA
        {
            public UInt32 cbSize;
            public Guid ClassGuid;
            public UInt32 DevInst; 
            public ulong Reserved;
        };
     SP_DEVINFO_DATA spData = new SP_DEVINFO_DATA();
spData.cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(spData);大小为什么变成了32?

解决方案 »

  1.   

    C#中long是64位也就是8个字节
    ULONG_PTR是4个字节
    所以上面的结构多了4个字节
    32-4=28[StructLayout(LayoutKind.Sequential)]
            public class SP_DEVINFO_DATA
            {
                public UInt32 cbSize;
                public Guid ClassGuid;
                public UInt32 DevInst; 
                public IntPtr Reserved;  //<<<<<<<
            };
      

  2.   

    SP_DEVINFO_DATA spData = new SP_DEVINFO_DATA();
    spData.cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(spData);NewDeviceInfoSet = SetupDiGetClassDevs(0, "PCI", IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES); SetupDiEnumDeviceInfo(NewDeviceInfoSet, RequiredSize, ref spData);为什么这条语句执行没有成功??GetLastError返回ERROR_INVALID_USER_BUFFER,为什么,哪用错了吗?
      

  3.   

    ref:
    http://www.pinvoke.net/default.aspx/setupapi/SetupDiEnumDeviceInfo.html