c++ - 从注册表 :Code throw unhandled exception 中删除项和子项

标签 c++ visual-c++ access-violation

我想删除注册表项和所有子项。我正在使用 here 中的代码.

代码编译正常,但在调试代码时

"Unhandled exception at 0x00416d14 in deletedemo.exe: 0xC0000005: Access violation writing location 0x0041ff01."

异常发生在行

// Check for an ending slash and add one if it is missing.

    lpEnd = lpSubKey + lstrlen(lpSubKey);

    if (*(lpEnd - 1) != TEXT('\\')) 
    {
        *lpEnd =  TEXT('\\'); //Here exception occur.
        lpEnd++;
        *lpEnd =  TEXT('\0');
    }

最佳答案

前段时间我写了一段递归删除Registry Key和它的Subkeys的代码。代码是这样的::

static BOOL RcrsvRegDel( HKEY hKey, LPTSTR lpszSub, DWORD dwOpenFlags )
{
BOOL    bRet = TRUE ;
LONG    lRet ;
DWORD   dwSize = MAX_PATH ;
TCHAR   szName[MAX_PATH] ;
HKEY    hKeySub = NULL ;
HRESULT hr = NULL ;
HANDLE  hProcess = NULL ;
HANDLE  hToken = NULL ;

do{
    lRet = RegOpenKeyEx( hKey, lpszSub, 0, dwOpenFlags, &hKeySub ) ;
    if( lRet != ERROR_SUCCESS )
    {
        bRet = FALSE ;
        break ;
    }

    while( ERROR_NO_MORE_ITEMS != (lRet = RegEnumKeyEx(hKeySub, 0, szName, &dwSize, NULL, 
        NULL, NULL, NULL)) )
        if( !RcrsvRegDel(hKeySub, szName, dwOpenFlags) ) 
        {
            bRet = FALSE ;
            break ;
        }

    lRet = RegDeleteKey( hKey, lpszSub ) ;
    printf("RegDelKey:: %S :: lRet = %ld\n", lpszSub, lRet) ;
    if( lRet != ERROR_SUCCESS )
    {
        bRet = FALSE ;
        break ;
    }

    if( hKeySub != NULL )
    {
        RegCloseKey(hKeySub) ;
        hKeySub = NULL ;
    }
}while(0) ;
return bRet ;
}

dwOpenFlags = 要传递给 RegOpenKeyEx 或 RegDeleteKey 的标志。

编辑::如果您不想自己递归删除整棵树,MSDN 有两个函数可以做到这一点。您可以随时使用它们,即 RegDeleteTreeSHDeleteKey .

关于c++ - 从注册表 :Code throw unhandled exception 中删除项和子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12890654/

相关文章:

c++ - 为什么我的输出在到达代码的这一部分时卡住了?

c++ - for( ... ) 循环索引声明样式

c - C中的变量声明及其内存地址

c# - Interop C#/C : AccessViolationException 问题

c++ - 写入时 WaitForMultipleObjects 访问冲突

c - 第一次机会异常 : KernelBase. dll

c++ - Win32 API 定时器

c++ - 在 C++ 中创建 afwin 文件

c++ - 从 MFC 应用程序将图像文件流式传输到 HTML 页面

c++ - 使用静态 openssl 1.1.1a 编译静态 curl