CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2006
    Posts
    16

    Question Get CPU Serial Number

    Dear All,

    I want to know how we can Get the CPU serial number using C++ code. I found some ASM instruction "cpuid" . But it was not working...

    Is there any API for getting CPU informations including CPU Serial Num ber ?. I know some API calls, but there no way to get CPUID from those APIs.

    Thanks to all...

    Regards,
    Ajai

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Get CPU Serial Number

    This thread might help you. Someone's posted a zip file which supposedly contains C++ code for obtaining the processor's serial number. I haven't tried it myself though.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  3. #3
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: Get CPU Serial Number

    This other thread might help you:
    http://www.codeguru.com/forum/showthread.php?t=355953

  4. #4
    Join Date
    Sep 2005
    Posts
    173

    Re: Get CPU Serial Number

    try to use WMI classes to retrieve CPU spesific information.
    use Win32_Processor class

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Get CPU Serial Number

    Making a search for for few magic words you'll find that was asked many, many times...
    Well, neither using whatever WMI function nor someting else (like CPUID instruction) can help to get CPU serial number.
    THIS THREAD can reveal that for you.
    Last edited by ovidiucucu; January 19th, 2006 at 04:42 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Mar 2006
    Posts
    1

    Re: Get CPU Serial Number

    I've two identical PC's, I use WMI classes and I obtain the identical information!!

    Any idea to obtain different ID ??


    These are the classes and objects that I use:
    From Win32_BaseBoard: Tag, Manufacturer, Product and Version
    From Win32_BIOS: SerialNumber
    From Win32_ComputerSystemProduct: IdentifyingNumber and UUID
    From Win32_Processor: ProcessorID

    And I also use ..Public Declare Function GetVolumeInformation& Lib "kernel32" Alias "GetVolumeInformationA".. from WinAPI32.

    My theorical UniqueID is:

    InfoBaseBoard & "_" & InfoBIOS & "_" & InfoComputer & "_" & InfoCPU & "_" & InfoDiskDriveC

    But, as I say before, I obtain a identical UniqueID with two identical PCs.

    ------------
    Sorry for my English.

  7. #7
    Join Date
    Jun 2007
    Posts
    2

    Re: Get CPU Serial Number

    Hello,
    I wonder if you could manage to obtain two different identifiers on two machines that are identical. I would need a hint myself.

    Thx for the info
    br

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Get CPU Serial Number

    "A problem well stated is a problem half solved.” - Charles F. Kettering

  9. #9
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Get CPU Serial Number

    Geez this thread is old.
    CPU serial numbers are not unique.

    You can use MAC addresses of NIC's installed. Although a user is able to change these values to whatever they want.

    You can get the volume serial number of a hard drive. Although I think user's can change this too.

  10. #10
    Join Date
    Oct 2013
    Posts
    1

    Re: Get CPU Serial Number

    #include <stdio.h>

    void getPSN(char *PSN)
    {int varEAX, varEBX, varECX, varEDX;
    char str[9];
    //%eax=1 gives most significant 32 bits in eax
    __asm__ __volatile__ ("cpuid": "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (1));
    sprintf(str, "%08X", varEAX); //i.e. XXXX-XXXX-xxxx-xxxx-xxxx-xxxx
    sprintf(PSN, "%C%C%C%C-%C%C%C%C", str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
    //%eax=3 gives least significant 64 bits in edx and ecx [if PN is enabled]
    __asm__ __volatile__ ("cpuid": "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (3));
    sprintf(str, "%08X", varEDX); //i.e. xxxx-xxxx-XXXX-XXXX-xxxx-xxxx
    sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
    sprintf(str, "%08X", varECX); //i.e. xxxx-xxxx-xxxx-xxxx-XXXX-XXXX
    sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
    }

    int main()
    {
    char PSN[30]; //24 Hex digits, 5 '-' separators, and a '\0'
    getPSN(PSN);
    printf("%s\n", PSN); //compare with: lshw | grep serial:
    return 0;
    }

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Get CPU Serial Number

    When posting code, please format properly first and use code tags. Go Advanced, select code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get CPU Serial Number

    amitmishra, thanks for your enthusiasm to help. Keep in mind that this thread has been around for 7 YEARS. There are many current threads that needs help.

    I am closing this thread now, because it has been revived in 2010 and now in 2013

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured