Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 



Sign in to follow this  
BrainDrain

Delphi: Detect if registry value exists

Recommended Posts

BrainDrain    0

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

Share this post


Link to post
Share on other sites

FAT64    0

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

Share this post


Link to post
Share on other sites
BrainDrain    0

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

Share this post


Link to post
Share on other sites
Yzöwl    30

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;

Share this post


Link to post
Share on other sites
BrainDrain    0

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

Share this post


Link to post
Share on other sites
Yzöwl    30

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;

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×