码迷,mamicode.com
首页 > 其他好文 > 详细

查询、创建、设置注册表键值的示例代码

时间:2014-04-30 22:12:40      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:注册表   查询   设置   创建   

示例代码将在注册表位置:HKEY_CURRENT_USER\Software\  读写键值

bool LicenseManage::OpenRegKey(HKEY& hRetKey)
{
    if (ERROR_SUCCESS == RegOpenKey(HKEY_CURRENT_USER,"Software", &hRetKey))
    {
        return true;
    }
    return false;
}
bool LicenseManage::CreateRegKey(string strSubKey, string strValueName, string strValue)
{
    HKEY hKey;
    HKEY hSubKey;
    if (OpenRegKey(hKey))
    {
        // 创建键
        RegCreateKey(hKey,strSubKey.c_str(), &hSubKey);
        // 设置键值
        if( ERROR_SUCCESS != RegSetValueEx(hSubKey,strValueName.c_str(),0,REG_SZ,(CONST BYTE *)strValue.c_str(),strValue.size()+1))
        {
            return false;
        }
        RegCloseKey(hKey) ; //关闭注册表
        return true;
    }
    return false;
}

bool LicenseManage::QueryRegKey(string strSubKey, string strValueName, string& strValue)
{
    DWORD dwType= 1;//定义数据类型
    DWORD dwLen = MAX_PATH;
    char data[MAX_PATH];
    memset(data,0,sizeof(data));
    HKEY hKey;
    HKEY hSubKey;
    if (OpenRegKey(hKey))
    {
        string strTempKey = "Software\\"+strSubKey;
        if (ERROR_SUCCESS == RegOpenKey(HKEY_CURRENT_USER,strTempKey.c_str(), &hSubKey))
        {
            if (ERROR_SUCCESS == RegQueryValueEx(hSubKey,strValueName.c_str(),0,&dwType,(LPBYTE)data,&dwLen))
            {
                strValue = data;
                RegCloseKey(hKey) ; //关闭注册表
                return true;
            }
        }
        RegCloseKey(hKey) ; //关闭注册表
    }
    return false;
}
bool LicenseManage::SetRegKey(string strSubKey, string strValueName, string strValue)
{
    HKEY hKey;
    HKEY hSubKey;
    if (OpenRegKey(hKey))
    {
        string strTempKey = "Software\\"+strSubKey;
        if (ERROR_SUCCESS == RegOpenKey(HKEY_CURRENT_USER,strTempKey.c_str(), &hSubKey))
        {
            if (ERROR_SUCCESS == RegSetValueEx(hSubKey,strValueName.c_str(),0,REG_SZ,(LPBYTE)strValue.c_str(),strValue.size()))
            {
                RegCloseKey(hKey) ; //关闭注册表
                return true;
            }
        }
        RegCloseKey(hKey) ; //关闭注册表
    }
    return false;
}


查询、创建、设置注册表键值的示例代码

标签:注册表   查询   设置   创建   

原文地址:http://blog.csdn.net/timothy721/article/details/24714797

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!