Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ to Delphi Translation

Posted on 2001-08-12
4
Medium Priority
?
914 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 600 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

715 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