当前位置:   article > 正文

获取CPUID序列号的两种办法_mfc中获取cpu序列号

mfc中获取cpu序列号

    Win32k 平台上,获取CPUID的办法主要有两种,一种是利用 WMI 另一种是利用 x86 汇编的 cpuid 指令,而最快的办法就是通过汇编了,而且 WMI 与汇编之间效率上的差距的确有点让人难以忍受,WMI 获取 CPUID 的效率几乎接近了一秒钟,而利用 cpuid 指令的办法,大概是几个 us 时间的问题,这种令人咋舌的巨大差异,让人有些难以忍受。

  1. using System;
  2. using System.Management;
  3. using System.Runtime.InteropServices;
  4. static class Program
  5. {
  6. [DllImport("kernel32.dll", SetLastError = false)]
  7. private static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);
  8. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  9. public delegate int __cpuid(ref int s1, ref int s2);
  10. static void Main()
  11. {
  12. /*
  13. pushad
  14. mov eax, 01h
  15. xor ecx, ecx
  16. xor edx, edx
  17. cpuid
  18. mov ecx, dword ptr[ebp + 8]
  19. mov dword ptr[ecx], edx
  20. mov ecx, dword ptr[ebp + 0Ch]
  21. mov dword ptr[ecx], eax
  22. popad
  23. */
  24. 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 };
  25. IntPtr address = GCHandle.Alloc(shellcode, GCHandleType.Pinned).AddrOfPinnedObject();
  26. VirtualProtect(address, (uint)shellcode.Length, 0x40, out uint lpflOldProtect);
  27. __cpuid cpuid = (__cpuid)Marshal.GetDelegateForFunctionPointer(address, typeof(__cpuid));
  28. int s1 = 0;
  29. int s2 = 0;
  30. for (int i = 0; i < 100000; i++)
  31. {
  32. cpuid(ref s1, ref s2);
  33. }
  34. Console.Write("asm: {0}", s1.ToString("X2") + s2.ToString("X2"));
  35. using (ManagementClass mc = new ManagementClass("Win32_Processor"))
  36. {
  37. ManagementObjectCollection moc = mc.GetInstances();
  38. foreach (ManagementObject mo in moc)
  39. {
  40. Console.WriteLine(", wmi: {0}", mo.Properties["ProcessorId"].Value.ToString());
  41. }
  42. }
  43. Console.ReadKey(false);
  44. }
  45. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/253398
推荐阅读
相关标签
  

闽ICP备14008679号