ASM obtains the CPU serial number/CPUID, asmcpuid

Source: Internet
Author: User

ASM obtains the CPU serial number/CPUID, asmcpuid

I know there are two ways to obtain the CPU serial number: ASM and WMI.

If it is better, it must be the first choice for compilation. There is nothing to explain.

We only embed assembly in the C #, C ++, and E languages to obtain the CPUID.

First, let's take a look at,

The above is C # The CPUID obtained after the embedded assembly is run. Let's take a look at the easy language

In the preceding example, the output values of the E language embedded in the Assembly are the same,

It can prove that the embedded assembly is running normally,

The above is the result of the C ++ embedded assembly operation. The output values are the same,

I have found two different computers to test whether the code works.

Verification Figure 1:

Verification Diagram 2:

From this, you can clearly see that the CPUID of two different CPUs should not be consistent.

But you have clearly seen the result of different serial numbers, so you don't have to worry about code problems.

#include "stdafx.h"#include <stdio.h>int* GetGPUID(){__asm{mov eax,00hxor edx,edxcpuidmov dword ptr [ebp-4],edxmov dword ptr [ebp-8],eaxmov eax,01hxor ecx,ecxxor edx,edxcpuidmov dword ptr [ebp-12],edxmov dword ptr [ebp-16],eaxmov         eax,dword ptr [ebp-4]  mov         dword ptr [ebp-20],eax  mov         eax,dword ptr [ebp-8]  mov         dword ptr [ebp-24],eax  mov         eax,dword ptr [ebp-12]  mov         dword ptr [ebp-28],eax  mov         eax,dword ptr [ebp-16]  mov         dword ptr [ebp-32],eax  lea         eax, dword ptr[ebp-20]  pop         edipop         esipop         ebxmov         esp,ebppop         ebpret}}int _tmain(int argc, _TCHAR* argv[]){int* ptr = GetGPUID();int s1 = *ptr++;int s2 = *ptr++;int s3 = *ptr++;int s4 = *ptr;printf("%X%X%X%X", s1, s2, s3, s4);getchar();return 0;}

The Assembly Code Section embedded in C ++ has the same functions as C # and E.

. Version 2. supports library eAPI. subroutine _ start window _ created. local variable ptr, integer type. local variable s1, integer type. local variable s2, integer type. local variable s3, integer type. local variable s4, integer ptr = CallWindowProc ({85,139,236,129,236,192, 0, 0, 0, 83, 86, 87,141,189, 64,255,255,255,185, 48, 0, 0, 0,184,204,204,204,204,243,171,184, 0, 0, 0, 0, 0, 0, 51,210, 15,162,137, 85,252,137, 69,248,184, 1, 0, 0, 0, 51,201, 51,210, 15,162,137, 85,244,137, 69,240,139, 69,252,137, 69,236,139, 69,248,137, 69,232,139, 69,244,137, 69,228,139, 69,240,137, 69,224,141, 69,236, 95, 94, 91,139,229, 93,195}, # NULL, # NULL) s1 = pointer to INTEGER (ptr) s2 = pointer to INTEGER (ptr + 4) s3 = pointer to INTEGER (ptr + 8) s4 = pointer to INTEGER (ptr + 12) information Box (format text ("% X", s1, s2, s3, s4), # information icon ,,)

The above is the code for calling Assembly embedded in the E language, but I suggest you download the packaged source code.

It contains three different codes: C ++, e language, and C #. I don't like Java very much. You can look down on me,

I do not deny that I am a C # diaosi programmer. Although I have always been a dish, I am very happy and have a click.

The above compilation is performed on x86, so you need to set the target platform/development environment in the C # project attribute.

Change to x86. Otherwise, you will not be able to run all the code, so it will be funny, you say, isn't it?

Now we are writing code. We need to declare all constants and required APIs.

Otherwise, how will it be used?

    static partial class Program    {        [DllImport("kernel32.dll", SetLastError = true)]        private static extern bool VirtualProtect(byte[] lpAddress, int dwSize, uint flNewProtect, out uint lpflOldProtect);        [DllImport("user32.dll", SetLastError = true)]        private static extern IntPtr CallWindowProc(byte[] lpPrevWndFunc, int hWnd, int Msg, int wParam, int lParam);        private const int NULL = 0;        private const int PAGE_EXECUTE_READWRITE = 64;    }

Now we must write the Assembly Embedded in C # in the form of machine code.

    static partial class Program    {        private static readonly byte[] buf_asm = { 85, 139, 236, 129, 236, 192, 0, 0, 0, 83, 86, 87, 141, 189, 64, 255, 255, 255, 185, 48, 0, 0, 0, 184, 204, 204, 204, 204, 243, 171, 184, 0, 0, 0, 0, 51, 210, 15, 162, 137, 85, 252, 137, 69, 248, 184, 1, 0, 0, 0, 51, 201, 51, 210, 15, 162, 137, 85, 244, 137, 69, 240, 139, 69, 252, 137, 69, 236, 139, 69, 248, 137, 69, 232, 139, 69, 244, 137, 69, 228, 139, 69, 240, 137, 69, 224, 141, 69, 236, 95, 94, 91, 139, 229, 93, 195 };    }

Now we are starting to enter the regular mode and write the code to modify the memory protection. Otherwise

Once a call, GameOver was mentioned in the previous article.

If you need to look at the http://blog.csdn.net/u012395622/article/details/46801475

    static partial class Program    {        private static void VirtualProtect(byte[] address)        {            uint lpflOldProtect;            VirtualProtect(address, address.Length, PAGE_EXECUTE_READWRITE, out lpflOldProtect);        }    }

Okay, it's time to get ready to write the following code.

    static partial class Program    {        private static string GetCPUID()        {            VirtualProtect(buf_asm);            IntPtr ptr = CallWindowProc(buf_asm, NULL, NULL, NULL, NULL);            int s1 = Marshal.ReadInt32(ptr);            int s2 = Marshal.ReadInt32(ptr, 4);            int s3 = Marshal.ReadInt32(ptr, 8);            int s4 = Marshal.ReadInt32(ptr, 12);            return s1.ToString("X") + s2.ToString("X") + s3.ToString("X") + s4.ToString("X");        }    }

In this case, the pointer to an integer array is returned in the Assembly.

The integer array has only four values, so you will see that I have been calling Marshal. ReadInt32

Of course, you can also use the native pointer in C #. It feels sour. Haha,

And then link all the values of the S1-S4 to "% X hexadecimal text ".

Return to the caller. OK solves the problem. The last time I used the delegate call assembly, this is replaced

CallWindowProc calls the function address. I just don't want to write the delegate,

    static partial class Program    {        [STAThread]        static void Main()        {            Console.WriteLine(GetCPUID());            Console.ReadKey(false);        }    }

The above section calls the GetCPUID function in the C # Drop Main function. The meaning of this section

It's not big. Anyway, it's perfect. No worries. Mom says I don't knoW programming.

Source code: http://pan.baidu.com/s/1o602yXs // contains C ++, e language, C # Three language source code,

When adding the C # source code to your project, remember to switch the development environment to x86 first. Otherwise, you won't be able to run it,

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.