c++ 读写注册表

void RegUpdate(HKEY hRootKey,LPCSTR szSubKey,LPCSTR szValueName,LPCSTR szValue)   
{   
   HKEY hKey;   
   LONG rc;   
   int nFile;   
   rc = RegCreateKeyEx(hRootKey, szSubKey, NULL, NULL, 0, KEY_WRITE, NULL, &hKey, NULL);   
   if(rc != ERROR_SUCCESS)
      return;
   nFile = strlen(szValue);   
   RegSetValueEx(hKey, szValueName, NULL, REG_SZ, (LPBYTE)szValue, nFile);   
   RegCloseKey(hKey);
}

string RegGet(HKEY hRootKey,LPCSTR szSubKey,LPCSTR szValueName)   
{   
    HKEY hKey;
    LONG rc; 
    DWORD dwType;
    char szValue[1024];
    DWORD dwSize;
    rc = RegOpenKey(hRootKey, szSubKey, &hKey);   
    if(rc != ERROR_SUCCESS)   
        return FALSE;

    rc = RegQueryValueEx(hKey, szValueName, NULL, &dwType, (LPBYTE)szValue, &dwSize);   
    if(rc == ERROR_SUCCESS && dwType == REG_SZ)
    { 
        RegCloseKey(hKey);   
        return (string)szValue;
    }
    RegCloseKey(hKey);   
    return NULL;
}

 

posted on 2014-02-25 16:58  空明流光  阅读(489)  评论(1编辑  收藏  举报

导航