Welcome to MSFN

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.


Sign in to follow this  
Followers 0
BrainDrain

Delphi: Detect if registry value exists

6 posts in this topic

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 by BrainDrain
0

Share this post


Link to post
Share on other sites

Try ...

IF Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) THEN

IF Reg.ValueExists('Test') THEN

Checkbox1.checked := true;

ELSE

CheckBox1.Checked := false;

... although it might be safer as an exception

TRY

IF Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) THEN

IF Reg.ValueExists('Test') THEN

Checkbox1.checked := true;

ELSE

CheckBox1.Checked := false;

EXCEPT/FINALLY

END

0

Share this post


Link to post
Share on other sites

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) THEN
IF Reg.ValueExists('Test') THEN
Checkbox1.checked := true
ELSE
CheckBox1.Checked := false;
EXCEPT
END;
end;

But that causes an Access Violation when ran

Edited by BrainDrain
0

Share this post


Link to post
Share on other sites

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 use

reg := TRegistry.Create (KEY_READ);
reg.RootKey :=HKEY_LOCAL_MACHINE;
try
If reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', FALSE) then;

0

Share this post


Link to post
Share on other sites

Thanks for that, I have admin rights and am using TRegistry, I'll try that code and get back

EDIT:

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) THEN
IF Reg.ValueExists('Test') THEN
Checkbox1.checked := true

Would do exactly what I'm after if it worked, Sorry for getting that wrong

I'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 :blink:

Edited by BrainDrain
0

Share this post


Link to post
Share on other sites

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;

0

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.