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

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

示例代码将在注册表位置: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 = http://www.mamicode.com/data;>

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