I have an windows application that performs a simple routine to determine whether a USB token is present. The method has always worked correctly on 32-bit machines however during testing on a 64-bit machine we started to see unexpected results.
I am calling the following method
[StructLayout(LayoutKind.Sequential)]
internal struct SP_DEVINFO_DATA
{
public Int32 cbSize;
public Guid ClassGuid;
public Int32 DevInst;
public UIntPtr Reserved;
};
[DllImport("setupapi.dll")]
internal static extern Int32 SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, Int32 MemberIndex, ref SP_DEVINFO_DATA DeviceInterfaceData);
The documentation for the SP_DEVINFO_DATA structure tells us that the cbSize is
the size, in bytes, of the SP_DEVINFO_DATA structure.
If we calculate cbSize for a 32-bit machine it will be 28 and 32 for a 64-bit machine.
I have tested this on both machines by recompiling with different cbSize values, what i want to know is how can i calculate this as runtime? My application need to run on both architectures.
internal static Int32 GetDeviceInfoData(Int32 iMemberIndex)
{
_deviceInfoData = new Win32DeviceMgmt.SP_DEVINFO_DATA
{
cbSize = ?? // 28 When 32-Bit, 32 When 64-Bit,
ClassGuid = Guid.Empty,
DevInst = 0,
Reserved = UIntPtr.Zero
};
return Win32DeviceMgmt.SetupDiEnumDeviceInfo(_deviceInfoSet, iMemberIndex, ref _deviceInfoData);
}
Thanks
Rohan
Use Marshal.SizeOf: