C++ 如何将系统序列号传递给变量? [英] 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
就是这样.但我想要的是将x"传递给一个 char 变量,因为它里面有一个破折号.程序从哪里调用序列号?有什么建议?(Windows 7 x64)
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)
推荐答案
官方认可的通过 C++ 以编程方式访问 WMI 的方法是 WMI 的 COM API.具体参见 WMI C++ 下的示例应用实例.
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\n",sn);
这篇关于C++ 如何将系统序列号传递给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!