C ++如何将系统序列号传递给变量?

时间:2012-02-04 07:47:02

标签: windows variables serial-number wmic

我已经能够检索我的系统序列号,但是如何将序列号本身传递给变量?

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

        ptr = system;

        ptr("wmic bios get serialnumber");      
    }

运行我的代码后,屏幕显示:

    SerialNumber
    xxxxxxxxxxxxx

完全是这样的。但我想要的只是将“x”传递给char变量,因为它中有一个破折号。程序从哪里调用序列号?有什么建议? (Windows 7 x64)

3 个答案:

答案 0 :(得分:1)

官方认可的通过C ++编程访问WMI的方法是COM API for WMI。具体参见WMI C++ Application Examples下的示例。

另一方面,如果您想快速访问序列号,请在这些行中添加一些内容到您的程序中:

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

答案 1 :(得分:0)

这是一种不使用文本文件的更好方法

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

答案 2 :(得分:0)

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