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

问题描述 投票:0回答:3

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

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

        ptr = system;

        ptr("wmic bios get serialnumber");      
    }

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

    SerialNumber
    xxxxxxxxxxxxx

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

windows variables serial-number wmic
3个回答
1
投票

官方认可的通过 C++ 以编程方式访问 WMI 的方法是 WMI 的 COM API。具体请参阅WMI C++ 应用程序示例下的示例。

另一方面,如果您想快速访问序列号,请在程序中添加以下内容:

#include <stdio.h>
#include <stdlib.h>

int main() {
    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);

    return 0;
}

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

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);
© www.soinside.com 2019 - 2024. All rights reserved.