shanhe 广州中同信息科技有限公司 技术总监  2004年03月31日
win98/win2000的VC注册表操作区别问题
我用类:
// Registry.cpp : implementation file
//

#include "stdafx.h"
#include "Registry.h"

/////////////////////////////////////////////////////////////////////////////
// CRegistry

CRegistry::CRegistry(HKEY hKey)
{
m_hKey=hKey;
}

CRegistry::~CRegistry()
{
Close();
}

/////////////////////////////////////////////////////////////////////////////
// CRegistry Functions

BOOL CRegistry::CreateKey(LPCTSTR lpSubKey)
{
ASSERT(m_hKey);
ASSERT(lpSubKey);

HKEY hKey;
DWORD dw;
long lReturn=RegCreateKeyEx(m_hKey,lpSubKey,0L,NULL,REG_OPTION_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dw);

if(lReturn==ERROR_SUCCESS)
{
m_hKey=hKey;
return TRUE;
}

return FALSE;

}

BOOL CRegistry::Open(LPCTSTR lpSubKey)
{
ASSERT(m_hKey);
ASSERT(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 CRegistry::Close()
{
if(m_hKey)
{
::RegCloseKey(m_hKey);
::RegFlushKey( m_hKey);
m_hKey=NULL;
}

}

BOOL CRegistry::DeleteValue(LPCTSTR lpValueName)
{
ASSERT(m_hKey);
ASSERT(lpValueName);

long lReturn=RegDeleteValue(m_hKey,lpValueName);

if(lReturn==ERROR_SUCCESS)
return TRUE;
return FALSE;

}

BOOL CRegistry::DeleteKey(HKEY hKey, LPCTSTR lpSubKey)
{
ASSERT(hKey);
ASSERT(lpSubKey);

long lReturn=RegDeleteValue(hKey,lpSubKey);

if(lReturn==ERROR_SUCCESS)
return TRUE;
return FALSE;

}

BOOL CRegistry::Write(LPCTSTR lpSubKey, int nVal)
{
ASSERT(m_hKey);
ASSERT(lpSubKey);

DWORD dwValue;
dwValue=(DWORD)nVal;

long lReturn=RegSetValueEx(m_hKey,lpSubKey,0L,REG_DWORD,(const BYTE *) &dwValue,sizeof(DWORD));

if(lReturn==ERROR_SUCCESS)
return TRUE;

return FALSE;

}

BOOL CRegistry::Write(LPCTSTR lpSubKey, DWORD dwVal)
{
ASSERT(m_hKey);
ASSERT(lpSubKey);

long lReturn=RegSetValueEx(m_hKey,lpSubKey,0L,REG_DWORD,(const BYTE *) &dwVal,sizeof(DWORD));

if(lReturn==ERROR_SUCCESS)
return TRUE;

return FALSE;

}

BOOL CRegistry::Write(LPCTSTR lpValueName, LPCTSTR lpValue)
{
ASSERT(m_hKey);
ASSERT(lpValueName);
ASSERT(lpValue);

long lReturn=RegSetValueEx(m_hKey,lpValueName,0L,REG_SZ,(const BYTE *) lpValue,strlen(lpValue)+1);

if(lReturn==ERROR_SUCCESS)
return TRUE;

return FALSE;

}


BOOL CRegistry::Read(LPCTSTR lpValueName, int* pnVal)
{
ASSERT(m_hKey);
ASSERT(lpValueName);
ASSERT(pnVal);

DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD dwDest;
long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)&dwDest,&dwSize);

if(lReturn==ERROR_SUCCESS)
{
*pnVal=(int)dwDest;
return TRUE;
}
return FALSE;

}

BOOL CRegistry::Read(LPCTSTR lpValueName, DWORD* pdwVal)
{
ASSERT(m_hKey);
ASSERT(lpValueName);
ASSERT(pdwVal);

DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD dwDest;
long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)&dwDest,&dwSize);

if(lReturn==ERROR_SUCCESS)
{
*pdwVal=dwDest;
return TRUE;
}
return FALSE;

}




BOOL CRegistry::RestoreKey(LPCTSTR lpFileName)
{
ASSERT(m_hKey);
ASSERT(lpFileName);

long lReturn=RegRestoreKey(m_hKey,lpFileName,REG_WHOLE_HIVE_VOLATILE);

if(lReturn==ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistry::SaveKey(LPCTSTR lpFileName)
{
ASSERT(m_hKey);
ASSERT(lpFileName);

long lReturn=RegSaveKey(m_hKey,lpFileName,NULL);

if(lReturn==ERROR_SUCCESS)
return TRUE;

return FALSE;
}


BOOL CRegistry::Read(LPCTSTR lpValueName, LPCTSTR lpVal)
{
ASSERT(m_hKey);
ASSERT(lpValueName);
ASSERT(lpVal);

DWORD dwType;
DWORD dwSize=200;
// LPCTSTR szString=new char[255];
//memset(szString,0,sizeof(szString));

long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)lpVal,&dwSize);

if(lReturn==ERROR_SUCCESS)
{
// lpVal=szString;
//strcpy(lpVal, szString);
return TRUE;
}
return FALSE;

}
写注册表,发现在98下重新启动后写的注册表项目OK,但是在win2000下却出现问题,重新启动后,通过程序写的注册表项不见了。

我写在HKEY_CURRENT_USER下

敬请赐教
...全文
44 点赞 收藏 9
写回复
9 条回复

还没有回复,快来抢沙发~

发动态
发帖子
VC/MFC
创建于2007-09-28

7889

社区成员

42.1w+

社区内容

VC/MFC相关问题讨论
社区公告
暂无公告