1

I want to use the property of WmiMonitorBrightness: CurrentBrightness to get the brightness value.

In Win10, using the C# & C++(by MFC) program can get the system brightness. I changed the OS to Win7 on the SAME COMPUTER, the C# program could get the correct brightness; However, the C++ program CANNOT get the correct value. Instead, it shows the garbled(no exception occurs).

Do anyone meet the similar circumstance? Hope someone could give me some suggestion. Thanks in advance!

C# ref: How to query GetMonitorBrightness from C#

my C++ code (just revise the Step 4, 6 &7 from https://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx to match what I needs):

Step4:

//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;

Step 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.
}

Step 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();
Community
  • 1
  • 1
Hughes
  • 121
  • 1
  • 11
  • `wcout << " CurrentBrightness : "` - This will output the **address** of the constant string literal (not the string literal). Is this intentional? Is this what you called *"it shows the garbled"*? – IInspectable Nov 22 '16 at 09:40
  • Thanks for your reply! I add the following code to get its value: `int Value = vtProp.intVal;cout << "CurrentBrightness Value : " << Value << endl;` But it also shows garbled the smae as the **wcout**. However, it shows the correct brightness value on Win10. But maybe I wrong, it's not a good way to get the real value. Would you please tell me the way in which you get the real value? Thanks a lot! – Hughes Nov 23 '16 at 07:39
  • `pclsObj->Get` has a return value, that you ignore. Start by not ignoring return values. – IInspectable Nov 23 '16 at 10:23

1 Answers1

0

I have used this WMI Query for Querying the Brightness using Visual C++ 2017, targeting Windows 8.1 64-bit, and it retrieves the CurrentBrightness property, correctly.

On Windows 7

I believe Windows Vista and Windows 7 may not support these WMI calls; UNLESS, you build the code using a modern VStudio version that can link against modern Win32 APIs(for Windows 8 and above), and then possibly run it on a Windows 7 machine, with those Win32 APIs static linked within Visual Studio.

Alternatively, one may be able to get Brightness of MOnitors(not sure if it works on LCD screens), using this Win32 API call: GetMonitorBrightness()

Cosmic OM...
  • 181
  • 4
  • 14