需要一个操作注册表的封装类,哪位大哥有源代码~~~
谢谢了先!!!

解决方案 »

  1.   

    http://www.vckbase.com/code/listcode.asp?mclsid=13&sclsid=1317
      

  2.   

    http://www.vckbase.com/document/viewdoc/?id=603
      

  3.   

    // Registry.hclass CRegistry
    {public:
    CRegistry();
    CRegistry( HKEY, const char * );
    ~CRegistry(); BOOL Open( HKEY, const char * );
    BOOL Close( void );
    BOOL IsOpen( void ); BOOL ReadDWORD( const char *, DWORD *, DWORD *pdwLastError = NULL );
    BOOL ReadString( const char *, LPVOID, int, DWORD *pdwLastError = NULL );
    char **ReadMultipleStrings( const char *, DWORD *pdwLastError = NULL );
    static void DeleteList( char ** ); BOOL WriteDWORD( const char *, DWORD, DWORD *pdwLastError = NULL );
    BOOL WriteString( const char *, LPVOID, DWORD *pdwLastError = NULL );
    BOOL Write( const char *, LPVOID, DWORD, int ); static BOOL CreateKey( HKEY, const char *, const char * );
    static BOOL DeleteKey( HKEY, const char * );protected:
    HKEY m_hKey;
    DWORD m_dwLastError;
    int m_nSize;
        char  *m_strUseKey;
    BOOL Read( const char *, LPVOID, int );};
    // Registry.cpp#include "stdafx.h"
    #include "Registry.h"
    CRegistry::CRegistry()
    { m_hKey = NULL;
    Open( m_hKey, m_strUseKey );}CRegistry::CRegistry( HKEY hKey, const char *lpszSubKey )
    { m_hKey = NULL;
    Open( hKey, lpszSubKey );}CRegistry::~CRegistry()
    { Close();}BOOL CRegistry::Open( HKEY hKey, const char *lpszSubKey )
    { Close(); if( ::RegOpenKeyEx( hKey, lpszSubKey, 0, KEY_ALL_ACCESS, &m_hKey ) != ERROR_SUCCESS ){
    m_hKey = NULL;
    m_dwLastError = GetLastError();
    return( FALSE );
    } return( TRUE );}BOOL CRegistry::Close( void )
    {
    BOOL bRet = TRUE; if( m_hKey == NULL )
    return( FALSE ); bRet = ( ::RegCloseKey( m_hKey ) == ERROR_SUCCESS );
    m_hKey = NULL; return( bRet );}BOOL CRegistry::IsOpen( void )
    { return( m_hKey != NULL );}BOOL CRegistry::Read( const char *lpszValueName, LPVOID lpReturnBuffer, int nSize )
    { if( m_hKey == NULL )
    return( FALSE ); DWORD dwSize = (DWORD) nSize;
    BOOL bRet = ( ::RegQueryValueEx( m_hKey, lpszValueName, NULL, NULL, (unsigned char *) lpReturnBuffer, &dwSize ) == ERROR_SUCCESS ); m_dwLastError = GetLastError(); return( bRet );}BOOL CRegistry::ReadDWORD( const char *lpszValueName, DWORD *pdwData, DWORD *pdwLastError )
    { if( m_hKey == NULL )
    return( FALSE ); BOOL bRet = Read( lpszValueName, pdwData, sizeof( DWORD ) ); if( pdwLastError != NULL )
    *pdwLastError = m_dwLastError; return( bRet );}BOOL CRegistry::ReadString( const char *lpszValueName, LPVOID lpReturnBuffer, int nSize, DWORD *pdwLastError )
    { if( m_hKey == NULL )
    return( FALSE ); char *lpWork = (char *) lpReturnBuffer;
    lpWork[0] = 0;
    BOOL bRet = Read( lpszValueName, lpReturnBuffer, nSize ); if( pdwLastError != NULL )
    *pdwLastError = m_dwLastError; return( bRet );}char **CRegistry::ReadMultipleStrings( const char *lpszValueName, DWORD *pdwLastError )
    {
    char szEntireString[2500]; if( !ReadString( lpszValueName, szEntireString, 2500, pdwLastError ) )
    return( NULL ); if( szEntireString[0] == 0 )
    return( NULL ); int nCount = 0;
    if( szEntireString[strlen(szEntireString)-1] != ';' )
    nCount = 1;
    char *pPointer = szEntireString;
    do{
    pPointer = strstr( pPointer, ";" );
    if( pPointer != NULL ){
    nCount++;
    pPointer++;
    }
    } while( pPointer != NULL ); int i = 0;
    char *pEnd;
    char **pList = (char **) new char [(nCount+3)*sizeof(char *)];
    if( pList == NULL )
    return( NULL );
    memset( pList, 0, ( nCount + 3 ) * sizeof(char *) ); pPointer = szEntireString;
    do{ pEnd = strstr( pPointer, ";" );
    int nSize = strlen( pPointer );
    if( pEnd != NULL )
    nSize = pEnd - pPointer; pList[i] = new char [nSize+3];
    if( pList[i] != NULL ){
    memset( pList[i], 0, nSize + 3 );
    memcpy( pList[i], pPointer, nSize );
    }
    else pEnd = NULL; pPointer = pEnd;
    if( pPointer != NULL )
    pPointer++; i++; } while( pPointer != NULL && pPointer[0] != 0 ); return( pList );}void CRegistry::DeleteList( char **pList )
    { if( pList == NULL )
    return; int i = 0;
    while( pList[i] != NULL ){
    delete [] pList[i];
    i++;
    }
    delete pList;}BOOL CRegistry::WriteDWORD( const char *lpszValueName, DWORD dwData, DWORD *pdwLastError )
    { if( m_hKey == NULL )
    return( FALSE ); m_nSize = sizeof( DWORD );
    BOOL bRet = Write( lpszValueName, &dwData, REG_DWORD, sizeof( DWORD ) ); if( pdwLastError != NULL )
    *pdwLastError = m_dwLastError; return( bRet );}BOOL CRegistry::WriteString( const char *lpszValueName, LPVOID lpData, DWORD *pdwLastError )
    { if( m_hKey == NULL )
    return( FALSE ); m_nSize = 2000;
    BOOL bRet = Write( lpszValueName, lpData, REG_SZ, (DWORD) strlen( (const char *) lpData ) + 1 ); if( pdwLastError != NULL )
    *pdwLastError = m_dwLastError; return( bRet );}BOOL CRegistry::Write( const char *lpszValueName, LPVOID lpData, DWORD dwType, int nSize )
    { if( m_hKey == NULL )
    return( FALSE ); DWORD dwSize = (DWORD) m_nSize;
    BOOL bRet = ( ::RegSetValueEx( m_hKey, lpszValueName, 0, dwType, (unsigned char *) lpData, nSize ) == ERROR_SUCCESS ); m_dwLastError = GetLastError(); return( bRet );}BOOL CRegistry::CreateKey( HKEY hKey, const char *lpszSubKey, const char *lpszClass )
    {
    HKEY hOpenedKey;
    DWORD dwDisposition;
    DWORD dwLastError; BOOL bRet = ( ::RegCreateKeyEx( hKey, lpszSubKey, 0, (char *) lpszClass, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hOpenedKey, &dwDisposition ) == ERROR_SUCCESS );
    if( bRet ) ::RegCloseKey( hOpenedKey );
    dwLastError = GetLastError(); return( bRet );}BOOL CRegistry::DeleteKey( HKEY hKey, const char *lpszSubKey )
    {
    BOOL bRet;
    DWORD dwLastError; bRet = ( ::RegDeleteKey( hKey, lpszSubKey ) == ERROR_SUCCESS );
    dwLastError = GetLastError(); return( bRet );}
      

  4.   

    wtl的
    /////////////////////////////////////////////////////////////////////////////
    // CRegKeyclass CRegKey
    {
    public:
    CRegKey();
    ~CRegKey();// Attributes
    public:
    operator HKEY() const;
    HKEY m_hKey;// Operations
    public:
    LONG SetValue(DWORD dwValue, LPCTSTR lpszValueName);
    LONG QueryValue(DWORD& dwValue, LPCTSTR lpszValueName);
    LONG QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount);
    LONG SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL); LONG SetKeyValue(LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL);
    static LONG WINAPI SetValue(HKEY hKeyParent, LPCTSTR lpszKeyName,
    LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL); LONG Create(HKEY hKeyParent, LPCTSTR lpszKeyName,
    LPTSTR lpszClass = REG_NONE, DWORD dwOptions = REG_OPTION_NON_VOLATILE,
    REGSAM samDesired = KEY_ALL_ACCESS,
    LPSECURITY_ATTRIBUTES lpSecAttr = NULL,
    LPDWORD lpdwDisposition = NULL);
    LONG Open(HKEY hKeyParent, LPCTSTR lpszKeyName,
    REGSAM samDesired = KEY_ALL_ACCESS);
    LONG Close();
    HKEY Detach();
    void Attach(HKEY hKey); LONG EnumKey(DWORD iIndex, LPTSTR pszName, LPDWORD pnNameLength, FILETIME* pftLastWriteTime = NULL) throw();

    LONG DeleteSubKey(LPCTSTR lpszSubKey);
    LONG RecurseDeleteKey(LPCTSTR lpszKey);
    LONG DeleteValue(LPCTSTR lpszValue);};inline CRegKey::CRegKey()
    {m_hKey = NULL;}inline CRegKey::~CRegKey()
    {Close();}inline CRegKey::operator HKEY() const
    {return m_hKey;}inline HKEY CRegKey::Detach()
    {
    HKEY hKey = m_hKey;
    m_hKey = NULL;
    return hKey;
    }inline void CRegKey::Attach(HKEY hKey)
    {
    ATLASSERT(m_hKey == NULL);
    m_hKey = hKey;
    }inline LONG CRegKey::DeleteSubKey(LPCTSTR lpszSubKey)
    {
    ATLASSERT(m_hKey != NULL);
    return RegDeleteKey(m_hKey, lpszSubKey);
    }inline LONG CRegKey::DeleteValue(LPCTSTR lpszValue)
    {
    ATLASSERT(m_hKey != NULL);
    return RegDeleteValue(m_hKey, (LPTSTR)lpszValue);
    }inline LONG CRegKey::Close()
    {
    LONG lRes = ERROR_SUCCESS;
    if (m_hKey != NULL)
    {
    lRes = RegCloseKey(m_hKey);
    m_hKey = NULL;
    }
    return lRes;
    }inline LONG CRegKey::EnumKey(DWORD iIndex, LPTSTR pszName, LPDWORD pnNameLength, FILETIME* pftLastWriteTime) throw()
    {
    FILETIME ftLastWriteTime; ATLASSERT(m_hKey != NULL);
    if (pftLastWriteTime == NULL)
    {
    pftLastWriteTime = &ftLastWriteTime;
    } return ::RegEnumKeyEx(m_hKey, iIndex, pszName, pnNameLength, NULL, NULL, NULL, pftLastWriteTime);
    }inline LONG CRegKey::Create(HKEY hKeyParent, LPCTSTR lpszKeyName,
    LPTSTR lpszClass, DWORD dwOptions, REGSAM samDesired,
    LPSECURITY_ATTRIBUTES lpSecAttr, LPDWORD lpdwDisposition)
    {
    ATLASSERT(hKeyParent != NULL);
    DWORD dw;
    HKEY hKey = NULL;
    LONG lRes = RegCreateKeyEx(hKeyParent, lpszKeyName, 0,
    lpszClass, dwOptions, samDesired, lpSecAttr, &hKey, &dw);
    if (lpdwDisposition != NULL)
    *lpdwDisposition = dw;
    if (lRes == ERROR_SUCCESS)
    {
    lRes = Close();
    m_hKey = hKey;
    }
    return lRes;
    }inline LONG CRegKey::Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired)
    {
    ATLASSERT(hKeyParent != NULL);
    HKEY hKey = NULL;
    LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyName, 0, samDesired, &hKey);
    if (lRes == ERROR_SUCCESS)
    {
    lRes = Close();
    ATLASSERT(lRes == ERROR_SUCCESS);
    m_hKey = hKey;
    }
    return lRes;
    }inline LONG CRegKey::QueryValue(DWORD& dwValue, LPCTSTR lpszValueName)
    {
    DWORD dwType = NULL;
    DWORD dwCount = sizeof(DWORD);
    LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
    (LPBYTE)&dwValue, &dwCount);
    ATLASSERT((lRes!=ERROR_SUCCESS) || (dwType == REG_DWORD));
    ATLASSERT((lRes!=ERROR_SUCCESS) || (dwCount == sizeof(DWORD)));
    return lRes;
    }inline LONG CRegKey::QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount)
    {
    ATLASSERT(pdwCount != NULL);
    DWORD dwType = NULL;
    LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
    (LPBYTE)szValue, pdwCount);
    ATLASSERT((lRes!=ERROR_SUCCESS) || (dwType == REG_SZ) ||
     (dwType == REG_MULTI_SZ) || (dwType == REG_EXPAND_SZ));
    return lRes;
    }inline LONG WINAPI CRegKey::SetValue(HKEY hKeyParent, LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName)
    {
    ATLASSERT(lpszValue != NULL);
    CRegKey key;
    LONG lRes = key.Create(hKeyParent, lpszKeyName);
    if (lRes == ERROR_SUCCESS)
    lRes = key.SetValue(lpszValue, lpszValueName);
    return lRes;
    }inline LONG CRegKey::SetKeyValue(LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName)
    {
    ATLASSERT(lpszValue != NULL);
    CRegKey key;
    LONG lRes = key.Create(m_hKey, lpszKeyName);
    if (lRes == ERROR_SUCCESS)
    lRes = key.SetValue(lpszValue, lpszValueName);
    return lRes;
    }inline LONG CRegKey::SetValue(DWORD dwValue, LPCTSTR lpszValueName)
    {
    ATLASSERT(m_hKey != NULL);
    return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_DWORD,
    (BYTE * const)&dwValue, sizeof(DWORD));
    }inline LONG CRegKey::SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName)
    {
    ATLASSERT(lpszValue != NULL);
    ATLASSERT(m_hKey != NULL);
    return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_SZ,
    (BYTE * const)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
    }inline LONG CRegKey::RecurseDeleteKey(LPCTSTR lpszKey)
    {
    CRegKey key;
    LONG lRes = key.Open(m_hKey, lpszKey, KEY_READ | KEY_WRITE);
    if (lRes != ERROR_SUCCESS)
    return lRes;
    FILETIME time;
    DWORD dwSize = 256;
    TCHAR szBuffer[256];
    while (RegEnumKeyEx(key.m_hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL,
    &time)==ERROR_SUCCESS)
    {
    lRes = key.RecurseDeleteKey(szBuffer);
    if (lRes != ERROR_SUCCESS)
    return lRes;
    dwSize = 256;
    }
    key.Close();
    return DeleteSubKey(lpszKey);
    }