-2

First time asking a question so hope I'm doing it right...

I'm trying to write a small program to change the reg key for the home page of internet explorer but each time I run it I'm getting the error that the key location open was unsuccessful. Any ideas why? (Tried running in admin)

//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;
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Jazz R
  • 3
  • 4
  • I suggest breaking the first if statement into two lines so that you can inspect the return value of `RegOpenKeyExW()`. This might give you a clue as to the issue. – Code-Apprentice Nov 14 '17 at 14:35
  • 1
    Why are you calling `GetLastError`? The documentation makes no mention of that. It says: *If the function fails, the return value is a nonzero error code defined in Winerror.h.*. – David Heffernan Nov 14 '17 at 14:41
  • 2
    Possible duplicate of [RegOpenKeyEx fails on HKEY\_LOCAL\_MACHINE](https://stackoverflow.com/questions/820846/regopenkeyex-fails-on-hkey-local-machine) – Raymond Chen Nov 14 '17 at 14:41
  • 1
    Do some debugging. Print the values of the variables. – klutt Nov 14 '17 at 14:41
  • I guess it's because the key name starts with a backslash – AcidJunkie Nov 14 '17 at 14:41
  • As a side note, if I were to open the URL (which I did not) of your example, would I get the impression that you try to promote something? Not to say spam. – Yunnosch Nov 14 '17 at 16:41
  • @Yunnosch Sorry, the URL was just some dummy data used for posting the question online (it was just an open song that I had playing on youtube) – Jazz R Nov 21 '17 at 13:06

1 Answers1

0

There are several problems with your code:

  1. Do NOT include a leading slash when specifying a subkey to RegCreateKeyEx() or RegOpenKeyEx().

  2. You are passing your URL as an ANSI string to a function that expects a Unicode string. And you are specifying the wrong data size.

  3. You are getting error codes from GetLastError(), but the Registry API doesn't use SetLastError() to report errors. Error codes are returned in function return values instead.

Try this instead:

//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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770