Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

c# - 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,
                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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...