我想在win2000下删除注册表中的一个键值:HKEY_LOCAL_MACHINE\software\microsoft\windows\CurrentVersion\Run中的abcde,
但用RegDelteKey和RegDelteValue好象都不行,哪位大侠给一下源码,100分相赠。

解决方案 »

  1.   

    //try this
    //删除一个键下的所有值
    BOOL DeleteKey(HKEY hKey,LPCTSTR pszSubKey,LPCTSTR pszValueName)
    {ASSERT( pszSubKey != 0 ) ;
    ASSERT( hKey != 0 ) ;HKEY hSubKey ;
    LONG lRet = RegOpenKeyEx(
    hKey, // key handle at root level
    pszSubKey, // path name of child key
    0, // reserved
    KEY_WRITE | KEY_READ, // requesting access
    &hSubKey // address of key to be returned
    );if( lRet != ERROR_SUCCESS ) 
    {
    ASSERT(0);
    TRACE0( "CRegistry::DeleteKey(): RegOpenKeyEx() failed\n" ) ;
    return FALSE ;
    }if( pszValueName ) 
    {
    // 如果参数pszValueName不为NULL则仅删除该键
    lRet = RegDeleteValue( hSubKey, pszValueName ) ;
    RegCloseKey( hSubKey ) ;
    RegCloseKey( hKey ) ;
    if( lRet != ERROR_SUCCESS ) 
    {
    ASSERT(0);
    TRACE0( "CRegistry::DeleteKey(): RegDeleteValue() failed\n" );
    return FALSE ;
    }

    else 
    {
    DWORD dwSubKeyCnt = 0 ;
    do 

    // 得到子键的信息
    DWORD dwMaxSubKey ;
    LONG lRet = RegQueryInfoKey(
    hSubKey,
    0, // buffer for class name
    0, // length of class name string
    0, // reserved
    &dwSubKeyCnt, // # of subkeys
    &dwMaxSubKey, // length of longest subkey
    0, // length of longest class name string
    0, // # of values
    0, // length of longest value name
    0, // length of longest value data
    0, // security descriptor
    0 // last write time
    ) ;
    if( lRet != ERROR_SUCCESS ) 
    {
    TRACE0( "CRegistry::DeleteKey(): RegQueryInfoKey() failed.\n" ) ;
    RegCloseKey( hSubKey ) ;
    return FALSE ;
    }
      

  2.   

    //try this
    //删除一个键下的所有值
    BOOL DeleteKey(HKEY hKey,LPCTSTR pszSubKey,LPCTSTR pszValueName)
    {ASSERT( pszSubKey != 0 ) ;
    ASSERT( hKey != 0 ) ;HKEY hSubKey ;
    LONG lRet = RegOpenKeyEx(
    hKey, // key handle at root level
    pszSubKey, // path name of child key
    0, // reserved
    KEY_WRITE | KEY_READ, // requesting access
    &hSubKey // address of key to be returned
    );if( lRet != ERROR_SUCCESS ) 
    {
    ASSERT(0);
    TRACE0( "CRegistry::DeleteKey(): RegOpenKeyEx() failed\n" ) ;
    return FALSE ;
    }if( pszValueName ) 
    {
    // 如果参数pszValueName不为NULL则仅删除该键
    lRet = RegDeleteValue( hSubKey, pszValueName ) ;
    RegCloseKey( hSubKey ) ;
    RegCloseKey( hKey ) ;
    if( lRet != ERROR_SUCCESS ) 
    {
    ASSERT(0);
    TRACE0( "CRegistry::DeleteKey(): RegDeleteValue() failed\n" );
    return FALSE ;
    }

    else 
    {
    DWORD dwSubKeyCnt = 0 ;
    do 

    // 得到子键的信息
    DWORD dwMaxSubKey ;
    LONG lRet = RegQueryInfoKey(
    hSubKey,
    0, // buffer for class name
    0, // length of class name string
    0, // reserved
    &dwSubKeyCnt, // # of subkeys
    &dwMaxSubKey, // length of longest subkey
    0, // length of longest class name string
    0, // # of values
    0, // length of longest value name
    0, // length of longest value data
    0, // security descriptor
    0 // last write time
    ) ;
    if( lRet != ERROR_SUCCESS ) 
    {
    TRACE0( "CRegistry::DeleteKey(): RegQueryInfoKey() failed.\n" ) ;
    RegCloseKey( hSubKey ) ;
    return FALSE ;
    }if( dwSubKeyCnt > 0 ) 
    {LPTSTR pszKeyName = new TCHAR [ dwMaxSubKey + 1 ] ;
    DWORD dwKeyNameLen = dwMaxSubKey ;
    lRet = RegEnumKey(
    hSubKey,
    0, // index
    pszKeyName, // address of buffer for key name string
    dwKeyNameLen+1 // max. length of key name string
    ) ;if( lRet != ERROR_SUCCESS ) 
    {
    TRACE0( "CRegistry::DeleteKey(): RegEnumKey() failed\n" ) ;
    delete [] pszKeyName ;
    RegCloseKey( hSubKey ) ;
    return FALSE ;
    }
    if( ! DeleteKey( hSubKey,pszKeyName, pszValueName ) ) 
    {
    delete [] pszKeyName ;
    RegCloseKey( hSubKey ) ;
    return FALSE ;
    }
    delete [] pszKeyName ;
    }
    } while( dwSubKeyCnt > 0 ) ;RegCloseKey( hSubKey ) ;lRet = RegDeleteKey( hKey, pszSubKey ) ;//RegCloseKey( hKey ) ;if( lRet != ERROR_SUCCESS ) 
    {
    TRACE0( "CRegistry::DeleteKey(): RegDeleteKey() failed\n" ) ;
    return FALSE ;
    }
    }return TRUE ;
    }
      

  3.   

    //try this
    //删除一个键下的所有值
    BOOL DeleteKey(HKEY hKey,LPCTSTR pszSubKey,LPCTSTR pszValueName)
    {ASSERT( pszSubKey != 0 ) ;
    ASSERT( hKey != 0 ) ;HKEY hSubKey ;
    LONG lRet = RegOpenKeyEx(
    hKey, // key handle at root level
    pszSubKey, // path name of child key
    0, // reserved
    KEY_WRITE | KEY_READ, // requesting access
    &hSubKey // address of key to be returned
    );if( lRet != ERROR_SUCCESS ) 
    {
    ASSERT(0);
    TRACE0( "CRegistry::DeleteKey(): RegOpenKeyEx() failed\n" ) ;
    return FALSE ;
    }if( pszValueName ) 
    {
    // 如果参数pszValueName不为NULL则仅删除该键
    lRet = RegDeleteValue( hSubKey, pszValueName ) ;
    RegCloseKey( hSubKey ) ;
    RegCloseKey( hKey ) ;
    if( lRet != ERROR_SUCCESS ) 
    {
    ASSERT(0);
    TRACE0( "CRegistry::DeleteKey(): RegDeleteValue() failed\n" );
    return FALSE ;
    }

    else 
    {
    DWORD dwSubKeyCnt = 0 ;
    do 

    // 得到子键的信息
    DWORD dwMaxSubKey ;
    LONG lRet = RegQueryInfoKey(
    hSubKey,
    0, // buffer for class name
    0, // length of class name string
    0, // reserved
    &dwSubKeyCnt, // # of subkeys
    &dwMaxSubKey, // length of longest subkey
    0, // length of longest class name string
    0, // # of values
    0, // length of longest value name
    0, // length of longest value data
    0, // security descriptor
    0 // last write time
    ) ;
    if( lRet != ERROR_SUCCESS ) 
    {
    TRACE0( "CRegistry::DeleteKey(): RegQueryInfoKey() failed.\n" ) ;
    RegCloseKey( hSubKey ) ;
    return FALSE ;
    }if( dwSubKeyCnt > 0 ) 
    {LPTSTR pszKeyName = new TCHAR [ dwMaxSubKey + 1 ] ;
    DWORD dwKeyNameLen = dwMaxSubKey ;
    lRet = RegEnumKey(
    hSubKey,
    0, // index
    pszKeyName, // address of buffer for key name string
    dwKeyNameLen+1 // max. length of key name string
    ) ;if( lRet != ERROR_SUCCESS ) 
    {
    TRACE0( "CRegistry::DeleteKey(): RegEnumKey() failed\n" ) ;
    delete [] pszKeyName ;
    RegCloseKey( hSubKey ) ;
    return FALSE ;
    }
    if( ! DeleteKey( hSubKey,pszKeyName, pszValueName ) ) 
    {
    delete [] pszKeyName ;
    RegCloseKey( hSubKey ) ;
    return FALSE ;
    }
    delete [] pszKeyName ;
    }
    } while( dwSubKeyCnt > 0 ) ;RegCloseKey( hSubKey ) ;lRet = RegDeleteKey( hKey, pszSubKey ) ;//RegCloseKey( hKey ) ;if( lRet != ERROR_SUCCESS ) 
    {
    TRACE0( "CRegistry::DeleteKey(): RegDeleteKey() failed\n" ) ;
    return FALSE ;
    }
    }return TRUE ;
    }
      

  4.   

    第一,函数名写错了。
    第二,检查一下,HKEY_LOCAL_MACHINE\software\microsoft\windows\CurrentVersion\Run
    当作参数用的时候对不对。
    第三,我只用过设置一个值,参考一下,有没有用我不知道:)
    hSoftDirKey=HKEY_LOCAL_MACHINE;
    strKeyValue="设置值";
    //The keyvalue we want to be set
    LPCTSTR szSoftware="\\software\\microsoft\\windows\\CurrentVersion\\Run";
    RegOpenKeyEx(hSoftDirKey,szSoftware,0,KEY_WRITE|KEY_READ,&hReturnKey);
    //Get the handle of the key
    DWORD dwSize=strlen(strKeyValue)+1;
    RegSetValueEx(hReturnKey,strKeyName,0,REG_SZ,(CONST BYTE*)(LPCTSTR)strKeyValue,dwSize);
      

  5.   

    win2000下需要有权限才可修改注册表。
      

  6.   

    runbuff (玩水的人),把你的code贴出来。
      

  7.   

    如果abcde是个子键并且它下面还有其他的子键,如abcde\edcba,用RegDelteKey是删不掉的,你必须使用SHDeleteKey,用法可以参照msdn。
      

  8.   

    谢谢各位。
    但只能把分给一位大侠而且wistaria(听风听雨)的代码是正确的,能同时用于win98和win2000。