Removing registry keys - error in MSDN example

This MSDN article should demonstrate how to remove a registry key that has subkeys, but the code has flaws.

The line that says

StringCchCopy (lpEnd, MAX_PATH*2, szName);

throws an exception that is associated with an attempt to copy outside the lpEnd buffer. I tried to fix the solution by replacing this line with the following

size_t subKeyLen = lstrlen(lpSubKey);
size_t bufLen = subKeyLen + lstrlen(szName)+1;
LPTSTR buf = new WCHAR[bufLen];
StringCchCopy(buf,bufLen,lpSubKey);
StringCchCopy(buf+subKeyLen,lstrlen(szName)+1,szName);
buf[bufLen-1]='\0';

I cannot go through the code because the target platform and the dev platform are different, but from the log I entered into the code, it looks like it just hangs, but does not throw an exception.

It is frustrating that MSDN articles are wrong ... you think they will be checked.

Any ideas on how to fix this?

Thank.

+3
source share
2 answers

Shlwapi.dll , SHDeleteKey. Vista +, RegDeleteTree ( Advapi32.dll), .

+1

. :

        if (!RegDelnodeRecurse(hKeyRoot, lpSubKey)) {
            break;

. lpSubKey buf, .

, , , () buf .

, , , ( , , 255)

StringCchCopy (lpEnd, MAX_PATH*2 - lstrlen(lpSubKey), szName);
+1

All Articles