BrainDrain Posted January 1, 2007 Share Posted January 1, 2007 (edited) In my application a checkbox is ticked to show if the Run On Startup key for it exists, when the app starts this code is used:Reg.RootKey := HKey_Local_Machine;Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false); IF Reg.ValueExists('Test') then Checkbox1.checked:=true; else CheckBox1.Checked:=false;However it doesn't do anything, I am looking at the string value "Test" in the registry at the above key. If anyone could work out why it's not doing anything it would be appreciated. Edited January 1, 2007 by BrainDrain Link to comment Share on other sites More sharing options...
FAT64 Posted January 1, 2007 Share Posted January 1, 2007 Try ...IF Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) THENIF Reg.ValueExists('Test') THENCheckbox1.checked := true;ELSECheckBox1.Checked := false;... although it might be safer as an exceptionTRY IF Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) THEN IF Reg.ValueExists('Test') THEN Checkbox1.checked := true; ELSE CheckBox1.Checked := false;EXCEPT/FINALLYEND Link to comment Share on other sites More sharing options...
BrainDrain Posted January 1, 2007 Author Share Posted January 1, 2007 (edited) I've tried using the code given above, but that didn't affect Checkbox1.visible at all (It stayed ticked or unticked unless I actually clicked it), I tried setting reg.Rootkey, as in this code:TRY reg.RootKey:=HKEY_LOCAL_MACHINE;IF Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) THENIF Reg.ValueExists('Test') THENCheckbox1.checked := trueELSECheckBox1.Checked := false;EXCEPTEND;end;But that causes an Access Violation when ran Edited January 1, 2007 by BrainDrain Link to comment Share on other sites More sharing options...
Yzöwl Posted January 1, 2007 Share Posted January 1, 2007 Are you sure you have access rights, HKLM requires Administrator rights for read/write, unless you state that you are reading or you set the access rights first.As we cannot see the rest of your code, I'm guessing you're using TRegistry; since you are only reading the key you could usereg := TRegistry.Create (KEY_READ);reg.RootKey :=HKEY_LOCAL_MACHINE;tryIf reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', FALSE) then; Link to comment Share on other sites More sharing options...
BrainDrain Posted January 1, 2007 Author Share Posted January 1, 2007 (edited) Thanks for that, I have admin rights and am using TRegistry, I'll try that code and get backEDIT:I made a mistake, I'm trying to detect if a Value in a key exists, reg.RootKey:=HKEY_LOCAL_MACHINE;IF Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) THENIF Reg.ValueExists('Test') THENCheckbox1.checked := trueWould do exactly what I'm after if it worked, Sorry for getting that wrongI've found that reg.RootKey:=HKEY_LOCAL_MACHINE;IF Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) THEN Application.Terminate; doesn't do anything either, Its starting to sound like a permissions problem, but I can write and delete string values fine Edited January 1, 2007 by BrainDrain Link to comment Share on other sites More sharing options...
Yzöwl Posted January 1, 2007 Share Posted January 1, 2007 Since it is not my language of choice, here is an 'educated, (or maybe not)', suggestion:var Reg : TRegistry;begin Reg := TRegistry.Create; Reg.RootKey := HKEY_LOCAL_MACHINE; KeyName := 'Software\Microsoft\Windows\CurrentVersion\Run'; Checkbox1.checked := false; try Reg.OpenKeyReadOnly(KeyName); if Reg.ValueExists('Test') then Checkbox1.checked := true; finally Reg.CloseKey; Reg.Free; end;end; Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now