CString pathString;
char subChar[MAX_BUFF_SIZE] = "Software\\netmsg\\" ;
HKEY hKey;

long retLong;
retLong  = ::RegOpenKeyEx(
  HKEY_CURRENT_USER,         // handle to open key
subChar,  // subkey name
0,   // reserved
KEY_ALL_ACCESS, // security access mask
&hKey // handle to open key
);
if (ERROR_SUCCESS != retLong)
{
AfxMessageBox("打开注册表出错1!");
}
    memset(subChar,0,sizeof(subChar));
LPBYTE subLPBYTE  = new BYTE[80];
DWORD  subDword = 80;
retLong = ::RegQueryValueEx(hKey,"ClientSet",NULL,NULL,subLPBYTE,&subDword);
    if (ERROR_SUCCESS != retLong )
{
char subKeyBuffer[MAX_BUFF_SIZE] = "ClientSet";
HKEY subKey;
DWORD dwFunc;
retLong = ::RegCreateKeyEx(hKey,
                             (LPCTSTR)subKeyBuffer,//要创建的子键名字的地址
                             0,//保留值,必须为0
 (LPTSTR)NULL,
 KEY_ENUMERATE_SUB_KEYS,
 KEY_CREATE_SUB_KEY,
 (LPSECURITY_ATTRIBUTES)NULL,
                             &subKey,
                             &dwFunc);
if (ERROR_SUCCESS != retLong)
{
AfxMessageBox("创建子键出错!");
}
// ::RegCloseKey(hKey);// retLong  = ::RegOpenKeyEx(
//  HKEY_CURRENT_USER,         // handle to open key
// subChar,  // subkey name
// 0,   // reserved
// KEY_ALL_ACCESS, // security access mask
// &hKey // handle to open key
// );
// if (ERROR_SUCCESS != retLong)
// {
// AfxMessageBox("打开注册表出错1!");
// }
        LPCTSTR filePath = "e:\\dfg.db";
char subKeyBuffer1[MAX_BUFF_SIZE] = "dbpath";
DWORD cbData = strlen(filePath)+1;
retLong = ::RegSetValueEx(
subKey,
"dbpath", 
0,
REG_SZ,
(LPBYTE)filePath,
cbData);
if (ERROR_SUCCESS != retLong)
{
AfxMessageBox("设置子键值出错2!");
}=================================================
   总是提示   设置子键值出错2 ,RegSetValueEx 运行不成功!

解决方案 »

  1.   

    CString pathString;
    char subChar[MAX_BUFF_SIZE] = "Software\\netmsg\\" ;
    HKEY hKey;

    long retLong;
    retLong  = ::RegOpenKeyEx(
      HKEY_CURRENT_USER,         // handle to open key
    subChar,  // subkey name
    0,   // reserved
    KEY_ALL_ACCESS, // security access mask
    &hKey // handle to open key
    );
    if (ERROR_SUCCESS != retLong)
    {
    AfxMessageBox("打开注册表出错1!");
    }
        memset(subChar,0,sizeof(subChar));
    LPBYTE subLPBYTE  = new BYTE[80];
    DWORD  subDword = 80;
    retLong = ::RegQueryValueEx(hKey,"ClientSet",NULL,NULL,subLPBYTE,&subDword);
        if (ERROR_SUCCESS != retLong )
    {
    char subKeyBuffer[MAX_BUFF_SIZE] = "ClientSet";
    HKEY subKey;
    DWORD dwFunc;
    retLong = ::RegCreateKeyEx(hKey,
                                 (LPCTSTR)subKeyBuffer,//要创建的子键名字的地址
                                 0,//保留值,必须为0
     (LPTSTR)NULL,
     KEY_ENUMERATE_SUB_KEYS,
     KEY_CREATE_SUB_KEY,
     (LPSECURITY_ATTRIBUTES)NULL,
                                 &subKey,
                                 &dwFunc);
    if (ERROR_SUCCESS != retLong)
    {
    AfxMessageBox("创建子键出错!");
    }
    ::RegCloseKey(hKey); retLong  = ::RegOpenKeyEx(
      subKey,         // handle to open key
    "",  // subkey name
    0,   // reserved
    KEY_ALL_ACCESS, // security access mask
    &hKey // handle to open key
    );
    if (ERROR_SUCCESS != retLong)
    {
    AfxMessageBox("打开注册表出错2!");
    }
            LPCTSTR filePath = "e:\\dfg.db";
    char subKeyBuffer1[MAX_BUFF_SIZE] = "dbpath";
    DWORD cbData = strlen(filePath)+1;
    retLong = ::RegSetValueEx(
    subKey,
    "dbpath", 
    0,
    REG_SZ,
    (LPBYTE)filePath,
    cbData);
    if (ERROR_SUCCESS != retLong)
    {
    AfxMessageBox("设置子键值出错2!");
    }
    ==============================================
       加了 OPEN 还是不行呀?