user1868232
user1868232

Reputation: 623

Delphi Writing to HKEY LOCAL MACHINE

I have a back up application that I wrote and I need it to run on start up, always for all users. I want to use

key := '\Software\Microsoft\Windows\CurrentVersion\Run';
Reg := TRegIniFile.Create;
try
  Reg.RootKey:=HKEY_LOCAL_MACHINE;
  Reg.CreateKey(Key);
  if Reg.OpenKey(Key,False) then Reg.WriteString(key, 'Backup', 'c:\backup.exe');
finally
  Reg.Free;
end;

I have written a manifest and added it as a resource, It asks for admin privileges every time it runs. However, it is not adding the the reg value and I am not sure why.

Upvotes: 1

Views: 4277

Answers (2)

David Heffernan
David Heffernan

Reputation: 613302

Your code runs in a 32 bit process. And as such it is subject to the registry redirector. That is the technology that maintains separate 32 and 64 bit views of certain portions the registry.

The way this is implemented is that the 32 bit view of HKLM\Software is stored under HKLM\Software\Wow6432Node. And that's where your registry writes are being re-directed to.

Now you could choose to write to the 64 bit view of the registry by using the KEY_WOW64_64KEY flag. However, there's no need to do that. You can simply write to the 32 bit view of the registry. When a user logs on Windows processes the Software\Microsoft\Windows\CurrentVersion\Run startup apps from both 32 and 64 bit views of the registry.

Many programs do this. Looking at my machine I can see the following entries under SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run:

  • Apple Push
  • iTunesHelper
  • QuickTime Task
  • DivXUpdate
  • SunJavaUpdate

In other words, the code in the question already works.

Upvotes: 2

user1868232
user1868232

Reputation: 623

Reg := TRegistry.Create(KEY_WRITE OR KEY_WOW64_64KEY);

Solved the issue. The above code is needed.

Upvotes: 1

Related Questions