windowsvariablesserial-numberwmic

C++ How do I pass a system serial number into a variable?


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)


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:

    #include <stdio.h>
    #include <stdlib.h>
    
    /*...*/
    
    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); //`sn` now contains 2nd line
    
    fclose(fp);          //cleanup temp file
    remove("sn.txt");
    
    printf("The serial Number is: %ws\n", sn);