Is it capable to run on 64 bit platform?
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,
开发者_JAVA百科 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
.
精彩评论