Tuesday, April 24, 2012

Unique CPU ID on Windows via C++

Well I would like to get an unique CPU ID so I can get an UNIQUE identification for each machine.
I did check __cpuid() from windows.h library, but the problem is that the ID it gets, it's the ID of all types of CPU, if you have an I3 Core processor and I have an I3 core processor, it will give the same "Unique ID".



Here is what I came up with to make a CPU ID from the __cpuid() that returns the same information on processors of the same type:



std::string GetCPUID(){

int CPUInfo[4] = {-1};
__cpuid(CPUInfo, 0);
if (CPUInfo[0] < 4)
return ""; //Error on retrieving

stringstream st;
for(int i = 0; i <= 3; i++){
st << CPUInfo[i];
}

return st.str();

}


I also checked a program called "Hardware ID Extractor" that works fine, it actually returns a UNIQUE CPU ID as the GUI as I wanted, so they made available a DLL so we can extract the CPU ID it generated , I tried everything with the cleanest code with the DLL and following their example but it seems that it returns nothing when ran on Windows 7. a lot of other people were complaining about this on Windows 7 via the DLL, even though the GUI on windows 7 returns the correct CPU ID.



Showing my work in the Hardware ID Extractor,



Here is the site where I downloaded the DLL (Where I chose the link of C++, VB, .NET): http://www.soft.tahionic.com/download-hdd_id/free-download/free%20download.html



And here is the DLL itself to download: http://www.soft.tahionic.com/download-hdd_id/free-download/DLL%20compressed/HardwareIDExtractorC.dll



This is the code I got from Hardware ID Extractor (I'm using Visual Studio 2008):



#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

int main (){

char* (__stdcall *GetIDESerialNumber)(BYTE);

HINSTANCE DllInst = NULL;
if (DllInst == NULL) DllInst = LoadLibrary("C:\\HardwareIDExtractorC.dll");
if (DllInst) {
GetIDESerialNumber = (char* (__stdcall*)(BYTE))GetProcAddress(DllInst, "GetIDESerialNumber");

//Now call the imported function
cout << "SN: " << GetIDESerialNumber(0); // 0 = first IDE hard drive in your system
}else{
printf("Not Loaded");
}

_getch();



return 0;
}


In the visual studio output it shows that the DLL was executed by saying:



'Test.exe': Loaded 'C:\HardwareIDExtractorC.dll', Binary was not built with debug information.


And the program output that I get is: Not loaded



Someone know what I can do to get a CPU ID Unique, like in the application Hardware ID Extractor? I need to get a CPU ID because if I get HDD information, the user may format their Hard Drive and the program will stop working, as I'm designing it to work only that specified computer.



I'm using Windows 7 32 bits Ultimate and Intel I3 Core Processor (if that helps in anyway).



I hope I was clear in my question.
Thanks in advance!





1 comment:

  1. Hi, i am also searching solution for the same prob. Did u get it to work. If this loadlibrary function will get to work than we can get ide serial number and cpuid. This sourcecode should return ide serial number. Actual function to return cpuid is "getcpuid()" from the same dll. If someone can solve this dll problem than i have nice algorithm to solve machine licensing. Plz. help if anyone have any solution.

    My system is Windows vista home premium sp 2

    contact me on bhargavmmehta@hotmail.com

    ReplyDelete