我有一个Windows应用程序执行一个简单的例程来确定是否存在USB令牌。该方法在32位机器上始终正常工作,但在64位机器上进行测试时,我们开始发现意外的结果。结构大小,检查是64位还是32位
我打电话以下方法
[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);
的说明文件SP_DEVINFO_DATA结构告诉我们,CBSIZE是 大小,以字节为单位SP_DEVINFO_DATA结构。
如果我们计算CBSIZE对于一个32位的机这将是一个64位的机28和32。
我已经用不同的CBSIZE值重新编译测试这两台机器,我想知道的是如何计算这个作为运行?我的应用程序需要在两种架构上运行。
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);
}
感谢
罗汉
汉斯,这解决了如何通过实际大小的问题,所以我upvoting。不过,我仍然有点关心包装。没有意义的时候通过实际的大小。 – 2010-09-21 16:56:48
@Steven - 如果包装不正确,那么将cbSize设置为一个常量值实际上是一件非常糟糕的事情。它不会让API检测到你错了,并报告一个很好的错误。它会随机失败,因为它会读取垃圾。 – 2010-09-21 17:25:48
@Hans - 欢呼声,效果很棒! – 2010-09-21 19:46:08