获取CPUID序列号的两种办法
生活随笔
收集整理的這篇文章主要介紹了
获取CPUID序列号的两种办法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? Win32k 平臺上,獲取CPUID的辦法主要有兩種,一種是利用 WMI 另一種是利用 x86 匯編的 cpuid 指令,而最快的辦法就是通過匯編了,而且 WMI 與匯編之間效率上的差距的確有點讓人難以忍受,WMI 獲取 CPUID 的效率幾乎接近了一秒鐘,而利用 cpuid 指令的辦法,大概是幾個 us 時間的問題,這種令人咋舌的巨大差異,讓人有些難以忍受。
using System; using System.Management; using System.Runtime.InteropServices;static class Program {[DllImport("kernel32.dll", SetLastError = false)]private static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);[UnmanagedFunctionPointer(CallingConvention.Cdecl)]public delegate int __cpuid(ref int s1, ref int s2);static void Main(){/*pushadmov eax, 01hxor ecx, ecxxor edx, edxcpuidmov ecx, dword ptr[ebp + 8]mov dword ptr[ecx], edxmov ecx, dword ptr[ebp + 0Ch]mov dword ptr[ecx], eaxpopad*/byte[] shellcode = { 96, 184, 1, 0, 0, 0, 51, 201, 51, 210, 15, 162, 139, 77, 8, 137, 17, 139, 77, 12, 137, 1, 97, 195 };IntPtr address = GCHandle.Alloc(shellcode, GCHandleType.Pinned).AddrOfPinnedObject();VirtualProtect(address, (uint)shellcode.Length, 0x40, out uint lpflOldProtect);__cpuid cpuid = (__cpuid)Marshal.GetDelegateForFunctionPointer(address, typeof(__cpuid));int s1 = 0;int s2 = 0;for (int i = 0; i < 100000; i++){cpuid(ref s1, ref s2);}Console.Write("asm: {0}", s1.ToString("X2") + s2.ToString("X2"));using (ManagementClass mc = new ManagementClass("Win32_Processor")){ManagementObjectCollection moc = mc.GetInstances();foreach (ManagementObject mo in moc){Console.WriteLine(", wmi: {0}", mo.Properties["ProcessorId"].Value.ToString());}}Console.ReadKey(false);} }總結
以上是生活随笔為你收集整理的获取CPUID序列号的两种办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络画板教程
- 下一篇: 超融合和服务器关系_超融合与传统服务器区