Reputation: 2349
How can delete keys on HKLM in windows 2000 or XP (before UAC)
it seems the program must run with admin rights but I don't find any correct ways .
this below codes are the delete procedure and it just work when I open the regedit
and open that key , then program can delete keys .
procedure TForm1.Button2Click(Sender: TObject);
var
reg : TRegistry;
begin
reg := TRegistry.Create(KEY_WRITE);
//reg.Access := KEY_WRITE;
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.DeleteKey('SYSTEM\\ControlSet001\\Services\\OLD_DRIVER\\Enum');
reg.CloseKey();
end;
Is there any other ways to delete the keys under HKLM like win32 API or run program with admin rights ?
NOTE : this is not a duplicate question , it's a question after crawled all over the stackoverflow and not answered yet .
EDIT & Notes :
1. The OS is specially in Windows 2000 .
2. The addressed key at above code is a sub-key without any other sub-key .
3. Repeat it , the above code will work if I Open the regedit then go to HKLM\\SYSTEM\\ControlSet001\\Services\\OLD_DRIVER\\Enum
.
4. I've test it the registry create with KEY_ALL_ACCESS
and KEY_WRITE
and got the same result .
5. I've just testing it on 32 bit OS and not need wow64 access
in registry create section .
Upvotes: 0
Views: 283
Reputation: 1483
To delete a key with content (and subkeys) please try this code. It uses the SHDeleteKey function which should be available in Windows 2000. Admin rights are usually required to delete keys in HKey_Local_Machine (members of the built in "Power Users" security group in Windows 2000 / XP can delete some, but not all).
{$APPTYPE CONSOLE}
uses
SysUtils, Windows;
function SHDeleteKey (hKey: HKEY; pszSubKey: LPCTSTR) : DWord; stdcall;
{$IFDEF UNICODE}
external 'shlwapi.dll' name 'SHDeleteKeyW';
{$ELSE}
external 'shlwapi.dll' name 'SHDeleteKeyA';
{$ENDIF}
const
cKey = 'SOFTWARE\Test';
begin
if (SHDeleteKey (HKey_Local_Machine, cKey) = Error_Success) then
WriteLn ('Success')
else WriteLn ('Error');
end.
Upvotes: 1