2011-08-11 6 views
1

私の友人と私はレジストリを変更するためにこのコードを書いていますが、実行するとレジストリに値を書き込むのではなく、正しく設定されているかどうかを確認するだけです。 プログラムの前に正しく設定されている場合は、ポップアップが表示されません。 レジストリキーが一致していない場合、プログラムは正しいレジストリ値を書き込むはずですが、現時点では機能しません。理由はありません。レジストリへのC++の書き込みは私のコードでは機能しませんか?

ここに完全なコードです:

if (version.dwMajorVersion >= 6) 
{ 
    HKEY ErrMode; 
    LONG res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\ControlSet001\\Control\\Windows", 0, KEY_READ | KEY_WRITE, &ErrMode); 
    if (res == ERROR_SUCCESS) 
    { 
     DWORD ErrorMode; 
     DWORD DRlen = sizeof(DWORD); 
     DWORD SUlen = sizeof(DWORD); 
     if (RegQueryValueEx(ErrMode, "ErrorMode", 0, NULL, (LPBYTE)&ErrorMode, &DRlen) == ERROR_SUCCESS) 
     { 
      if (ErrorMode != 2) // any of errormode is non-2 
      { 
       if (MessageBox(NULL, "Windows Error Reporting is still turned on.\r\nThis program may not work correctly with reporting turned on. Shall I disable it?\r\nNote:The readme explains how you can disable and enable Error Reporting under Windows)","Windows Error Reporting:", 
        MB_YESNO | MB_ICONQUESTION | MB_APPLMODAL) == IDYES) 
       { 
        ErrorMode = 2; // change local 
        DRlen = sizeof(DWORD); 
        SUlen = sizeof(DWORD); 
        if (RegSetValueEx(ErrMode,"ErrorMode",2, REG_DWORD, (LPBYTE)&ErrorMode, DRlen) != ERROR_SUCCESS) 
        { 
         char* buff = new char[1024]; 
         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,res,0,buff,1024,0); 
         MessageBox(NULL, buff,"Error...", MB_OK | MB_ICONEXCLAMATION); 
         delete[] buff; 
         ok = false; 
        } 
       } 
       else 
        ok = false; 
      }   
     } 
     if (RegCloseKey(ErrMode) != ERROR_SUCCESS) 
      MessageBox(NULL, "Could not close Registry Key handle.","Error...",MB_OK | MB_ICONEXCLAMATION); 
    } 
    else 
    { 
     char* buff = new char[1024]; 
     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,res,0,buff,1024,0); 
     CString message; 
     message.Format("Could not read from registry. Make sure you have either turned off error reporting\r\nor that you run this program from a privileged account.\r\nError: %s",buff); 
     MessageBox(NULL, message.GetBuffer(),"Error...",MB_OK | MB_ICONEXCLAMATION); 
     delete[] buff; 
     ok = false; 
    } 
    HKEY DontSUI; 
    res = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\Windows Error Reporting", 0, KEY_READ | KEY_WRITE, &DontSUI); 
    if (res == ERROR_SUCCESS) 
    { 
     DWORD DontShowUI; 
     DWORD DRlen = sizeof(DWORD); 
     DWORD SUlen = sizeof(DWORD); 
     if (RegQueryValueEx(DontSUI, "DontShowUI", 0, NULL, (LPBYTE)&DontShowUI, &DRlen) == ERROR_SUCCESS) 
     { 
      if (DontShowUI != 1) // any of DontShowUI is non 1 
      { 
       if (MessageBox(NULL, "Windows Error Reporting is still turned on.\r\nThis program may not work correctly with reporting turned on. Shall I disable it?\r\nNote:The readme explains how you can disable and enable Error Reporting under Windows)","Windows Error Reporting:", 
        MB_YESNO | MB_ICONQUESTION | MB_APPLMODAL) == IDYES) 
       { 
        DontShowUI = 1; // change local 
        DRlen = sizeof(DWORD); 
        SUlen = sizeof(DWORD); 
        if (RegSetValueEx(DontSUI,"DontShowUI",1, REG_DWORD, (LPBYTE)&DontShowUI, DRlen) != ERROR_SUCCESS) 
        { 
         char* buff = new char[1024]; 
         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,res,0,buff,1024,0); 
         MessageBox(NULL, buff,"Error...", MB_OK | MB_ICONEXCLAMATION); 
         delete[] buff; 
         ok = false; 
        } 
       } 
       else 
        ok = false; 
      }   
     } 
     if (RegCloseKey(DontSUI) != ERROR_SUCCESS) 
      MessageBox(NULL, "Could not close Registry Key handle.","Error...",MB_OK | MB_ICONEXCLAMATION); 
    } 
    else 
    { 
     char* buff = new char[1024]; 
     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,res,0,buff,1024,0); 
     CString message; 
     message.Format("Could not read from registry. Make sure you have either turned off error reporting\r\nor that you run this program from a privileged account.\r\nError: %s",buff); 
     MessageBox(NULL, message.GetBuffer(),"Error...",MB_OK | MB_ICONEXCLAMATION); 
     delete[] buff; 
     ok = false; 
    } 
    if (!ok) 
    { 
     MessageBox(NULL,"Windows Error Reporting may still be active.\r\nThis program may not function correctly.","Warning",MB_ICONWARNING |MB_OK); 
    } 
} 
+0

あなたが持っていると思う問題を私たちに指摘する短いスニペットを投稿できますか? –

+0

これをやめます: 'char * buff = new char [1024];'関数の先頭に1つのバッファを割り当てます。 'char buffer [1024];'次に、割り当てを解除する必要はありません。 –

+1

はコードの最初の30行にあります。これはレジストリから読み込んでレジストリに書き込む部分です。何かが失敗した場合、ユーザーにメッセージボックスが表示されます。 その後、DontShowUIレジストリキーで同じコードが再利用されています。 – gijs007

答えて

1

2、あなたのコード内で使用していたのではなく、instructed in MSDNとして、0からRegSetValueEx()にお電話でReservedパラメータを設定してみてください。

+0

これはうまくいきました:)ありがとうございます:) Windows VistaとServer 2008では、このエラーが表示されます。api-ms-win-core-localregistry-l1-1-0.dll ヘッダーがありません。 これは私のヘッダのリストです: の#include "stdafx.hを" の#include "ServerCheckerGUI.h" の#include "Control.h" の#include "ServerDoc.h" の#include gijs007

1

HKEY_LOCAL_MACHINEはシステムの位置です。 「通常の」ユーザーとしては、そこに書き込む権限がありません。管理者権限が必要です。

+0

UACがオフになっていて、adminとの互換モードでも実行しようとしましたが、それは役に立ちませんでした。 – gijs007

+0

管理者の問題のため、書き込みがHKLM以外の場所に仮想化されている可能性があります。私は、互換モードが本当にアプリケーションでadminとして実行されているのか、それとも単にシミュレートするのかは分かりません。 –

関連する問題