简体   繁体   English

如何从C ++中的Windows注册表中获取计算机制造商和型号?

[英]How to get computer manufacturer and model from windows registry in C++?

I am writing my own C++ code to read the computer model and manufacturer on a Windows computer by reading and parsing the registry key 我正在编写自己的C ++代码,通过读取和解析注册表项来读取Windows计算机上的计算机模型和制造商

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/services/mssmbios/Data/SMBiosData

Is there any library function in Windows / C++ / Visual Studio that allows me to get this information directly? Windows / C ++ / Visual Studio中是否有任何库函数可以直接获取此信息?

The steps you need are explained on Creating a WMI Application Using C++ . 使用C ++创建WMI应用程序中介绍了所需的步骤。 MSDN even includes a sample program . MSDN甚至包括一个示例程序 You just need to change two strings. 你只需要改变两个字符串。

  1. Change SELECT * FROM Win32_Process to SELECT * FROM Win32_ComputerSystem SELECT * FROM Win32_Process更改为SELECT * FROM Win32_ComputerSystem
  2. Change Name to Manufacturer and then again for Model . Name更改为Manufacturer ,然后再将Model更改为

With the help of the Microsoft example code , I was able to create this method. Microsoft示例代码的帮助下,我能够创建此方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM