Struct Size, check if 64-bit or 32-bit
<p>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.</p>

<p>I am calling the following method</p>

<pre><code>[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);
</code></pre>

<p>The documentation for the <a href="http://msdn.microsoft.com/en-us/library/ff552344%28VS.85%29.aspx" rel="nofollow">SP_DEVINFO_DATA</a> structure tells us that the <b>cbSize</b> is
the size, in bytes, of the SP_DEVINFO_DATA structure. </p>

<p>If we calculate cbSize for a 32-bit machine it will be 28 and 32 for a 64-bit machine.</p>

<p>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.</p>

<pre><code>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);
}
</code></pre>

<p>Thanks</p>

<p>Rohan</p>
