Search code examples
cregistry

Editing Registry using C Giving warnings and not editing registry keys


The following code should be changing these registry values, but it's giving warnings and even when the code actually runs, it won't set the values. It's not giving an error it's just not doing it. I've also confirmed the function and code are getting run. I fixed one warning by adding the sizeof() arguments, but the rest (see below the code) are still there and it still won't actually write to the registry. also when I try to edit a string value it works but not dword values.

//dependences for anyone who wants to try to fix it

#include <winreg.h>
#include <conio.h>
#include <stdlib.h> 
#include <string.h> 
#include <stdlib.h>
#include <unistd.h>  

void startBCPE() {
    //Sets registery values for BCPE
    HKEY key;
    RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\Setup", 0, KEY_ALL_ACCESS, &key);
    RegSetValueExA(key, "SystemSetupInProgress", 0, REG_DWORD, 1, sizeof(1)); //this line fails to edit the registry
    RegSetValueExA(key, "SetupType", 0, REG_DWORD, 2, sizeof(2)); //this line fails to edit the registry
    RegSetValueExA(key, "SetupPhase", 0, REG_DWORD, 3, sizeof(3)); //this line fails to edit the registry
    RegSetValueExA(key, "CmdLine", 0, REG_SZ, "cmd.exe", sizeof("cmd.exe")); //This line works however
    RegCloseKey(key);
    return;
}

And the warnings:

.\Fizz.c:17:64: warning: passing argument 5 of 'RegSetValueExA' makes pointer from integer without a cast [-Wint-conversion]
     RegSetValueExA(key, "SystemSetupInProgress", 0, REG_DWORD, 1, sizeof(1));
                                                                ^
In file included from c:\mingw\include\windows.h:52:0,
                 from .\Fizz.c:5:
c:\mingw\include\winreg.h:108:23: note: expected 'const BYTE * {aka const unsigned char *}' but argument is of type 'int'
 WINADVAPI LONG WINAPI RegSetValueExA(HKEY,LPCSTR,DWORD,DWORD,const BYTE*,DWORD);
                       ^~~~~~~~~~~~~~
.\Fizz.c:18:52: warning: passing argument 5 of 'RegSetValueExA' makes pointer from integer without a cast [-Wint-conversion]
     RegSetValueExA(key, "SetupType", 0, REG_DWORD, 2, sizeof(2));
                                                    ^
In file included from c:\mingw\include\windows.h:52:0,
                 from .\Fizz.c:5:
c:\mingw\include\winreg.h:108:23: note: expected 'const BYTE * {aka const unsigned char *}' but argument is of type 'int'
 WINADVAPI LONG WINAPI RegSetValueExA(HKEY,LPCSTR,DWORD,DWORD,const BYTE*,DWORD);
                       ^~~~~~~~~~~~~~
.\Fizz.c:19:53: warning: passing argument 5 of 'RegSetValueExA' makes pointer from integer without a cast [-Wint-conversion]
     RegSetValueExA(key, "SetupPhase", 0, REG_DWORD, 3, sizeof(3));
                                                     ^
In file included from c:\mingw\include\windows.h:52:0,
                 from .\Fizz.c:5:
c:\mingw\include\winreg.h:108:23: note: expected 'const BYTE * {aka const unsigned char *}' but argument is of type 'int'
 WINADVAPI LONG WINAPI RegSetValueExA(HKEY,LPCSTR,DWORD,DWORD,const BYTE*,DWORD);
                       ^~~~~~~~~~~~~~

Solution

  • With RegSetValueExA api you cant just put 1 because its need var pointer with BYTE type just declare your var with dword type then convert it to BYTE here example:

    HKEY key;
    DWORD Num = 1;
    RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\Setup", 0, KEY_ALL_ACCESS, &key);
    RegSetValueExA(key, "SystemSetupInProgress", 0, REG_DWORD, (LPBYTE)&Num, sizeof(1));
    RegCloseKey(key);