Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

windows - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
",sn);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...