主题:註冊表問題
我初學VC++ 2005, 下面這段代碼無法讀取註冊表內容,請各位高手幫忙看看問題出在哪里, 為什么無法打開和讀取失敗?輸出結果是Open false read false.
註冊表HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\4.0下
MostRecent值為2007-01-18 類型為REG_SZ
RegisterPlugin myPlug;
LPCTSTR subKey;
LPCTSTR reName;
subKey=(LPCTSTR)"SOFTWARE\\McNeel\\Rhinoceros\\4.0";
if (myPlug.Open(subKey))
cout<<"Open True";
else
cout<<"Open False";
cout<<"\n";
reName=(LPCTSTR)"MostRecent";
CString* lpVal=new CString;
if (myPlug.Read(reName,lpVal))
cout<< "read True";
else
cout<< "read False";
RegisterPlugin類如下:
class RegisterPlugin
{
public:
RegisterPlugin(HKEY hKey=HKEY_LOCAL_MACHINE);
~RegisterPlugin(void);
BOOL Read(LPCTSTR lpValueName, CString* lpVal);
void Close();
BOOL Open(LPCTSTR lpSubKey);
protected:
HKEY m_hKey;
};
RegisterPlugin::RegisterPlugin(HKEY hKey)
{
m_hKey=hKey;;
}
RegisterPlugin::~RegisterPlugin()
{
Close();
}
BOOL RegisterPlugin::Open(LPCTSTR lpSubKey)
{
HKEY hKey;
long lReturn=RegOpenKeyEx(m_hKey,lpSubKey,0L,KEY_ALL_ACCESS,&hKey);
if(lReturn==ERROR_SUCCESS)
{
m_hKey=hKey;
return TRUE;
}
return FALSE;
}
void RegisterPlugin::Close()
{
if(m_hKey)
{
RegCloseKey(m_hKey);
m_hKey=NULL;
}
}
BOOL RegisterPlugin::Read(LPCTSTR lpValueName, CString* lpVal)
{
DWORD dwType;
DWORD dwSize=200;
char szString[2550];
long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)szString,&dwSize);
if(lReturn==ERROR_SUCCESS)
{
*lpVal=szString;
return TRUE;
}
return FALSE;
}
註冊表HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\4.0下
MostRecent值為2007-01-18 類型為REG_SZ
RegisterPlugin myPlug;
LPCTSTR subKey;
LPCTSTR reName;
subKey=(LPCTSTR)"SOFTWARE\\McNeel\\Rhinoceros\\4.0";
if (myPlug.Open(subKey))
cout<<"Open True";
else
cout<<"Open False";
cout<<"\n";
reName=(LPCTSTR)"MostRecent";
CString* lpVal=new CString;
if (myPlug.Read(reName,lpVal))
cout<< "read True";
else
cout<< "read False";
RegisterPlugin類如下:
class RegisterPlugin
{
public:
RegisterPlugin(HKEY hKey=HKEY_LOCAL_MACHINE);
~RegisterPlugin(void);
BOOL Read(LPCTSTR lpValueName, CString* lpVal);
void Close();
BOOL Open(LPCTSTR lpSubKey);
protected:
HKEY m_hKey;
};
RegisterPlugin::RegisterPlugin(HKEY hKey)
{
m_hKey=hKey;;
}
RegisterPlugin::~RegisterPlugin()
{
Close();
}
BOOL RegisterPlugin::Open(LPCTSTR lpSubKey)
{
HKEY hKey;
long lReturn=RegOpenKeyEx(m_hKey,lpSubKey,0L,KEY_ALL_ACCESS,&hKey);
if(lReturn==ERROR_SUCCESS)
{
m_hKey=hKey;
return TRUE;
}
return FALSE;
}
void RegisterPlugin::Close()
{
if(m_hKey)
{
RegCloseKey(m_hKey);
m_hKey=NULL;
}
}
BOOL RegisterPlugin::Read(LPCTSTR lpValueName, CString* lpVal)
{
DWORD dwType;
DWORD dwSize=200;
char szString[2550];
long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)szString,&dwSize);
if(lReturn==ERROR_SUCCESS)
{
*lpVal=szString;
return TRUE;
}
return FALSE;
}