私はプログラムでモニターの明るさを調整しようとしています。少しの研究の後、私はこのlinkを考え出し、次のコードを書いていました。この行を実行する際にGetMonitorCapabilitiesとGetMonitorBrightness関数の使用方法
#include "Windows.h"
#include "WinUser.h"
#include "PhysicalMonitorEnumerationAPI.h"
#include "HighLevelMonitorConfigurationAPI.h"
#include <strsafe.h>
void ShowError(LPTSTR lpszFunction);
int main()
{
HMONITOR hMonitor = NULL;
DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
HWND hWnd = GetDesktopWindow();
// Get the monitor handle.
hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
// Get the number of physical monitors.
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);
if (bSuccess)
{
// Allocate the array of PHYSICAL_MONITOR structures.
pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors* sizeof(PHYSICAL_MONITOR));
if (pPhysicalMonitors != NULL)
{
// Get the array.
bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);
// Get physical monitor handle.
HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;
LPDWORD pdwMinimumBrightness = NULL;
LPDWORD pdwCurrentBrightness = NULL;
LPDWORD pdwMaximumBrightness = NULL;
bSuccess = GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);
if (bSuccess == FALSE)
{
ShowError(TEXT("GetMonitorBrightness"));
}
// Close the monitor handles.
bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);
// Free the array.
free(pPhysicalMonitors);
}
}
return 0;
}
void ShowError(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf)/sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
このコードがクラッシュ:
bSuccess = GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);
文書によると、その機能がサポートされない場合があります。
If this function is supported, the GetMonitorCapabilities function returns the MC_CAPS_BRIGHTNESS flag.
だから、それをチェックするために、私はちょうどGetMonitorBrightness
を呼び出す前に、私のコードに次のブロックを追加します。モニタがDDC/CIをサポートしていない場合
は再び、documentationによると、GetMonitorCapabilities
機能に障害が発生した:私はそのブロックを追加した後
LPDWORD pdwMonitorCapabilities = NULL;
LPDWORD pdwSupportedColorTemperatures = NULL;
bSuccess = GetMonitorCapabilities(hPhysicalMonitor, pdwMonitorCapabilities, pdwSupportedColorTemperatures);
if (bSuccess == FALSE)
{
ShowError(TEXT("GetMonitorCapabilities"));
}
残念ながら、私は次のエラーを受け取りました。
私のモニタがDDC/CIをサポートしているかどうかを確認して、それがわかった。さらに、私は手動でDDC/CIサポートをモニタ設定から無効にすると、以前のエラーメッセージが次のものに切り替わるので、今私はかなり確信しています私のモニタはDDC/CIサポートを持っています。
私は正しいすべてをやっているような気がしますが、どうやら私はしませんよ。要するに、GetMonitorCapabilities
関数は意味を持たないエラーメッセージで失敗し、GetMonitorBrightness
関数がクラッシュします。
注:
私のモニターはDell U2713Hです。私は、私は、Microsoft Visual C++コンパイラー12.0(x86のに)GetMonitorBrightness()
とGetMonitorCapabilities()
へ
うわー、新人のミス。ありがとう。ドキュメンテーションは私を混乱させた「LPDWORD」と言っていました。今は完璧に動作しています。 – guneykayim
ドキュメントでは、それらが '_Out_'パラメータであるとも言われているので、出力*を何かに書く必要があります*。代わりに '_Out_Opt_'でなければNULLは許されません。 [SAL Annotations](https://msdn.microsoft.com/library/ms235402.aspx)を参照してください。 –
はい、私の悪いです。私が言ったように、ルーキーミス。 – guneykayim