gpt4 book ai didi

c++ - 使用 CreateFile 打开显示时的正确路径是什么?

转载 作者:搜寻专家 更新时间:2023-10-31 01:32:52 24 4
gpt4 key购买 nike

最简单的例子:

#include <Windows.h>
#include <ntddvdeo.h>
#include <string>


std::string GetLastErrorAsString()
{
LPSTR msgBuffer = nullptr;
DWORD errorMessageID = ::GetLastError();
std::string msg("No Error");

if(errorMessageID != 0)
{
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errorMessageID,
MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),
(LPSTR)&msgBuffer,
0,
NULL);
msg = std::string(msgBuffer, size);

//Free buffer
LocalFree(msgBuffer);
}
return msg;
}

int APIENTRY wWinMain( _In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
DWORD nOutBufferSize = 0;
DWORD result = NULL;

typedef struct _DISPLAY_BRIGHTNESS {
UCHAR ucDisplayPolicy;
UCHAR ucACBrightness;
UCHAR ucDCBrightness;
} DISPLAY_BRIGHTNESS, *PDISPLAY_BRIGHTNESS;

DISPLAY_BRIGHTNESS displayBrightness;

displayBrightness.ucDisplayPolicy = 0;
displayBrightness.ucACBrightness = 50;
displayBrightness.ucDCBrightness = 0;
nOutBufferSize = sizeof(displayBrightness);

HANDLE h = CreateFile( L"\\\\.\\DISPLAY2",
GENERIC_READ | GENERIC_WRITE,
0, NULL,
OPEN_EXISTING,
0, NULL);

if (h == INVALID_HANDLE_VALUE) {
MessageBoxA(NULL, GetLastErrorAsString().c_str(), "Test",
MB_OK | MB_ICONERROR);
return false;
}
return true;
}

我总是在 CreateFile 之后收到“访问被拒绝”,即使以管理员身份运行 Visual Studio 也是如此。所以问题是,如果 \\.\DISPLAY2 未被识别,Windows 还接受什么作为设备名称/路径?但是,这是我从 EnumDisplayMonitors 获得的确切设备名称:

Found Monitor with 1920x1080 called \\.\DISPLAY1 located at 0:00
Found Monitor with 1680x1050 called \\.\DISPLAY2 located at -1680:240

最佳答案

如果您想要物理监视器的句柄,请获取 HMONITOR来自 EnumDisplayMonitors 的回调并将其用作 GetPhysicalMonitorsFromHMONITOR 的参数.

这会给你 HANDLEPHYSICAL_MONITOR目的。例如,您可以使用该句柄调用 these functions

但是,如前所述 - 也许您想要获得的是设备上下文的句柄?然后使用 CreateDC , CreateFile不会在这里工作。


编辑:

如果要更改亮度,请尝试第一种方法 - 获取物理显示器 HANDLE并使用 SetMonitorBrightness

编辑 2:

好的,这段代码对我有用,实际上是从 EnumDisplayMonitors 返回的设备路径对我也不起作用。我不是这段代码的作者,我找到了它 here (自己想不通)

DISPLAY_DEVICE ddAdapter;
ddAdapter.cb = sizeof(DISPLAY_DEVICE);
for (DWORD dwAdapterNum = 0; EnumDisplayDevices(NULL, dwAdapterNum, &ddAdapter, 0); dwAdapterNum++)
{
if ((ddAdapter.StateFlags & (DISPLAY_DEVICE_ATTACHED_TO_DESKTOP | DISPLAY_DEVICE_MIRRORING_DRIVER)) == DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
{
DISPLAY_DEVICE ddMonitor;
ddMonitor.cb = sizeof(DISPLAY_DEVICE);
for (DWORD dwMonitorNum = 0; EnumDisplayDevices(ddAdapter.DeviceName, dwMonitorNum, &ddMonitor, EDD_GET_DEVICE_INTERFACE_NAME); dwMonitorNum++)
{
if (ddMonitor.StateFlags & DISPLAY_DEVICE_ACTIVE)
{
HANDLE hMonitor = CreateFile(ddMonitor.DeviceID, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);

// ...

CloseHandle(hMonitor);
}
}
}
}

关于c++ - 使用 CreateFile 打开显示时的正确路径是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42350636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com