Jump to content
Ian Branch

Delete a Registry Key...

Recommended Posts

Hi Team,

Not sure what is going on here..  Just about to tear what little hair I have out.

I am trying to delete a registry key.  Win 10, 64bit, D10.3.2.  32bit App.

I have the following code..

procedure TMainForm.ClearKey1Click(Sender: TObject);
var
  reg: TRegistry;
begin
  //
    MessageBeep(MB_ICONINFORMATION);
    reg := TRegistry.Create(KEY_WRITE);
    reg.RootKey := HKEY_CURRENT_USER;

    reg.DeleteKey('DBiW\DBiW\JTGrid');

    reg.CloseKey();
  //
end;

It runs without issue/error but does not delete the Key. :-(

Is this some 64bit gotcha?

I need it to work in 64 & 32 bit environments.

Yes, System.Win.Registry is in the Uses list.

Thoughts/suggestions appreciated.

Share this post


Link to post

Hi David,

No.  It is holding a set of string properties from a Grid.  See picture.

As you can see I simplified the above code.

Somehow I got two pictures in here. 😞

 

IanScreenshot_4.png.3167feee4ab0a0ea6d48ea44e1f5b974.png

Screenshot_4.png

Edited by Ian Branch

Share this post


Link to post

OK, just looked tlat the documentation and it explains it all. You have to delete the sub keys. And you won't get an error in your code because the function returns a boolean indicating success or failure. You didn't check this. 

 

Let me gently suggest that rather than tearing hair out, the documentation may help!

Share this post


Link to post

Hi David,

Feel free to point out the error of my ways.  I used to be perfect, now I'm not so sure. ;-)

I have been lead astray by the ClearKey function above being called immediately after the JobTicket Form Modal Show from within the Menu call.

I separated it out and called the routine separately and it works.  With all the key data there.

Yes, I know, back to front but I had every expectation it would work. :-(

Thanks for your input and gentle prompt. :-)

Regards,

Ian

 

Share this post


Link to post
1 hour ago, David Heffernan said:

just looked tlat the documentation and it explains it all. You have to delete the sub keys.

Seems like the docs are mistaken here. The source code is more reliable and shows that subkeys are internally deleted.

Share this post


Link to post

 

On 9/7/2019 at 11:16 PM, Ian Branch said:

I have the following code..


    reg.RootKey := HKEY_CURRENT_USER;
    reg.DeleteKey('DBiW\DBiW\JTGrid');

 

Is "HKCU\DbiW\DBiW\TJGrid" the actual path to your subkey?  Your screenshot does not show the whole tree.  It is very unusual to see a user-defined key that is not located under one of the system-provided keys, such as "HKCU\Software" (where they usually belong).

On 9/7/2019 at 11:16 PM, Ian Branch said:

It runs without issue/error but does not delete the Key. 😞

TRegistry.DeleteKey() returns a boolean to indicate success, which you are not checking.  If it returns False, check the TRegistry.LastError and/or TRegistry.LastErrorMsg properties, they tell you why if failed.

On 9/8/2019 at 12:29 AM, David Heffernan said:

Don't you need to empty the key before you delete it? 

Despite what the documentation claims, TRegistry.DeleteKey() actually handles this internally for you.  It runs a recursive loop to delete all subkeys before then deleting the requested key.  You don't have to delete them manually (though you probably should so you can perform error handling on each one).  Otherwise, use the Win32 RegDeleteTree() or SHDeleteKey() function instead and let the OS delete all of the subkeys for you.

Share this post


Link to post
2 hours ago, Remy Lebeau said:

 

OK first try at quoting and I may screw it up.

I abbreviated the path for the snippet.

"HKCU\Software" - You are correct and I will ammend it.

2 hours ago, Remy Lebeau said:

Function return, - Probably should have.

2 hours ago, Remy Lebeau said:

Despite what the documentation claims, TRegistry.DeleteKey() actually handles this internally for you.  It runs a recursive loop to delete all subkeys before then deleting the requested key.  You don't have to delete them manually (though you probably should so you can perform error handling on each one).  Otherwise, use the Win32 RegDeleteTree() or SHDeleteKey() function instead and let the OS delete all of the subkeys for you.

This is what I found.

Share this post


Link to post

Yep.  Screwed it up.  How do you get the individual quote sections into the reply please?

  • Like 1

Share this post


Link to post
8 hours ago, Ian Branch said:

How do you get the individual quote sections into the reply please?

Highlight the text with the mouse and click on the then appearing Quote bubble: 

11-09-_2019_09-15-48.gif

Share this post


Link to post
1 minute ago, Uwe Raabe said:

Highlight the text with the mouse and click on the then appearing Quote bubble: 

Thanks Uwe, appreciated.

 

Ian

Share this post


Link to post

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

×