Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

        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;
        }

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
212 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...