Windows VC ++获取机器型号名称

时间:2012-03-09 04:29:11

标签: windows visual-c++

任何人都可以告诉我如何获取Windows机器的型号名称。 我是Windows VC ++的新手。

例如,我在Windows上运行了IBM ThinkCenter M50。这里的型号名称是“Think Center M50”。我想使用一些API从系统中获取此信息。

先谢谢, Shashi Kiran G M

3 个答案:

答案 0 :(得分:5)

或者,您可以使用注册表项:

HKEY_LOCAL_MACHINE \ SYSTEM \ CURRENTCONTROLSET \控制\ SystemInformation

还:HKEY_LOCAL_MACHINE \ HARDWARE \ DESCRIPTION \ System \ BIOS(仅限Win7或更高版本)

SystemManufacturer和SystemProductName条目应该这样做。使用WMI进行保存,我出于性能原因不惜一切代价避免使用WMI。

答案 1 :(得分:1)

正如Ben建议的那样,你需要使用WMI。

您要查找的类是Win32_ComputerSystem,其中包含一个类型为字符串的只读Model属性,该属性返回制造商提供给计算机的产品名称。

我将留下编写C ++代码来进行WMI调用as an exercise for the reader

请注意Ben的警告:并非所有制造商都在BIOS中发布此信息。这很可能是IBM所做的,所以你的测试用例应该很好,但这不是一个普遍的假设,你有理由做出。应用程序不应该依赖包含特定值的此属性。

答案 2 :(得分:0)

Microsoft example code的帮助下,我能够创建此方法。

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