Is it capable of working on a 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, 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; } } 
-1
source share
1 answer

No, it is not 64 bit safe. Although your hDeviceInfo correctly defined as IntPtr , you treat it like a 32-bit value when you compare it.

In addition, you do not want to compare with IntPtr.Zero . SetupDiGetClassDevs returns INVALID_HANDLE_VALUE when it fails. INVALID_HANDLE_VALUE is -1. You must compare all 64 bits of the value to determine if the function worked. If you try something like:

 if (hDeviceInfo.ToInt32() != -1) 

Then you risk the error if the return value is something like 0x100000001.

It is best to use SafeHandle rather than IntPtr .

+1
source

All Articles