1

現在の部屋の明るさでモニターの明るさを調整する小さなプログラムをセットアップしようとしています。

MSDN の指示に従って、これをセットアップしました。

cout << "Legen Sie das Fenster bitte auf den zu steuernden Monitor.\n";
system("PAUSE");
HMONITOR hMon = NULL;
char OldConsoleTitle[1024];
char NewConsoleTitle[1024];
GetConsoleTitle(OldConsoleTitle, 1024);
SetConsoleTitle("CMDWindow7355608");
Sleep(40);
HWND hWnd = FindWindow(NULL, "CMDWindow7355608");
SetConsoleTitle(OldConsoleTitle);
hMon = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);


DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(
    hMon,
    &cPhysicalMonitors
    );

if(bSuccess)
{
    pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
        cPhysicalMonitors* sizeof(PHYSICAL_MONITOR));

    if(pPhysicalMonitors!=NULL)
    {
        LPDWORD min = NULL, max = NULL, current = NULL;
        GetPhysicalMonitorsFromHMONITOR(hMon, cPhysicalMonitors, pPhysicalMonitors);

        HANDLE pmh = pPhysicalMonitors[0].hPhysicalMonitor;

        if(!GetMonitorBrightness(pmh, min, current, max))
        {
            cout << "Fehler: " << GetLastError() << endl;
            system("PAUSE");
            return 0;
        }

        //cout << "Minimum: " << min << endl << "Aktuell: " << current << endl << "Maximum: " << max << endl;

        system("PAUSE");
    }

}

しかし、問題: GetMonitorBrightness() を使用しようとするたびに、プログラムがクラッシュしAccess Violation while writing at Position 0x00000000ます (このエラーをドイツ語から翻訳しました) 。

デバッグしようとしているときに、pPhysicalMonitors には実際に使用したいモニターが含まれていることがわかりましたが、含まれているのpPhysicalMonitors[0].hPhysicalMonitorは 0x0000000 のみです。これは問題の一部でしょうか?

4

1 に答える 1

2

GetMonitorBrightness() を使用しようとするたびに、位置 0x00000000 に書き込み中にアクセス違反でプログラムがクラッシュします (このエラーをドイツ語から翻訳しました)。

に NULL ポインタを渡してGetMonitorBrightness()いるため、出力値を無効なメモリに書き込もうとするとクラッシュします。

と同様GetNumberOfPhysicalMonitorsFromHMONITOR()に、GetMonitorBrightness()実際の変数のアドレスを渡す必要があります。たとえば、次のようになります。

DWORD min, max, current;
if (!GetMonitorBrightness(pmh, &min, &current, &max))

デバッグしようとしているときに、pPhysicalMonitors には実際に使用したいモニターが含まれていることがわかりましたが、pPhysicalMonitors[0].hPhysicalMonitor には 0x0000000 しか含まれていません。これは問題の一部でしょうか?

いいえ。ただし、それが > 0 であることを確認するためにチェックしていません。実際に配列にデータが入力されていることを確認するためcPhysicalMonitorsに、の戻り値を無視しています。GetPhysicalMonitorsFromHMONITOR()PHYSICAL_MONITOR

于 2016-03-18T19:55:08.290 に答える