Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 223
Giorgio Avatar asked Sep 06 '12 12:09

Giorgio


People also ask

How to find computer make and model from CMD?

Find computer make and model from CMD. Just run the command given below to get computer model. wmic csproduct get vendor, version. There’s another command which can be used to get the computer system model information.

How do I find the manufacturer of my computer?

In the Registry Editor, use the left sidebar to navigate to the following key: If your PC already has manufacturer information, you’ll see several string values in the OEMInformation key. If your PC doesn’t have these values, you’ll need to add them.

How do I find the model of my computer Windows 10?

In Windows 10 and 11, go to Settings > System > About and check your PC’s name for its model information. A faster way to access the system information page is to right-click the Start button and select System. You’ll find your computer’s model on its packaging.

How to get the computer system model and architecture information?

There’s another command which can be used to get the computer system model information. We can also get architecture (32bit/64bit) using this command. wmic computersystem get model,name,manufacturer,systemtype Based on the information you require, you can use any of the above commands.


2 Answers

The steps you need are explained on Creating a WMI Application Using C++. MSDN even includes a sample program. You just need to change two strings.

  1. Change SELECT * FROM Win32_Process to SELECT * FROM Win32_ComputerSystem
  2. Change Name to Manufacturer and then again for Model.
like image 61
Raymond Chen Avatar answered Nov 07 '22 07:11

Raymond Chen


With the help of the Microsoft example code, I was able to create this method.

#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 };
}
like image 38
Daniel Ryan Avatar answered Nov 07 '22 09:11

Daniel Ryan