2016-03-18 5 views
1

現在の部屋の明るさでモニターの明るさを調整するために小さなプログラムを設定しようとしています。アクセス違反でGetMonitorBrightness()がクラッシュする

私は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"); 
    } 

} 

しかし、問題:Access Violation while writing at Position 0x00000000と私はGetMonitorBrightnessを(使用しようと毎回)プログラムがクラッシュを

(私はドイツからこのエラーを翻訳しました)

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

答えて

2

位置0x00000000ので書きながら、私はGetMonitorBrightnessを(使用しようと毎回)アクセス違反でプログラムがクラッシュ(私はドイツからこのエラーを翻訳)

あなたはGetMonitorBrightness()にNULLポインタを渡しているので、それがクラッシュしますその出力値を無効なメモリに書き込もうとするときに発生します。ただ、GetNumberOfPhysicalMonitorsFromHMONITOR()よう

GetMonitorBrightness()は例えば、あなたが実際の変数のアドレスを渡すことを期待:

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

デバッグしようとしたときに、私はpPhysicalMonitorsは実際に[私が使用するモニタが含まれていますが、pPhysicalMonitorsことを見ました0] .hPhysicalMonitorには0x0000000のみが含まれます。これは問題の一部になる可能性がありますか?

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

+0

ありがとう、DWORDとアドレスで修正されました。 – DoktorMerlin

関連する問題