0

I am currently trying to delete a subkey from "Software\Microsoft\Windows\CurrentVersion\Run". The problem is dispite having tried every possible solution out there my error code is unique and the only question about it hasn't been answered: how to remove program from startup list using c++ . I am using Visual Studio on Windows x64 10 the programm is a Win32 application.

Created key with:

   BOOL registerForLocalStartup(PCWSTR regName, PCWSTR pathToExe, PCWSTR args)
{
    HKEY hKey = NULL;
    LONG lResult = 0;
    BOOL fSuccess = TRUE;
    DWORD dwSize;

    const size_t count = MAX_PATH * 2;
    wchar_t szValue[count] = {};


    wcscpy_s(szValue, count, L"\"");
    wcscat_s(szValue, count, pathToExe);
    wcscat_s(szValue, count, L"\" ");

    if (args != NULL)
    {
        // caller should make sure "args" is quoted if any single argument has a space
        // e.g. (L"-name \"Mark Voidale\"");
        wcscat_s(szValue, count, args);
    }

    // For admin HKEY_LOCAL_MACHINE
    lResult = RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, (KEY_WRITE | KEY_READ), NULL, &hKey, NULL);

    fSuccess = (lResult == 0);

    if (fSuccess)
    {
        dwSize = (wcslen(szValue) + 1) * 2;
        lResult = RegSetValueExW(hKey, regName, 0, REG_SZ, (BYTE*)szValue, dwSize);
        fSuccess = (lResult == 0);
    }

    if (hKey != NULL)
    {
        RegCloseKey(hKey);
        hKey = NULL;
    }

    return fSuccess;
}

Here my code:

bool DeleteValueKey(HKEY hKeyRoot, std::wstring Subkey, std::wstring ValueKey)
{
    HKEY    hKey = NULL;
    bool    bReturn = false;

    long result = RegOpenKeyEx(hKeyRoot, Subkey.c_str(), 0, KEY_READ | KEY_WRITE | KEY_WOW64_32KEY, &hKey);

    wcout << "Result of RegOpenKeyEx: " << result << endl;

    if (result == ERROR_SUCCESS)
    {
        long result2 = RegDeleteKeyEx(hKey, ValueKey.c_str(), KEY_WOW64_32KEY, 0);
        wcout << "Result of RegDeleteKeyEx: " << result2 << endl;
        if (result2 == ERROR_SUCCESS)
        {
            bReturn = true;
        }
    }

    if (hKey != NULL) { RegCloseKey(hKey); }

    return bReturn;
}

And this is what I tried to call:

bool result = DeleteValueKey(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", L"test1");

            if (result)
            {
                wcout << "SUCCESS" << endl;
            }
            else
            {
                wcout << "FAILURE: "<< GetLastError() << endl;
            }*/

OUTPUT:

Result of RegOpenKeyEx: 0 
Result of RegDeleteKeyEx: 2 
FAILURE: 0

Has someone an idea ? I am going crazy not beeing able to fix such a blatant issue...

Community
  • 1
  • 1
Qubasa
  • 183
  • 8
  • Why do you not examine the return value of the registry functions to see what the actual error is - your code discards this important information. –  Feb 20 '17 at 17:43
  • Is your program 32 bit or 64 bit? Is your Windows 32 bit or 64 bit? What error code are you getting? What does `::GetLastError()` return? – Richard Critten Feb 20 '17 at 17:44
  • I use Windows x64 and my application is x32. The error code I am getting is 0. – Qubasa Feb 20 '17 at 17:46
  • _"Result of RegDeleteKeyEx: 2"_ 2 is Not Found Also you only pass KEY_WOW64_32KEY into delete. Therefore Open and Delete are seeing different views of the registry – Richard Critten Feb 20 '17 at 17:57
  • I am checking with autoruns the key is there! – Qubasa Feb 20 '17 at 17:59
  • I already tried adding | KEY_WOW64_32KEY to Open but it didn't help the error messages are the same. – Qubasa Feb 20 '17 at 18:03

1 Answers1

1

To delete your value in the Run key you should use RegDeleteKeyValue (or RegDeleteValue if you support WinXP and older).

RegDeleteKeyEx is used to delete the whole key (and all its values) and you don't want to do that here because you don't own the Run key.

See this blog post for the terminology used to describe the various parts of the registry.

Anders
  • 97,548
  • 12
  • 110
  • 164