⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regkey.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************
//
//  Copyright (c) 1993,  Microsoft Corp.
//
//  File:  DOMDLGS.H
//
//  Implementation file for registry management classes
//
//  History:
//      Scott V. Walker 10/5/94
//
//****************************************************************************

#include "stdafx.h"

#include "portable.h"

#include "regkey.h"


BOOL g_bLostConnection = FALSE;


//****************************************************************************
//
//  CRegistryValue Implementation
//
//****************************************************************************

IMPLEMENT_DYNAMIC(CRegistryValue, CObject)

//****************************************************************************
//
//  CRegistryValue::CRegistryValue
//
//****************************************************************************
CRegistryValue::CRegistryValue()
{
    m_dwType = REG_NONE;
    m_dwDataLength = 0;
    m_pData = NULL;
}

//****************************************************************************
//
//  CRegistryValue::CRegistryValue
//
//****************************************************************************
CRegistryValue::CRegistryValue(LPCTSTR pszName, DWORD dwType,
    DWORD dwDataLength, LPBYTE pData)
{
    Set(pszName, dwType, dwDataLength, pData);
}

//****************************************************************************
//
//  CRegistryValue::~CRegistryValue
//
//****************************************************************************
CRegistryValue::~CRegistryValue()
{
    Empty();
}

//****************************************************************************
//
//  CRegistryValue::Set
//
//  Sets the value data fields.  The data pointed to by pData is COPIED!
//
//****************************************************************************
void CRegistryValue::Set(LPCTSTR pszName, DWORD dwType,
    DWORD dwDataLength, LPBYTE pData)
{
    Empty();

    m_sName = pszName;
    m_dwType = dwType;
    m_dwDataLength = dwDataLength;
    if (dwDataLength == 0 || pData == NULL)
        m_pData = NULL;
    else
    {
        m_pData = new BYTE[dwDataLength];
        memcpy(m_pData, pData, dwDataLength);
    }
}

//****************************************************************************
//
//  CRegistryValue::Get
//
//  Gets the value data fields.  The data pointed to by m_pData is COPIED
//  into the buffer pointed to by pData... this buffer better be big enough!
//  If pData is NULL, no copy is performed.
//
//****************************************************************************
void CRegistryValue::Get(CString &sName, DWORD &dwType,
    DWORD &dwDataLength, LPBYTE pData)
{
    sName = m_sName;
    dwType = m_dwType;
    dwDataLength = m_dwDataLength;
    if (dwDataLength != 0 && pData != NULL)
        memcpy(pData, m_pData, m_dwDataLength);
}

//****************************************************************************
//
//  CRegistryValue::Empty
//
//  Clear the value data and deletes its data buffer.
//
//****************************************************************************
void CRegistryValue::Empty()
{
    m_sName.Empty();
    m_dwType = REG_NONE;
    m_dwDataLength = 0;
    if (m_pData != NULL)
        delete m_pData;
    m_pData = NULL;
}

//****************************************************************************
//
//  CRegistryValue::operator=
//
//  Assignment operator.  Copies CRegistryValue object.
//
//****************************************************************************
const CRegistryValue& CRegistryValue::operator=(CRegistryValue &other)
{
    Set(other.m_sName, other.m_dwType, other.m_dwDataLength, other.m_pData);

    return *this;
}

//****************************************************************************
//
//  CRegistryKey Implementation
//
//****************************************************************************

IMPLEMENT_DYNAMIC(CRegistryKey, CObject)

//****************************************************************************
//
//  CRegistryKey::CRegistryKey
//
//****************************************************************************
CRegistryKey::CRegistryKey()
{
    // The lost connection status is initialized only once so that if a connection
    // is ever lost we won't waste any time trying to close keys.

    Initialize();
}

//****************************************************************************
//
//  CRegistryKey::Initialize
//
//****************************************************************************
void CRegistryKey::Initialize()
{
    m_bConnected = FALSE;
    m_bOpen = FALSE;
    m_bLocal = TRUE;
    m_bDirty = FALSE;

    m_hkeyConnect = NULL;
    m_hkeyRemote = NULL;
    m_hkeyOpen = NULL;
    m_Sam = 0;

    m_dwSubKeys = 0;
    m_dwMaxSubKey = 0;
    m_dwMaxClass = 0;
    m_dwValues = 0;
    m_dwMaxValueName = 0;
    m_dwMaxValueData = 0;
    m_dwSecurityDescriptor = 0;

    m_ftLastWriteTime.dwLowDateTime = 0;
    m_ftLastWriteTime.dwHighDateTime = 0;

    m_lResult = ERROR_SUCCESS;
}

//****************************************************************************
//
//  CRegistryKey::~CRegistryKey
//
//  Destructor.
//
//****************************************************************************
CRegistryKey::~CRegistryKey()
{
    if (g_bLostConnection) {
        // If we lost the registry connection, it will be useless to do anything.
        return;
    }

    // If we're currently open, then close.
    if (m_bOpen)
        Close(TRUE);

    // If we're currently connected, then disconnect.
    if (m_bConnected)
        Disconnect(TRUE);
}

//****************************************************************************
//
//  CRegistryKey::Connect
//
//****************************************************************************
LONG CRegistryKey::Connect(LPCTSTR pszComputer, HKEY hkey)
{
    if (g_bLostConnection) {
        return RPC_S_SERVER_UNAVAILABLE;
    }

    TCHAR szName[MAX_COMPUTERNAME_LENGTH + 1];
    CString sComputer;
    HKEY hkeyRemote;
    DWORD dwNumChars;

    m_lResult = ERROR_SUCCESS;

    if (m_bConnected)
    {
        m_lResult = Disconnect(TRUE);
        if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
            g_bLostConnection = TRUE;
        }
        if (m_lResult != ERROR_SUCCESS)
            return m_lResult;
    }

    // Is this the local machine?

    dwNumChars = MAX_COMPUTERNAME_LENGTH + 1;

    sComputer = pszComputer;
    GetComputerName(szName, &dwNumChars);
    if (sComputer.IsEmpty() || !lstrcmpi(pszComputer, szName))
    {
        // Local

        m_bLocal = TRUE;
        hkeyRemote = NULL;
    }
    else
    {
        // Remote

        m_bLocal = FALSE;
        m_lResult = RegConnectRegistry(pszComputer, hkey, &hkeyRemote);

        if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
            g_bLostConnection = TRUE;
        }
        if (m_lResult != ERROR_SUCCESS)
            return m_lResult;
        lstrcpy(szName, pszComputer);
    }

    m_bConnected = TRUE;
    m_hkeyConnect = hkey;
    m_hkeyRemote = hkeyRemote;
    m_sComputer = szName;

    return ERROR_SUCCESS;
}

//****************************************************************************
//
//  CRegistryKey::Disconnect
//
//****************************************************************************
LONG CRegistryKey::Disconnect(BOOL bForce)
{
    m_lResult = ERROR_SUCCESS;

    if (m_bConnected)
    {
        // Close the open key
        if (m_bOpen)
        {
            m_lResult = Close(bForce);
            if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
                g_bLostConnection = TRUE;                
            }

            if (!bForce && m_lResult != ERROR_SUCCESS)
                return m_lResult;
        }

        // Close the remote connection
        if (!g_bLostConnection) {
            if (!m_bLocal)
            {
                m_lResult = RegCloseKey(m_hkeyRemote);
                if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
                    g_bLostConnection = TRUE;
                }
                if (!bForce && m_lResult != ERROR_SUCCESS)
                    return m_lResult;
            }
        }
    }
    


    
    Initialize();

    return ERROR_SUCCESS;
}

//****************************************************************************
//
//  CRegistryKey::Create
//
//****************************************************************************
LONG CRegistryKey::Create(LPCTSTR pszKeyName, DWORD &dwDisposition,
    LPCTSTR pszClass, REGSAM samDesired, LPSECURITY_ATTRIBUTES lpSecAttr)
{
    if (g_bLostConnection) {
        return RPC_S_SERVER_UNAVAILABLE;
    }
    
    HKEY hkeyOpen, hkey;

    m_lResult = ERROR_SUCCESS;
    dwDisposition = 0;

    if (m_bOpen)
    {
        m_lResult = Close(TRUE);
        if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
            g_bLostConnection = TRUE;
        }
        if (m_lResult != ERROR_SUCCESS)
            return m_lResult;
    }

    // If not connected, default to \\Local_Machine\HKEY_LOCAL_MACHINE
    if (!m_bConnected)
    {
        m_lResult = Connect();
        if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
            g_bLostConnection = TRUE;
        }
        if (m_lResult != ERROR_SUCCESS)
            return m_lResult;
    }

    // Attempt to create the specified subkey
    if (m_bLocal)
        hkey = m_hkeyConnect;
    else
        hkey = m_hkeyRemote;

    m_lResult = RegCreateKeyEx(hkey, pszKeyName, 0, (LPTSTR)pszClass,
        REG_OPTION_NON_VOLATILE, samDesired, lpSecAttr, &hkeyOpen,
        &dwDisposition);
    if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
        g_bLostConnection = TRUE;
    }
    if (m_lResult != ERROR_SUCCESS)
        return m_lResult;

    m_lResult = RegCloseKey(hkeyOpen);
    if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
        g_bLostConnection = TRUE;
        return m_lResult;
    }

    return Open(pszKeyName, samDesired);
}

//****************************************************************************
//
//  CRegistryKey::Open
//
//****************************************************************************
LONG CRegistryKey::Open(LPCTSTR pszKeyName, REGSAM samDesired)
{
    if (g_bLostConnection) {
        return RPC_S_SERVER_UNAVAILABLE;
    }


    HKEY hkeyOpen, hkey;
    CString sWork;
    int nPos;

    m_lResult = ERROR_SUCCESS;

    if (m_bOpen)
    {
        m_lResult = Close(TRUE);
        if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
            g_bLostConnection = TRUE;
        }
        if (m_lResult != ERROR_SUCCESS)
            return m_lResult;
    }

    // If not connected, default to \\Local_Machine\HKEY_LOCAL_MACHINE
    if (!m_bConnected)
    {
        m_lResult = Connect();
        if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
            g_bLostConnection = TRUE;
        }
        if (m_lResult != ERROR_SUCCESS)
            return m_lResult;
    }

    // Attempt to open the specified subkey
    if (m_bLocal)
        hkey = m_hkeyConnect;
    else
        hkey = m_hkeyRemote;
    m_lResult = RegOpenKeyEx(hkey, pszKeyName, 0, samDesired, &hkeyOpen);
    if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
        g_bLostConnection = TRUE;
    }
    if (m_lResult != ERROR_SUCCESS)
        return m_lResult;

    // Attempt to get info about this key.

    TCHAR szBuffer[1024 + 1];
    DWORD dwClass, dwSubKeys, dwMaxSubKey, dwMaxClass, dwValues;
    DWORD dwMaxValueName, dwMaxValueData, dwSecurityDescriptor;
    FILETIME ftLastWriteTime;

    dwClass = 1024 + 1;
    m_lResult = RegQueryInfoKey(hkeyOpen, szBuffer, &dwClass, 0, &dwSubKeys, 
        &dwMaxSubKey, &dwMaxClass, &dwValues, &dwMaxValueName,
        &dwMaxValueData, &dwSecurityDescriptor, &ftLastWriteTime);
    
    if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
        g_bLostConnection = TRUE;
        return m_lResult;
    }

    if (m_lResult != ERROR_SUCCESS)
    {
        RegCloseKey(hkeyOpen);
        return m_lResult;
    }

    // Success! save all the data.

    m_sFullName = pszKeyName;
    nPos = m_sFullName.ReverseFind('\\');
    if (nPos >= 0)
        m_sKeyName = m_sFullName.Mid(nPos + 1);
    else
        m_sKeyName = m_sFullName;

    m_hkeyOpen = hkeyOpen;
    m_bOpen = TRUE;
    m_Sam = samDesired;
    m_sClass = szBuffer;
    m_dwSubKeys = dwSubKeys;
    m_dwMaxSubKey = dwMaxSubKey;
    m_dwMaxClass = dwMaxClass;
    m_dwValues = dwValues;
    m_dwMaxValueName = dwMaxValueName;
    m_dwMaxValueData = dwMaxValueData;
    m_dwSecurityDescriptor = dwSecurityDescriptor;
    m_ftLastWriteTime = ftLastWriteTime;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -