通過WMI - Win32_Processor - ProcessorId獲取到的並不是CPU的序列號,也並不唯一

程式設計師海風發表於2014-10-13

現在網上不少教程,教人通過WMI - Win32_Processor - ProcessorId來獲取CPU的“序列號”,典型程式碼如下:

        public static string GetCPUSerialNumber()
        {
            string cpuSerialNumber = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuSerialNumber = mo["ProcessorId"].ToString();
                break;
            }
            mc.Dispose();
            moc.Dispose();
            return cpuSerialNumber;
        }

 

實際上,獲取到的值並不是CPU的編號或者序列號,也並不是唯一的,對此,微軟在msdn上有相關說明:

msdn連結:http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx

msdn上的原文是這樣說的:

ProcessorId

Data type: string
Access type: Read-only

Processor information that describes the processor features. For an x86 class CPU, the field format depends on the processor support of the CPUID instruction. If the instruction is supported, the property contains 2 (two) DWORD formatted values. The first is an offset of 08h-0Bh, which is the EAX value that a CPUID instruction returns with input EAX set to 1. The second is an offset of 0Ch-0Fh, which is the EDX value that the instruction returns. Only the first two bytes of the property are significant and contain the contents of the DX register at CPU reset—all others are set to 0 (zero), and the contents are in DWORD format.

 

根據msdn的說明可以知道,如果CPU支援CPUID指令的話,這個ProcessorId的值是指:“在暫存器EAX為1的時候,執行CPUID指令,返回的EAX和EDX暫存器裡的資料,包含的是CPU型號、支援的指令集等資訊”。如果CPU不支援CPUID指令呢?msdn沒說,估計應該是空吧。

 

所以,ProcessorId並不是CPU的編號或者序列號,只是CPU的型號,指令集等資訊。

 

參考連結:

[1]. Win32_Processor class

[2]. An attempt to bring CPUID to C#

 

相關文章