我用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"));
}
请问为什么?
我的代码:
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"));
}
不解
LPCTSTR RegPath = L"\\Software\\DLUT_ISUI\\VVTalk\\Setting\\RunOnce";
那个FORMAT_MESSAGE_FROM_SYSTEM怎么使用啊?
我用的字符集是MBCS
#define ERROR_BAD_PATHNAME 161L
果然是路径有问题
可是我没写错啊
那个是我贴错代码了,不好意思,实际上没有“\\”
没有“\\”时,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()里的内容分开,我仿照他那样做还是不行
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这个数据的代码,楼主只要模仿即可。