Solved

C++ to Delphi Translation

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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