9 个解决方案
#1
在此小弟不胜感激。谢谢了
#3
// MyRegistry.h: interface for the CMyRegistry class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYREGISTRY_H__D7B38CC5_5955_4121_9856_AF86F724ED50__INCLUDED_)
#define AFX_MYREGISTRY_H__D7B38CC5_5955_4121_9856_AF86F724ED50__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMyRegistry: public CObject
{
public:
CMyRegistry(HKEY hRootKey = HKEY_LOCAL_MACHINE);//构造函数带有默认参数
virtual ~CMyRegistry();
BOOL VerifyKey (LPCTSTR pszPath);
BOOL VerifyValue (LPCTSTR pszValue);
BOOL CreateKey (LPCTSTR pszPath);
void Close();
BOOL DeleteValue (LPCTSTR pszValue);
BOOL DeleteKey (LPCTSTR pszPath);
BOOL Write (LPCTSTR pszKey, int iVal);
BOOL Write (LPCTSTR pszKey, DWORD dwVal);
BOOL Write (LPCTSTR pszKey, LPCTSTR pszVal);
BOOL Read (LPCTSTR pszKey, int& iVal);
BOOL Read (LPCTSTR pszKey, DWORD& dwVal);
BOOL Read (LPCTSTR pszKey, CString& sVal);
BOOL IsEqual(LPCTSTR pszValue,int nn);
BOOL IsEqual(LPCTSTR pszValue,DWORD dw);
BOOL IsEqual(LPCTSTR pszValue,LPCTSTR lpsz);
protected:
HKEY m_hSubKey; //保存打开的子键句柄
HKEY m_hRootKey; //保存根键句柄
};
#endif // !defined(AFX_MYREGISTRY_H__D7B38CC5_5955_4121_9856_AF86F724ED50__INCLUDED_)
#4
// MyRegistry.cpp: implementation of the CMyRegistry class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyRegExample.h"
#include "MyRegistry.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyRegistry::CMyRegistry(HKEY hRootKey)
{
m_hRootKey = hRootKey;
}
CMyRegistry::~CMyRegistry()//在析构函数中关闭打开注册表句柄
{
Close();
}
/*================================================================
* 函数名: VerifyKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 判断给定的路径是否存在 (兼有打开的功能)
如果第一个参数为NULL,则使用默认的根键。
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::VerifyKey (LPCTSTR pszPath)
{
ASSERT (pszPath);
LONG ReturnValue = ::RegOpenKeyEx (m_hRootKey, pszPath, 0L,KEY_ALL_ACCESS, &m_hSubKey);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: VerifyValue
* 参数: (LPCTSTR pszValue)
* 功能描述: 判断给定的值是否存在 (请先调用VerifyKey,然后在使用该函数)
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::VerifyValue (LPCTSTR pszValue)
{
ASSERT(m_hSubKey);
LONG lReturn = ::RegQueryValueEx(m_hSubKey, pszValue, NULL,
NULL, NULL, NULL);
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: CreateKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 创建路径
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::CreateKey (LPCTSTR pszPath)
{
DWORD dw;
LONG ReturnValue = ::RegCreateKeyEx (m_hRootKey, pszPath, 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
&m_hSubKey, &dw);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, int iVal)
* 功能描述: 写入整型值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Write (LPCTSTR lpszKeyName, int iVal)
{
DWORD dwValue;
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
dwValue = (DWORD)iVal;
LONG ReturnValue = ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_DWORD,
(CONST BYTE*) &dwValue, sizeof(DWORD));
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, DWORD dwVal)
* 功能描述: 写入DWORD值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Write (LPCTSTR lpszKeyName, DWORD dwVal)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
return ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_DWORD,
(CONST BYTE*) &dwVal, sizeof(DWORD));
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, LPCTSTR pszData)
* 功能描述: 写入字符串值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Write (LPCTSTR lpszKeyName, LPCTSTR pszData)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
ASSERT(pszData);
ASSERT(AfxIsValidAddress(pszData, strlen(pszData), FALSE));
LONG ReturnValue = ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_SZ,
(CONST BYTE*) pszData, strlen(pszData) + 1);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, int& iVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取整数
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Read(LPCTSTR lpszKeyName, int& iVal)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
iVal = (int)dwDest;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, DWORD& dwVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取DWORD值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Read (LPCTSTR lpszKeyName, DWORD& dwVal)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
dwVal = dwDest;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, CString& sVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取字符串值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Read (LPCTSTR lpszKeyName, CString& sVal)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
DWORD dwType;
DWORD dwSize = 200;
char szString[255];
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,&dwType, (BYTE *) szString, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
sVal = szString;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: DeleteValue
* 参数: (LPCTSTR pszValue)
* 功能描述: 删除值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::DeleteValue (LPCTSTR pszValue)
{
ASSERT(m_hSubKey);
if(::RegDeleteValue(m_hSubKey, pszValue)== ERROR_SUCCESS)
return TRUE;
else
return FALSE;
}
/*================================================================
* 函数名: DeleteKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 删除路径
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::DeleteKey (LPCTSTR pszPath)
{
ASSERT(pszPath);
if(::RegDeleteKey(m_hRootKey, pszPath) == ERROR_SUCCESS)
return TRUE;
else
return FALSE;
}
/*================================================================
* 函数名: Close
* 参数:
* 功能描述: 关闭注册表
* 返回值: void
================================================================*/
void CMyRegistry::Close()
{
if (m_hSubKey)
{
::RegCloseKey (m_hSubKey);
m_hSubKey = NULL;
}
}
#5
谢谢各位的帮忙小弟拜谢了
#6
这段代码怎么用????????????????????
#7
4楼的答案很好呀!
偶就不发了
偶就不发了
#8
我没用过VC++....................
#9
4楼的能不能把MyRegExample.h这个文件拿过来一下
#1
在此小弟不胜感激。谢谢了
#2
#3
// MyRegistry.h: interface for the CMyRegistry class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYREGISTRY_H__D7B38CC5_5955_4121_9856_AF86F724ED50__INCLUDED_)
#define AFX_MYREGISTRY_H__D7B38CC5_5955_4121_9856_AF86F724ED50__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMyRegistry: public CObject
{
public:
CMyRegistry(HKEY hRootKey = HKEY_LOCAL_MACHINE);//构造函数带有默认参数
virtual ~CMyRegistry();
BOOL VerifyKey (LPCTSTR pszPath);
BOOL VerifyValue (LPCTSTR pszValue);
BOOL CreateKey (LPCTSTR pszPath);
void Close();
BOOL DeleteValue (LPCTSTR pszValue);
BOOL DeleteKey (LPCTSTR pszPath);
BOOL Write (LPCTSTR pszKey, int iVal);
BOOL Write (LPCTSTR pszKey, DWORD dwVal);
BOOL Write (LPCTSTR pszKey, LPCTSTR pszVal);
BOOL Read (LPCTSTR pszKey, int& iVal);
BOOL Read (LPCTSTR pszKey, DWORD& dwVal);
BOOL Read (LPCTSTR pszKey, CString& sVal);
BOOL IsEqual(LPCTSTR pszValue,int nn);
BOOL IsEqual(LPCTSTR pszValue,DWORD dw);
BOOL IsEqual(LPCTSTR pszValue,LPCTSTR lpsz);
protected:
HKEY m_hSubKey; //保存打开的子键句柄
HKEY m_hRootKey; //保存根键句柄
};
#endif // !defined(AFX_MYREGISTRY_H__D7B38CC5_5955_4121_9856_AF86F724ED50__INCLUDED_)
#4
// MyRegistry.cpp: implementation of the CMyRegistry class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyRegExample.h"
#include "MyRegistry.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyRegistry::CMyRegistry(HKEY hRootKey)
{
m_hRootKey = hRootKey;
}
CMyRegistry::~CMyRegistry()//在析构函数中关闭打开注册表句柄
{
Close();
}
/*================================================================
* 函数名: VerifyKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 判断给定的路径是否存在 (兼有打开的功能)
如果第一个参数为NULL,则使用默认的根键。
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::VerifyKey (LPCTSTR pszPath)
{
ASSERT (pszPath);
LONG ReturnValue = ::RegOpenKeyEx (m_hRootKey, pszPath, 0L,KEY_ALL_ACCESS, &m_hSubKey);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: VerifyValue
* 参数: (LPCTSTR pszValue)
* 功能描述: 判断给定的值是否存在 (请先调用VerifyKey,然后在使用该函数)
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::VerifyValue (LPCTSTR pszValue)
{
ASSERT(m_hSubKey);
LONG lReturn = ::RegQueryValueEx(m_hSubKey, pszValue, NULL,
NULL, NULL, NULL);
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: CreateKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 创建路径
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::CreateKey (LPCTSTR pszPath)
{
DWORD dw;
LONG ReturnValue = ::RegCreateKeyEx (m_hRootKey, pszPath, 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
&m_hSubKey, &dw);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, int iVal)
* 功能描述: 写入整型值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Write (LPCTSTR lpszKeyName, int iVal)
{
DWORD dwValue;
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
dwValue = (DWORD)iVal;
LONG ReturnValue = ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_DWORD,
(CONST BYTE*) &dwValue, sizeof(DWORD));
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, DWORD dwVal)
* 功能描述: 写入DWORD值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Write (LPCTSTR lpszKeyName, DWORD dwVal)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
return ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_DWORD,
(CONST BYTE*) &dwVal, sizeof(DWORD));
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, LPCTSTR pszData)
* 功能描述: 写入字符串值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Write (LPCTSTR lpszKeyName, LPCTSTR pszData)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
ASSERT(pszData);
ASSERT(AfxIsValidAddress(pszData, strlen(pszData), FALSE));
LONG ReturnValue = ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_SZ,
(CONST BYTE*) pszData, strlen(pszData) + 1);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, int& iVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取整数
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Read(LPCTSTR lpszKeyName, int& iVal)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
iVal = (int)dwDest;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, DWORD& dwVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取DWORD值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Read (LPCTSTR lpszKeyName, DWORD& dwVal)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
dwVal = dwDest;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, CString& sVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取字符串值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::Read (LPCTSTR lpszKeyName, CString& sVal)
{
ASSERT(m_hSubKey);
ASSERT(lpszKeyName);
DWORD dwType;
DWORD dwSize = 200;
char szString[255];
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,&dwType, (BYTE *) szString, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
sVal = szString;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: DeleteValue
* 参数: (LPCTSTR pszValue)
* 功能描述: 删除值
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::DeleteValue (LPCTSTR pszValue)
{
ASSERT(m_hSubKey);
if(::RegDeleteValue(m_hSubKey, pszValue)== ERROR_SUCCESS)
return TRUE;
else
return FALSE;
}
/*================================================================
* 函数名: DeleteKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 删除路径
* 返回值: BOOL
================================================================*/
BOOL CMyRegistry::DeleteKey (LPCTSTR pszPath)
{
ASSERT(pszPath);
if(::RegDeleteKey(m_hRootKey, pszPath) == ERROR_SUCCESS)
return TRUE;
else
return FALSE;
}
/*================================================================
* 函数名: Close
* 参数:
* 功能描述: 关闭注册表
* 返回值: void
================================================================*/
void CMyRegistry::Close()
{
if (m_hSubKey)
{
::RegCloseKey (m_hSubKey);
m_hSubKey = NULL;
}
}
#5
谢谢各位的帮忙小弟拜谢了
#6
这段代码怎么用????????????????????
#7
4楼的答案很好呀!
偶就不发了
偶就不发了
#8
我没用过VC++....................
#9
4楼的能不能把MyRegExample.h这个文件拿过来一下