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