0

I am trying to write to my Windows 10 registry, to include a program in Startup.

At the moment I have written the following code, which is not wroking:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winnt.h>
#include <winreg.h>
    
int main()
{
    const TCHAR* path = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
    LPDWORD holdResult;
    PHKEY hKey;
    int lResult;
    //lResult = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_READ, &hKey);
    lResult = RegCreateKeyExA(HKEY_CURRENT_USER, "myprogram", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &holdResult);
    
    if (lResult != ERROR_SUCCESS)
    {
        if (lResult == ERROR_FILE_NOT_FOUND) {
            printf("Key not found.\n");
            return TRUE;
        }
        else {
            printf("Error opening key.\n");
            return FALSE;
        }
    }
    printf("Key already existed or not: %p\n", holdResult);
    
    char* szPath = "C:\\Users\\Myname\\Documents\\coolprogram.exe";
    lResult = RegSetValueExA(hKey, "program", 0, REG_SZ,(LPBYTE)szPath, sizeof(wchar_t)*(wcslen(szPath)+1));
    printf("Key successfully set or not: %d\n", lResult);
    RegCloseKey(hKey);
    return 0;
}

The strange thing is, although the code does not write anything to the registry, I get no error message, and the program executes as successful!

This is what gets printed to the terminal when I run the code:

Key already existed or not: 0000000000000002
Key successfully set or not: 0

So, the key had already been previously created, and was succesfully set, which did not happen, there is nothing in my registry.

I believe this is a permissions problem, because for some reason I cannot alter my registry permissions manually, even after setting myself as owner, it is impossible for me to allow "Full Control" to myself.

But I expected some sort of error, such as "ACCESS DENIED: INSUFFICIENT PERMISSIONS", but I am getting a success. Strange.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

1

You are passing the wrong key path to RegCreateKeyEx(). You are trying to write your program value to HKCU\myprogram rather than to HKCU\SOFTWARE\Microsoft\...\Run.

You are also passing invalid pointers to RegCreateKeyEx(). Its last 2 parameters expect pointers to HKEY and DWORD variables, but you are passing in pointers to HKEY* and DWORD* variables instead.

You are also passing an incorrect parameter to RegSetValueExA(), too. You are giving it an ANSI string (OK, since it is an ANSI function), but are passing in a Unicode string length that is 2x the byte size of the actual ANSI string (bad).

In fact, your code shouldn't even compile as shown.

Try this instead:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winnt.h>
#include <winreg.h>

int main()
{
    const char* szKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    const char* szPath = "C:\\Users\\Myname\\Documents\\coolprogram.exe";
    DWORD dwDisposition;
    HKEY hKey;
    LSTATUS lResult;

    //lResult = RegOpenKeyExA(HKEY_CURRENT_USER, szKey, 0, KEY_QUERY_VALUE, &hKey);
    lResult = RegCreateKeyExA(HKEY_CURRENT_USER, szKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKey, &dwDisposition);

    if (lResult != ERROR_SUCCESS)
    {
        printf("Error creating key: %ld\n", lResult);
        return 0;
    }

    printf("Key already existed or not: %lu\n", dwDisposition);

    lResult = RegSetValueExA(hKey, "program", 0, REG_SZ, (LPBYTE)szPath, sizeof(TCHAR)*(strlen(szPath)+1));
    printf("Key successfully set or not: %ld\n", lResult);
    RegCloseKey(hKey);

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770