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();