Solved

C++ to Delphi Translation

Posted on 2001-08-12
4
880 Views
Last Modified: 2012-05-05
I need help translating the following code from C++ to Delphi (using the same API calls and not the VCL TRegistry equivalents):


BOOL RegRemoveKey(HKEY hKeyParent, LPCTSTR pszSubKey){
     HKEY          hKeyTarget;
     BOOL          bResult = TRUE;
     char          *pszNameBuffer = NULL;
     DWORD          dwLen,
                    dwMaxNameLen,
                    dwSubkeyCount;
     LRESULT          lResult;

     lResult = RegOpenKeyEx(hKeyParent, pszSubKey, 0, KEY_READ, &hKeyTarget);
     if (lResult != ERROR_SUCCESS)
          return FALSE;.
     lResult = RegQueryInfoKey(hKeyTarget, NULL, NULL, NULL, &dwSubkeyCount, &dwMaxNameLen, NULL, NULL, NULL, NULL, NULL, NULL);
     if (lResult != ERROR_SUCCESS){
          RegCloseKey(hKeyTarget);
          return FALSE;
     }
     if (dwSubkeyCount > 0){
          ++dwMaxNameLen;
          pszNameBuffer = (char *) malloc(dwMaxNameLen);
          if (pszNameBuffer == NULL){
               RegCloseKey(hKeyTarget);
               return FALSE;
          }
          while (TRUE){
               dwLen = dwMaxNameLen;
               lResult = RegEnumKeyEx(hKeyTarget, 0, pszNameBuffer, &dwLen, NULL, NULL, NULL, NULL);
               if (lResult == ERROR_NO_MORE_ITEMS){
                    break;
               }
               else if (lResult == ERROR_SUCCESS){
                    if (!RegRemoveKey(hKeyTarget, pszNameBuffer)){
                         bResult = FALSE;
                         break;
                    }
               }
               else {
                    bResult = FALSE;
                    break;
               }
          }
     }
     RegCloseKey(hKeyTarget);
     free(pszNameBuffer);
     if (bResult){
          lResult = RegDeleteKey(hKeyParent, pszSubKey);
          if (lResult != ERROR_SUCCESS)
               bResult = FALSE;
     }
     return bResult;
}



0
Comment
Question by:xenfung
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 150 total points
ID: 6378704
How about this code excerpt from the Jedi Code Library?
It is not a direct translation, but it does the same. Deleting a full registry tree.
It is also better because it recursively deletes the tree.
If you want to use it then better get the JCL from http://delphi-jedi.org.

function RegDeleteKeyTree(const RootKey: HKEY; const Key: string): Boolean;
var
  RegKey: HKEY;
  I: DWORD;
  Size: DWORD;
  NumSubKeys: DWORD;
  MaxSubKeyLen: DWORD;
  KeyName: string;
begin
  Result := RegOpenKeyEx(RootKey, RelativeKey(Key), 0, KEY_ALL_ACCESS, RegKey) = ERROR_SUCCESS;
  if Result then
  begin
    RegQueryInfoKey(RegKey, nil, nil, nil, @NumSubKeys, @MaxSubKeyLen, nil, nil, nil, nil, nil, nil);
    if NumSubKeys <> 0 then
      for I := NumSubKeys-1 downto 0 do
      begin
        Size := MaxSubKeyLen+1;
        SetLength(KeyName, Size);
        RegEnumKeyEx(RegKey, I, PChar(KeyName), Size, nil, nil, nil, nil);
        SetLength(KeyName, StrLen(PChar(KeyName)));
        Result := RegDeleteKeyTree(RootKey, Key + '\' + KeyName);
        if not Result then
          Break;
      end;
    RegCloseKey(RegKey);
    if Result then
      Result := Windows.RegDeleteKey(RootKey, RelativeKey(Key)) = ERROR_SUCCESS;
    end
    else
      WriteError(Key);
end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6378730
I've compiled it successfully, but not run. Should work, though...

function RegRemoveKey(keyParent: HKEY; subKey: string) : bool;
var keyTarget : HKEY;
    nameBuf   : string;
    len       : dword;
    maxLen    : dword;
    subkeyCnt : dword;
begin
  result := false;
  if RegOpenKeyEx(keyParent, pchar(subKey), 0, KEY_READ, keyTarget) = ERROR_SUCCESS then begin
    try
      result := RegQueryInfoKey(keyTarget, nil, nil, nil, @subKeyCnt, @maxLen, nil, nil, nil, nil, nil, nil) = ERROR_SUCCESS;
      if result and (subKeyCnt > 0) then begin
        SetLength(nameBuf, maxLen + 1);
        repeat
          len := maxLen;
          case RegEnumKeyEx(keyTarget, 0, pchar(nameBuf), len, nil, nil, nil, nil) of
            ERROR_NO_MORE_ITEMS : break;
            ERROR_SUCCESS       : result := RegRemoveKey(keyTarget, pchar(nameBuf));
            else                  result := false;
          end;
        until not result;
      end;
    finally RegCloseKey(keyTarget) end;
    result := result and (RegDeleteKey(keyParent, pchar(subKey)) = ERROR_SUCCESS);
  end;
end;

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6378734
Gggrrrrrr... Someone was faster...   :-)
0
 

Author Comment

by:xenfung
ID: 6515634
while not an exact translation, robert's submission does what i was looking for.

thanks to both for your help.


xen
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 73
Dev Express grid collapse 2 44
Wincontrol not (correctly) drawn 15 37
Tvertscrollbox like a whatsapp layout 5 26
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question