Question

I've been able to retrieve my system serial number, but how do I pass the serial number itself into a variable?

    int main()
    {
        char newSerial;
        int (*ptr) (const char[]);

        ptr = system;

        ptr("wmic bios get serialnumber");      
    }

After running my code, the screen displays:

    SerialNumber
    xxxxxxxxxxxxx

exactly like this. But what I want is to pass just the "x's" into a char variable since it has a dash in it. Where exactly is the program calling the serial number from? Any suggestions? (Windows 7 x64)

Was it helpful?

Solution

The officially sanctioned way to get programatic access to WMI through C++ is the COM API for WMI. See specifically the examples under WMI C++ Application Examples.

If on the other hand, you want quick access to the serial number, add something along these lines to your program:

system("wmic bios get serialnumber > sn.txt");
wchar_t sn[16];
FILE* fp = fopen("sn.txt","r, ccs=UTF-8");
fgetws(sn,16,fp); //dummy read of first line
fgetws(sn,16,fp); //now sn contains 2nd line

fclose(fp);          //cleanup temp file
remove("sn.txt");  

printf("The serial Number is: %ws\n",sn);

OTHER TIPS

Here is a better approach without using text file

QProcess proc;
//proc.start("cscript " + path, QIODevice::ReadWrite);
proc.start("wmic bios get serialnumber",QIODevice::ReadWrite);
//qDebug() << path;
proc.waitForFinished();
QString uID = proc.readAll();
qDebug()<<uID; // serial number of the laptop
    ShellExecute(NULL, L"open", L"cmd.exe", L"/c wmic bios get serialnumber > sn.txt", NULL, SW_HIDE);

wchar_t sn[16];
FILE* fp = fopen("sn.txt","r, ccs=UTF-8");
fgetws(sn,16,fp); //dummy read of first line
fgetws(sn,16,fp); //now sn contains 2nd line

fclose(fp);          //cleanup temp file
remove("sn.txt");  

printf("The serial Number is: %ws\n",sn);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top