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

发布于 2025-01-01 23:47:54 字数 392 浏览 0 评论 0原文

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

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

        ptr = system;

        ptr("wmic bios get serialnumber");      
    }

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

    SerialNumber
    xxxxxxxxxxxxx

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2025-01-08 23:47:54

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

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

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

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);
等往事风中吹 2025-01-08 23:47:54

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

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

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
も星光 2025-01-08 23:47:54
    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);
    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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文