2017-11-14 5 views
-2

初めてを開くことができないので、私はホームページのREGキーを変更するための小さなプログラムを書くためにしようとしているは、なぜ私が質問をして、私のレジストリキー

...私は右のそれをやっている願っています私はそれを実行するたびに、キーの場所が開いていないというエラーが表示されます。なぜどんなアイデア? (管理者で実行してみました)

//this string array will be the value for the new home page (w/ null termination) 
char newHomePage[] = "https://www.youtube.com/watch?v=gwJ_LgYYvpU \0"; 
HKEY homePageKey = NULL; //handle for the key once opened 


//Open reg key we wish to change, if this fails then abort 

//reg key for home page 
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"\\SOFTWARE\\Microsoft\\Internet Explorer\\Main", 0, KEY_SET_VALUE, &homePageKey) == ERROR_SUCCESS) 
{ 
    printf("Key location open successful \n"); 
    if (RegSetValueExW(homePageKey, L"Start Page", 0, REG_SZ, (LPBYTE)&newHomePage, sizeof(char)) == ERROR_SUCCESS) 
    { 
     printf("Key changed in registry \n"); 
    } 
    else 
    { 
     printf("Key not changed in registry \n"); 
     printf("Error %u ", (unsigned int)GetLastError()); 
    } 
    RegCloseKey(homePageKey); 
} 
else 
{ 
    printf("Error: %u \n", (unsigned int)GetLastError()); 
    printf("Key location open UNsuccessful \n"); 
    system("pause"); 
    RegCloseKey(homePageKey); 
    return 0; 
} 

return 0; 
+0

を私はあなたが 'RegOpenKeyExW()'の戻り値を調べることができるように2行にあれば最初の文を破る示唆しています。これはあなたに問題の手がかりを与えるかもしれません。 –

+1

なぜGetLastErrorを呼びますか?ドキュメンテーションはそれについて言及していない。それは言う:*関数が失敗した場合、戻り値はWinerror.hで定義された非ゼロエラーコードです*。 –

+2

[RegOpenKeyExがHKEY \ _LOCAL \ _MACHINEで失敗する可能性があります]の複製(https://stackoverflow.com/questions/820846/regopenkeyex-fails-on-hkey-local-machine) –

答えて

0

あなたのコードにはいくつかの問題があります。

  1. RegCreateKeyEx()またはRegOpenKeyEx()にサブキーを指定するときに先頭のスラッシュが含まれていないが。

  2. URLをANSI文字列として、Unicode文字列が必要な関数に渡しています。そして、間違ったデータサイズを指定しています。

  3. GetLastError()からエラーコードが表示されますが、レジストリAPIはエラーを報告するのにSetLastError()を使用しません。代わりにエラーコードが関数の戻り値で返されます。

代わりにこれを試してみてください:

//this string array will be the value for the new home page (w/ null termination) 
const wchar_t newHomePage[] = L"https://www.youtube.com/watch?v=gwJ_LgYYvpU\0"; 
HKEY homePageKey = NULL; //handle for the key once opened 

//Open reg key we wish to change, if this fails then abort 
//reg key for home page 
LONG lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Internet Explorer\\Main", 0, KEY_SET_VALUE, &homePageKey); 
if (lResult == ERROR_SUCCESS) 
{ 
    printf("Key location open successful \n"); 
    lResult = RegSetValueExW(homePageKey, L"Start Page", 0, REG_SZ, (LPBYTE)newHomePage, sizeof(newHomePage)); // or (lstrlenW(newHomePage)+1)*sizeof(wchar_t) 
    if (lResult == ERROR_SUCCESS) 
    { 
     printf("Key changed in registry \n"); 
    } 
    else 
    { 
     printf("Key not changed in registry \n"); 
     printf("Error %ld \n", lResult); 
    } 
    RegCloseKey(homePageKey); 
} 
else 
{ 
    printf("Key location open UNsuccessful \n"); 
    printf("Error: %ld \n", lResult); 
} 
system("pause"); 
return 0; 
関連する問題