【问题标题】:WmiMonitorBrightness don't work on Win7 by C++WmiMonitorBrightness 在 C++ 的 Win7 上不起作用
【发布时间】:2017-04-05 18:12:12
【问题描述】:

我想使用WmiMonitorBrightness的属性:CurrentBrightness来获取亮度值。

在Win10中,使用C# & C++(by MFC)程序可以获得系统亮度。 我在同一台电脑上将操作系统更改为Win7,C#程序可以得到正确的亮度;但是,C++ 程序无法获得正确的值。而是显示乱码(不出现异常)。

有没有人遇到过类似的情况? 希望有人能给我一些建议。提前致谢!

C# 参考:How to query GetMonitorBrightness from C#

我的 C++ 代码(只需修改 https://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx 中的第 4、6 和 7 步以符合我的需要):

第四步:

//Connect to WMI through the IWbemLocator::ConnectServer method

IWbemServices *pSvc = NULL;

hres = pLoc->ConnectServer(
    _bstr_t(L"ROOT\\WMI"),   // Object path of WMI namespace
    NULL,                    // User name. NULL = current user
    NULL,                    // User password. NULL = current
    0,                       // Locale. NULL indicates current
    NULL,                    // Security flags.
    0,                       // Authority (for example, Kerberos)
    0,                       // Context object 
    &pSvc                    // pointer to IWbemServices proxy
    );

if (FAILED(hres))
{
    cout << "Could not connect. Error code = 0x"
            << hex << hres << endl;
    pLoc->Release();
    CoUninitialize();
    return 1;                // Program has failed.
}

cout << "Connected to ROOT\\WMI namespace" << endl;

第 6 步:

// Use the IWbemServices pointer to make requests of WMI

// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT * FROM WmiMonitorBrightness"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator);

if (FAILED(hres))
{
    cout << "Query for operating system name failed."
        << " Error code = 0x"
        << hex << hres << endl;
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();
    return 1;               // Program has failed.
}

第 7 步:

// Get the data from the query in step 6

IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;

while (pEnumerator)
{
    HRESULT hr = pEnumerator->Next(
        WBEM_INFINITE,  //lTimeOut [in]
        1,              //Number of requested objects.
        &pclsObj,       //Pointer to enough storage to hold the number of IWbemClassObject 
                                //interface pointers specified by uCount. 
        &uReturn        //Pointer to a ULONG that receives the number of objects returned.
                                //This number can be less than the number requested in uCount.
        );

    if (0 == uReturn)
    {
        break;
    }

    VARIANT vtProp;

    // Get the value of the Name property
    hr = pclsObj->Get(L"CurrentBrightness", 0, &vtProp, 0, 0);
    wcout << " CurrentBrightness : " << vtProp.intVal << endl;

    pclsObj->Release();
}

// Cleanup
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();

【问题讨论】:

  • wcout &lt;&lt; " CurrentBrightness : " - 这将输出常量字符串文字(不是字符串文字)的 address。这是故意的吗?这就是你所说的“显示乱码”
  • 感谢您的回复!我添加以下代码来获取它的值:int Value = vtProp.intVal;cout &lt;&lt; "CurrentBrightness Value : " &lt;&lt; Value &lt;&lt; endl; 但它也显示乱码的 smae 作为 wcout。但是,它在 Win10 上显示正确的亮度值。但也许我错了,这不是获得真正价值的好方法。你能告诉我你获得真正价值的方式吗?非常感谢!
  • pclsObj-&gt;Get 有一个返回值,你可以忽略它。首先不要忽略返回值。

标签: c++ windows-7 mfc wmi


【解决方案1】:

我已使用此 WMI 查询使用 Visual C++ 2017 查询亮度,目标是 Windows 8.1 64 位,它正确检索 CurrentBrightness 属性。

在 Windows 7 上

我相信 Windows Vista 和 Windows 7 可能不支持这些 WMI 调用;除非,您使用可以链接到现代 Win32 API(适用于 Windows 8 及更高版本)的现代 VStudio 版本构建代码,然后可能在 Windows 7 机器上运行它,这些 Win32 API 在 Visual Studio 中静态链接。

或者,可以使用以下 Win32 API 调用获取 MOnitors 的亮度(不确定它是否适用于 LCD 屏幕):GetMonitorBrightness()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    相关资源
    最近更新 更多