win98/win2000的VC注册表操作区别问题

shanhe 2004-03-31 04:13:51
我用类:
// 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下

敬请赐教
...全文
71 9 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
primen 2004-04-02
恭喜
  • 打赏
  • 举报
回复
shanhe 2004-04-02
问题出在:REG_OPTION_VOLATILE,大家看看msdn上的说明:
RegCreateKeyEx
The RegCreateKeyEx function creates the specified key. If the key already exists in the registry, the function opens it.

LONG RegCreateKeyEx(
HKEY hKey, // handle to an open key
LPCTSTR lpSubKey, // address of subkey name
DWORD Reserved, // reserved
LPTSTR lpClass, // address of class string
DWORD dwOptions, // special options flag
REGSAM samDesired, // desired security access
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
// address of key security structure
PHKEY phkResult, // address of buffer for opened handle
LPDWORD lpdwDisposition // address of disposition value buffer
);

dwOptions
Specifies special options for the key. This parameter can be one of the following values. Value Meaning
REG_OPTION_NON_VOLATILE This key is not volatile; this is the default. The information is stored in a file and is preserved when the system is restarted. The RegSaveKey function saves keys that are not volatile.
REG_OPTION_VOLATILE Windows NT: This key is volatile; the information is stored in memory and is not preserved when the system is restarted. The RegSaveKey function does not save volatile keys. This flag is ignored if the key already exists.
Windows 95: This value is ignored. If REG_OPTION_VOLATILE is specified, the RegCreateKeyEx function creates a nonvolatile key and returns ERROR_SUCCESS.

REG_OPTION_BACKUP_RESTORE Windows NT: If this flag is set, the function ignores the samDesired parameter and attempts to open the key with the access required to backup or restore the key. If the calling thread has the SE_BACKUP_NAME privilege enabled, the key is opened with ACCESS_SYSTEM_SECURITY and KEY_READ access. If the calling thread has the SE_RESTORE_NAME privilege enabled, the key is opened with ACCESS_SYSTEM_SECURITY and KEY_WRITE access. If both privileges are enabled, the key has the combined accesses for both privileges.

注意:
REG_OPTION_VOLATILE Windows NT: This key is volatile; the information is stored in memory and is not preserved when the system is restarted. The RegSaveKey function does not save volatile keys. This flag is ignored if the key already exists.
  • 打赏
  • 举报
回复
shanhe 2004-04-01
to: primen(癞蛤蟆)
写入时,如果不关机或者重新启动,注册表示可以看到写入的内容的,但是重新启动就不同了。我通过regedit手工写入注册表是可以的,但是通过编程方式写入总重新启动后丢失写入的内容,我怀疑是上述类的操作有漏洞,但又找不出,所以请求大家
  • 打赏
  • 举报
回复
tyzyx 2004-04-01
好像98和2000的机制不一样,98每天起来都有备份,可以在dos下打scanreg/restore来恢复前五天的注册表,可是2000到现在我还没有找到类似的命令
  • 打赏
  • 举报
回复
primen 2004-04-01
对于WINDOWS2K,LPSTR要转换成BSTR才可以吧!
  • 打赏
  • 举报
回复
shanhe 2004-04-01
再次UP,发言有奖
  • 打赏
  • 举报
回复
shanhe 2004-04-01
告了几个小时,终于发现了问题,还是自己回答吧,这个类有漏洞,我整理后提交出来。有人指出后我加分.
  • 打赏
  • 举报
回复
看晕了,帮你up
  • 打赏
  • 举报
回复
shanhe 2004-03-31
自己up
  • 打赏
  • 举报
回复
发帖
VC/MFC

1.6w+

社区成员

VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
帖子事件
创建了帖子
2004-03-31 04:13
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……