它是能够在64位平台上运行? [英] Is it capable to run on 64 bit platform?
问题描述
VAR guidComPorts = Guid.Empty;
UInt32的的dwSize;
IntPtr的hDeviceInfo;
VAR缓冲=新的字节[512];
VAR的providerName =新[] {};
VAR spddDeviceInfo =新SpDevinfoData();
VAR BSTATUS = SetupDiClassGuidsFromName(宝姿,参考guidComPorts,1,出来的dwSize);
如果(BSTATUS)
{
hDeviceInfo = SetupDiGetClassDevs(
REF guidComPorts,
(IntPtr的)空,
(IntPtr的)空,
Digcf present | DigcfProfile);
如果(hDeviceInfo.ToInt32()!= 0)
{ 而(真)
{
spddDeviceInfo.CbSize = Marshal.SizeOf(spddDeviceInfo); //是这一行64位
BSTATUS = SetupDiEnumDeviceInfo(hDeviceInfo,n设备++,楼盘spddDeviceInfo);
打破;
} }
返回;
} }
没有,是不是64位的安全。虽然你的 hDeviceInfo
正确定义为的IntPtr
,你把它当作一个32位的值,当你比较吧。
此外,你不想来比较 IntPtr.Zero
。 SetupDiGetClassDevs 返回 INVALID_HANDLE_VALUE
时失败。 INVALID_HANDLE_VALUE
为-1。你必须的值的所有64位进行比较,以确定该函数失败。如果您尝试这样的:
如果(hDeviceInfo.ToInt32()!= -1)
这时如果返回的值是一样的东西0x100000001你就有可能出错。
您最好的选择是使用的SafeHandle 而不是的IntPtr
。
var guidComPorts = Guid.Empty;
UInt32 dwSize;
IntPtr hDeviceInfo;
var buffer = new byte[512];
var providerName = new[] { };
var spddDeviceInfo = new SpDevinfoData();
var bStatus = SetupDiClassGuidsFromName("Ports", ref guidComPorts, 1, out dwSize);
if (bStatus)
{
hDeviceInfo = SetupDiGetClassDevs(
ref guidComPorts,
(IntPtr)null,
(IntPtr)null,
DigcfPresent | DigcfProfile);
if (hDeviceInfo.ToInt32() != 0)
{
while (true)
{
spddDeviceInfo.CbSize = Marshal.SizeOf(spddDeviceInfo);// IS IT THIS LINE WORK FOR 64 BIT
bStatus = SetupDiEnumDeviceInfo(hDeviceInfo, nDevice++, ref spddDeviceInfo);
break;
}
}
return;
}
}
No, that is not 64-bit safe. Although your hDeviceInfo
is correctly defined as an IntPtr
, you treat it as a 32-bit value when you're comparing it.
Also, you don't want to compare against IntPtr.Zero
. SetupDiGetClassDevs returns INVALID_HANDLE_VALUE
when it fails. INVALID_HANDLE_VALUE
is -1. You have to compare all 64 bits of the value to determine if the function failed. If you try something like:
if (hDeviceInfo.ToInt32() != -1)
Then you risk an error if the returned value is something like 0x100000001.
Your best bet is to use SafeHandle rather than IntPtr
.
这篇关于它是能够在64位平台上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!