0
votes

My test program to insert values into the registry does not work. I have not found a solution on Google or this website. After running the program as administrator, it instantly closes and the register is not modified.

HKEY hkey;
const char PATH[] = "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"; 

RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    0,
    KEY_WRITE,
    &hkey);

RegSetValueEx(hkey,
    "TestWordPad",
    0,
    REG_SZ,
    (BYTE*)PATH,
    strlen(PATH));

RegCloseKey(hkey);

return 0;

After I start debugging is this in output:

'ConsoleApplication1.exe' (Win32): Loaded 'C:\Users\jakub\Desktop\ConsoleApplication1\Debug\ConsoleApplication1.exe'. Symbols loaded. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-timezone-l1-1-0.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-file-l2-1-0.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-localization-l1-2-0.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-synch-l1-2-0.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-processthreads-l1-1-1.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-file-l1-2-0.dll'. Cannot find or open the PDB file. The program '[3052] ConsoleApplication1.exe' has exited with code 0 (0x0).

1
The errors you see are all Cannot find or open the PDB file. It means you're using libraries without symbols for debug, but it won't affect the result.Francesco Dondi
when I run it normal write me access denied if I run it as administator program closeMarek
@joppari: Why wouldn't your program close if it successfully updated the registry?MSalters

1 Answers

1
votes

There are 2 reasons this code won't work consistently:

  1. If run on Windows Vista or later it requires Admin rights (e.g. elevated UAC) to write to HKLM. You can try this by launching an Admin command prompt or running Visual Studio as Administrator

  2. If this code is compiled as a 32-bit app but run on a 64-bit system it actually modifies the following key:

    HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run
    

If you take these 2 points into account the code does work. I would recommend checking the return codes from each function. For instance:

LONG lResult=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    0,
    KEY_WRITE,
    &hkey);

If you run the code without Admin rights you will find this returns 5 (ERROR_ACCESS_DENIED).