Solved

C++ to Delphi Translation

Posted on 2001-08-12
4
870 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now