xenfung
asked on
C++ to Delphi Translation
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;
}
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
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,
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Gggrrrrrr... Someone was faster... :-)
ASKER
while not an exact translation, robert's submission does what i was looking for.
thanks to both for your help.
xen
thanks to both for your help.
xen
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,
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.