Click here to Skip to main content
15,887,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anyone please tell me how get processor serial number(psn) using _cpuid() function i tried lot but i cant get. Please help me
Posted
Updated 18-Jun-23 20:48pm
Comments
Volynsky Alex 10-Aug-12 5:42am    
Please see following links:
http://www.codeproject.com/Articles/6359/Get-the-PIII-processor-serial-number-psn-in-Intel
http://stackoverflow.com/questions/5658975/c-get-processor-id

 
Share this answer
 
Comments
nilam Kasalkar 9-Aug-12 6:31am    
It return true or false for processor serial number.
I write code
int info[4] = { -1 };

__cpuid(info, 0);
if (info[0] < 1)
{
CAlert::InformationAlert("Not");
}

// Up to you...you do not need to mask results and you may use
// features bits "as is".
__cpuid(info, 1);
int family = info[0] & 0xf00;
int features = info[3] & 0xf000;

std::stringstream id;
id << std::hex << std::setw(4) << std::setfill('0') << family << features;

But i dont get
public string GetCPUId()
	{
	    string cpuInfo =  String.Empty;        
	    //create an instance of the Managemnet class with the
	    //Win32_Processor class
	    ManagementClass mgmt = new ManagementClass("Win32_Processor");
	    //create a ManagementObjectCollection to loop through
	    ManagementObjectCollection objCol = mgmt.GetInstances();
	    //start our loop for all processors found
	    foreach(ManagementObject obj in objCol)
	    {
	        if(cpuInfo == String.Empty)
	        {
            // only return cpuInfo from first CPU
	            cpuInfo = obj.Properties["ProcessorId"].Value.ToString();
	        }           
	    }
	    return cpuInfo;    
	}
    }


this program may help you to get processorID.
 
Share this answer
 
Comments
nilam Kasalkar 16-Aug-12 4:56am    
How to do this in MAC OS?
Please help.
try to use WMI classes to retrieve CPU spesific information.
use Win32_Processor class .
 
Share this answer
 
Comments
nilam Kasalkar 9-Aug-12 6:54am    
How to use Win32_Processor class in c++ to retrieve cpu number
Usually is invoked in the C program a function with asm code. Some ideas you can found in Wikipedia CPUID. In linux there are two options: dmidecode and libcpuid.
 
Share this answer
 
v2
Get the MAC (Media Access Control) address. But this may not be more interesting than the CPU ID.

- Get the name of the computer with:
Code:

C#
int get_computer_name(BYTE *computer_name, DWORD *computer_name_lg)
{
   HKEY hKey;
   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
               "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
               0, KEY_QUERY_VALUE, &hKey ) != ERROR_SUCCESS)
      return FALSE;
   if (RegQueryValueEx(hKey, "ComputerName", NULL, NULL,
                       (LPBYTE) computer_name,
                       (LPDWORD) computer_name_lg) != ERROR_SUCCESS) {
      RegCloseKey(hKey);
      return FALSE;
   }
   RegCloseKey(hKey);
   return TRUE;
}
...
   BYTE name[250] = "Default name";
   DWORD name_lg = 250;
   if (get_computer_name(name, &name_lg) == TRUE) {
   ...
   }

This is working well with my Windows ME computer and with my Windows XP computer. But, unfortunately there is no guarantee that computer names will be unique. It depends on the name that was assigned to them when Windows was installed.
 
Share this answer
 
v2
Comments
nilam Kasalkar 9-Aug-12 7:24am    
I need unique machine id
#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;
}
 
Share this answer
 
using System;
using System.Windows.Forms;
using System.Management;
5namespace trainingCSharp
{
public partial class Form1 : Form
{
public Form1()
1 {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
string info = "";
foreach (ManagementObject obj in searcher.Get())
{
try
{
info = obj.Properties["ProcessorId"].Value.ToString() + "\n";
}
catch { }
}
MessageBox.Show(info);
}

}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900