Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C++ to Delphi Translation

Posted on 2001-08-12
4
Medium Priority
?
917 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 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Loops Section Overview
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

971 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