我用RegOpenKeyEx()进行判断,可是每次判断都返回不存在,即使注册表中那一项已经存在了也是如此。
请问为什么?
我的代码:
HKEY hKey;
long ret;
LPTSTR RegPath = "\\Software\\DLUT_ISUI\\VVTalk\\Setting\\RunOnce";
ret = RegOpenKeyEx(HKEY_CURRENT_USER,RegPath,0,KEY_READ,&hKey);
if (ret != ERROR_SUCCESS)
{
m_strMyName = "Chris";
m_strIP = "127.0.0.1";
theApp.WriteProfileString(_T("Setting"),_T("MyName"),m_strMyName);
theApp.WriteProfileString(_T("Setting"),_T("OppIP"),m_strIP);
theApp.WriteProfileInt(_T("Setting"),_T("RunOnce"),1);
}
else
{
m_strMyName = theApp.GetProfileString(_T("Setting"),_T("MyName"));
m_strIP = theApp.GetProfileString(_T("Setting"),_T("OppIP"));
}

解决方案 »

  1.   

    是RegOpenKeyEx()中权限的问题吗?
    不解
      

  2.   

    If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.
      

  3.   

    你调试运行看一下ret的值是多少
      

  4.   

    仔细看了下,你的编码是不是UNICODE?你改下试试。
    LPCTSTR RegPath = L"\\Software\\DLUT_ISUI\\VVTalk\\Setting\\RunOnce";
      

  5.   

    ret的值是161
    那个FORMAT_MESSAGE_FROM_SYSTEM怎么使用啊?
    我用的字符集是MBCS
      

  6.   

    这样改出错 LPCTSTR没问题 L出错,字符集的原因吧,要是改成UNICODE,别的地方有很多ERROR
      

  7.   

    ret == 161
    #define ERROR_BAD_PATHNAME               161L
    果然是路径有问题
    可是我没写错啊
      

  8.   

    LPTSTR RegPath = "Software\\DLUT_ISUI\\VVTalk\\Setting\\RunOnce";前面多了两个\\
      

  9.   

    终于有人说话了,不好意思,刚才系统不让我发帖,一个人最多连续三贴
    那个是我贴错代码了,不好意思,实际上没有“\\”
    没有“\\”时,ret也不是161,而是2:
    #define ERROR_FILE_NOT_FOUND     2L
    我后来改成这样就好用了,不知道为什么:
    LPTSTR RegPath = "Software\\DLUT_ISUI\\VVTalk\\Setting\\/*RunOnce*/";
    将RunOnce即keyname去掉就好用了
    MSDN里的例子是这样的:
             lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                TEXT("SYSTEM\\CurrentControlSet\\Control\\"
                     "ProductOptions"), 0, KEY_QUERY_VALUE, &hKey );
             if( lRet != ERROR_SUCCESS )
                return FALSE;不太明白,为什么要把TEXT()里的内容分开,我仿照他那样做还是不行
      

  10.   

    HKEY hKey;
                char szProductType[80];
                DWORD dwBufLen;            RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                   "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
                   0, KEY_QUERY_VALUE, &hKey );
                RegQueryValueEx( hKey, "ProductType", NULL, NULL,
                   (LPBYTE) szProductType, &dwBufLen);
                RegCloseKey( hKey );
    这是MSDN里标准的查询SYSTEMHKEY_LOCAL_MACHINE\CurrentControlSet\Control\ProductOptions这个键值下ProductType这个数据的代码,楼主只要模仿即可。
      

  11.   

    模仿了,不行啊,error,但是不写后一个""中的内容就可以