我想使用WmiMonitorBrightness:CurrentBrightness的属性来获取亮度值。
在Win10中,使用C#& C ++(通过MFC)程序可以获得系统亮度。 我在相同的计算机上将操作系统更改为Win7,C#程序可以获得正确的亮度;但是,C ++程序无法获得正确的值。相反,它显示乱码(没有例外)。
有没有人遇到类似的情况? 希望有人能给我一些建议。提前谢谢!
C#ref:How to query GetMonitorBrightness from C#
我的C ++代码(只需修改https://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx中的第4步,第6步和第7步,以符合我的需要):
步骤4:
//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();