Mic*_*son 14 c++ windows winapi
我试图通过Windows API获取/设置显示器的亮度级别.我已经尝试过低级监视器配置功能和高级监视器配置功能,但它们似乎都在同一个地方出现故障.在这两种情况下,我都没有问题获得HMONITOR句柄并从HMONITOR获取物理监视器句柄,但是一旦我尝试查询DDC/CI功能,我就会收到错误说" An error occurred while transmitting data to the device on the I2C bus."
导致此错误的特定函数是高级函数的GetMonitorCapabilities和低级函数的GetCapabilitiesStringLength.它们都会导致同样的错误.
这让我相信可能我的显示器不支持DDC/CI,但我知道我的笔记本电脑的显示器亮度可以通过控制面板进行更改,因此必须通过软件以某种方式进行控制.我也可以成功地使用WMI类在PowerShell脚本中得到/上描述设置亮度此页面.我读过的大多数内容都表明大多数现代笔记本电脑屏幕都支持DDC/CI.
有没有办法找出导致此错误的原因或获取有关它的更多信息?我目前正在Windows 7的Visual Studio 2013中使用C++.如果我不能使用当前的方法,我可能在我的C++程序中使用WMI,但我想我会先问这里.
这是我目前的代码:
#include "stdafx.h"
#include <windows.h>
#include <highlevelmonitorconfigurationapi.h>
#include <lowlevelmonitorconfigurationapi.h>
#include <physicalmonitorenumerationapi.h>
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
DWORD minBrightness, curBrightness, maxBrightness;
HWND curWin = GetConsoleWindow();
if (curWin == NULL) {
std::cout << "Problem getting a handle to the window." << std::endl;
return 1;
}
// Call MonitorFromWindow to get the HMONITOR handle
HMONITOR curMon = MonitorFromWindow(curWin, MONITOR_DEFAULTTONULL);
if (curMon == NULL) {
std::cout << "Problem getting the display monitor" << std::endl;
return 1;
}
// Call GetNumberOfPhysicalMonitorsFromHMONITOR to get the needed array size
DWORD monitorCount;
if (!GetNumberOfPhysicalMonitorsFromHMONITOR(curMon, &monitorCount)) {
std::cout << "Problem getting the number of physical monitors" << std::endl;
return 1;
}
// Call GetPhysicalMonitorsFromHMONITOR to get a handle to the physical monitor
LPPHYSICAL_MONITOR physicalMonitors = (LPPHYSICAL_MONITOR)malloc(monitorCount*sizeof(PHYSICAL_MONITOR));
if (physicalMonitors == NULL) {
std::cout << "Unable to malloc the physical monitor array." << std::endl;
return 1;
}
if (!GetPhysicalMonitorsFromHMONITOR(curMon, monitorCount, physicalMonitors)) {
std::cout << "Problem getting the physical monitors." << std::endl;
return 1;
}
std::cout << "Num Monitors: " << monitorCount << std::endl; // This prints '1' as expected.
wprintf(L"%s\n", physicalMonitors[0].szPhysicalMonitorDescription); // This prints "Generic PnP Monitor" as expected
// Call GetMonitorCapabilities to find out which functions it supports
DWORD monCaps;
DWORD monColorTemps;
// The following function call fails with the error "An error occurred while transmitting data to the device on the I2C bus."
if (!GetMonitorCapabilities(physicalMonitors[0].hPhysicalMonitor, &monCaps, &monColorTemps)) {
std::cout << "Problem getting the monitor's capabilities." << std::endl;
DWORD errNum = GetLastError();
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
LPVOID buffer;
FormatMessage(flags, NULL, errNum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&buffer, 0, NULL);
wprintf(L"%s\n", buffer);
return 1;
}
// Same error when I use GetCapabilitiesStringLength(...) here.
// More code that is currently never reached...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:另外我应该注意它physicalMonitors[0].hPhysicalMonitor是0,即使监视器计数和文本描述有效并且GetPhysicalMonitorsFromHMONITOR函数返回成功.有关为什么会这样的想法吗?
这是一个"不稳定的硬件"问题,它所讨论的I2C总线是视频适配器和显示器之间的逻辑互连.主要用于即插即用.底层错误代码是0xC01E0582,STATUS_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA.它由视频微型端口驱动程序中的DxgkDdiI2CTransmitDataToDisplay()辅助函数生成.供应商的视频驱动程序工作是配置它,提供功能,使总线和实现IOCTL底层GetMonitorCapabilities().
很明显,你是这里的设备驱动程序,在你的C++程序中没有任何关于这个失败的事情.您可以通过从视频适配器制造商处查找驱动程序更新来随机旋转命运之轮.但显示器在这里出现故障的非零赔率.先试试另一个.
| 归档时间: |
|
| 查看次数: |
1226 次 |
| 最近记录: |