Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

Delphi: Detect if registry value exists

- - - - -

  • Please log in to reply
5 replies to this topic

#1
BrainDrain

BrainDrain

    Junior

  • Member
  • Pip
  • 53 posts
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, 01 January 2007 - 07:18 AM.



How to remove advertisement from MSFN

#2
FAT64

FAT64

    Senior Member

  • Member
  • PipPipPipPip
  • 658 posts
  • OS:Windows 7 x64
  • Country: Country Flag
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
There are 10 types of people in the world, those who understand binary and those who don't.

#3
BrainDrain

BrainDrain

    Junior

  • Member
  • Pip
  • 53 posts
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, 01 January 2007 - 04:34 AM.


#4
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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;


#5
BrainDrain

BrainDrain

    Junior

  • Member
  • Pip
  • 53 posts
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, 01 January 2007 - 07:28 AM.


#6
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN