赞
踩
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()
- {
- /*
- pushad
- mov eax, 01h
- xor ecx, ecx
- xor edx, edx
- cpuid
- mov ecx, dword ptr[ebp + 8]
- mov dword ptr[ecx], edx
- mov ecx, dword ptr[ebp + 0Ch]
- mov dword ptr[ecx], eax
- popad
- */
- 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);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。