问题
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.
回答1:
Reg := TRegistry.Create(KEY_WRITE OR KEY_WOW64_64KEY);
Solved the issue. The above code is needed.
回答2:
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.
来源:https://stackoverflow.com/questions/15594940/delphi-writing-to-hkey-local-machine