如何从C++ Windows注册表中获取计算机制造商和模型? 我正在编写自己的C++代码,通过读取和解析注册表项来读取Windows计算机上的计算机模型和制造商。 HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/services/mssmbios/Data/SMBiosData 在Windows /C++ + VisualStudio中是否有任何库函数允许我直接获取这些信息?< P>您需要解释的步骤。MSDN甚至包括。您只需要更改两个字符串 将从Win32\u进程中选择*更改为从Win32\u计算机系统中选择* 将名称更改为制造商,然后再次更改型号。
在的帮助下,我创建了这个方法如何从C++ Windows注册表中获取计算机制造商和模型? 我正在编写自己的C++代码,通过读取和解析注册表项来读取Windows计算机上的计算机模型和制造商。 HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/services/mssmbios/Data/SMBiosData 在Windows /C++ + VisualStudio中是否有任何库函数允许我直接获取这些信息?< P>您需要解释的步骤。MSDN甚至包括。您只需要更改两个字符串 将从Win32\u进程中选择*更改为从Win32\u计算机系统中选择* 将名称更改为制造商,然后再次更改型号。,c++,windows,visual-studio-2010,C++,Windows,Visual Studio 2010,在的帮助下,我创建了这个方法 #include <Wbemidl.h> #pragma comment(lib, "wbemuuid.lib") std::pair<CString,CString> getComputerManufacturerAndModel() { // Obtain the initial locator to Windows Management on a particular host computer. IWbemLocat
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
std::pair<CString,CString> getComputerManufacturerAndModel() {
// Obtain the initial locator to Windows Management on a particular host computer.
IWbemLocator *locator = nullptr;
IWbemServices *services = nullptr;
auto hResult = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&locator);
auto hasFailed = [&hResult]() {
if (FAILED(hResult)) {
auto error = _com_error(hResult);
TRACE(error.ErrorMessage());
TRACE(error.Description().Detach());
return true;
}
return false;
};
auto getValue = [&hResult, &hasFailed](IWbemClassObject *classObject, LPCWSTR property) {
CString propertyValueText = "Not set";
VARIANT propertyValue;
hResult = classObject->Get(property, 0, &propertyValue, 0, 0);
if (!hasFailed()) {
if ((propertyValue.vt == VT_NULL) || (propertyValue.vt == VT_EMPTY)) {
} else if (propertyValue.vt & VT_ARRAY) {
propertyValueText = "Unknown"; //Array types not supported
} else {
propertyValueText = propertyValue.bstrVal;
}
}
VariantClear(&propertyValue);
return propertyValueText;
};
CString manufacturer = "Not set";
CString model = "Not set";
if (!hasFailed()) {
// Connect to the root\cimv2 namespace with the current user and obtain pointer pSvc to make IWbemServices calls.
hResult = locator->ConnectServer(L"ROOT\\CIMV2", nullptr, nullptr, 0, NULL, 0, 0, &services);
if (!hasFailed()) {
// Set the IWbemServices proxy so that impersonation of the user (client) occurs.
hResult = CoSetProxyBlanket(services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);
if (!hasFailed()) {
IEnumWbemClassObject* classObjectEnumerator = nullptr;
hResult = services->ExecQuery(L"WQL", L"SELECT * FROM Win32_ComputerSystem", WBEM_FLAG_FORWARD_ONLY |
WBEM_FLAG_RETURN_IMMEDIATELY, nullptr, &classObjectEnumerator);
if (!hasFailed()) {
IWbemClassObject *classObject;
ULONG uReturn = 0;
hResult = classObjectEnumerator->Next(WBEM_INFINITE, 1, &classObject, &uReturn);
if (uReturn != 0) {
manufacturer = getValue(classObject, (LPCWSTR)L"Manufacturer");
model = getValue(classObject, (LPCWSTR)L"Model");
}
classObject->Release();
}
classObjectEnumerator->Release();
}
}
}
if (locator) {
locator->Release();
}
if (services) {
services->Release();
}
CoUninitialize();
return { manufacturer, model };
}
这些信息可以通过WMI Windows Machine Instrumentation获得,但这远不是一个简单的库函数。因此我宁愿自己解析注册表项并查找相关项。可能是的,但我对WMI API知之甚少。也许这会让我惊讶,太简单了……我已经设法读取了注册表项的内容。我正在尝试使用我在上找到的信息来解析它。这似乎不太难。bvi目前也是我的朋友。我根据我引用的文章中的格式编写了一个小程序。我假设SMBiosData包含一系列条目,每个条目都采用该表格式。不幸的是,情况似乎并非如此:在数据的开头有字节0x00、0x02,这肯定是错误的,因为我希望第二个字节至少是0x04,因为它应该指示第一个条目的格式化部分的大小。请参阅格式描述以了解这意味着什么。我甚至不知道你在这个网站上,雷蒙德。请允许我卑躬屈膝一分钟——就这样。现在好多了。