如何访问指定的注册表主键及子键?

无聊客 2001-10-31 08:46:30
我想实现以下目标:
A程序 在XXXXXXX目录下的A主键下建立B子键,设立键值
B程序 从上述地址的子键B中读取键值
请问要如何实现呢?具体函数是什么?
...全文
174 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsd198 2001-10-31
  • 打赏
  • 举报
回复
MFC不是有CRegKey类么,好像完全能完成你的工作了,不用API也可以
无聊客 2001-10-31
  • 打赏
  • 举报
回复
谢谢,再次感谢
fly_pig 2001-10-31
  • 打赏
  • 举报
回复
像上面那个CRegistry就是封装了那几个注册表API的包容类,MFC就是这样干的
jason802 2001-10-31
  • 打赏
  • 举报
回复
API是直接用,就像使用一句普通的C语句.
jason802 2001-10-31
  • 打赏
  • 举报
回复
如果你编SDK的程序,只能用API函数,如果你用MFC编程,两者都可以。
fly_pig 2001-10-31
  • 打赏
  • 举报
回复
当然,当然,MFC只不过是一个Win32 API的大集合,封装了win32 API的实现,使你编程更容易
use_id 2001-10-31
  • 打赏
  • 举报
回复

看这个:

// Registry.h

class CRegistry
{

public:
CRegistry();
CRegistry( HKEY, const char * );
~CRegistry();

BOOL Open( HKEY, const char * );
BOOL Close( void );
BOOL IsOpen( void );

BOOL ReadDWORD( const char *, DWORD *, DWORD *pdwLastError = NULL );
BOOL ReadString( const char *, LPVOID, int, DWORD *pdwLastError = NULL );
char **ReadMultipleStrings( const char *, DWORD *pdwLastError = NULL );
static void DeleteList( char ** );

BOOL WriteDWORD( const char *, DWORD, DWORD *pdwLastError = NULL );
BOOL WriteString( const char *, LPVOID, DWORD *pdwLastError = NULL );
BOOL Write( const char *, LPVOID, DWORD, int );

static BOOL CreateKey( HKEY, const char *, const char * );
static BOOL DeleteKey( HKEY, const char * );

protected:
HKEY m_hKey;
DWORD m_dwLastError;
int m_nSize;
char *m_strUseKey;
BOOL Read( const char *, LPVOID, int );

};

// Registry.cpp

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

CRegistry::CRegistry()
{

m_hKey = NULL;
Open( m_hKey, m_strUseKey );

}

CRegistry::CRegistry( HKEY hKey, const char *lpszSubKey )
{

m_hKey = NULL;
Open( hKey, lpszSubKey );

}

CRegistry::~CRegistry()
{

Close();

}

BOOL CRegistry::Open( HKEY hKey, const char *lpszSubKey )
{

Close();

if( ::RegOpenKeyEx( hKey, lpszSubKey, 0, KEY_ALL_ACCESS, &m_hKey ) != ERROR_SUCCESS ){
m_hKey = NULL;
m_dwLastError = GetLastError();
return( FALSE );
}

return( TRUE );

}

BOOL CRegistry::Close( void )
{
BOOL bRet = TRUE;

if( m_hKey == NULL )
return( FALSE );

bRet = ( ::RegCloseKey( m_hKey ) == ERROR_SUCCESS );
m_hKey = NULL;

return( bRet );

}

BOOL CRegistry::IsOpen( void )
{

return( m_hKey != NULL );

}

BOOL CRegistry::Read( const char *lpszValueName, LPVOID lpReturnBuffer, int nSize )
{

if( m_hKey == NULL )
return( FALSE );

DWORD dwSize = (DWORD) nSize;
BOOL bRet = ( ::RegQueryValueEx( m_hKey, lpszValueName, NULL, NULL, (unsigned char *) lpReturnBuffer, &dwSize ) == ERROR_SUCCESS );

m_dwLastError = GetLastError();

return( bRet );

}

BOOL CRegistry::ReadDWORD( const char *lpszValueName, DWORD *pdwData, DWORD *pdwLastError )
{

if( m_hKey == NULL )
return( FALSE );

BOOL bRet = Read( lpszValueName, pdwData, sizeof( DWORD ) );

if( pdwLastError != NULL )
*pdwLastError = m_dwLastError;

return( bRet );

}

BOOL CRegistry::ReadString( const char *lpszValueName, LPVOID lpReturnBuffer, int nSize, DWORD *pdwLastError )
{

if( m_hKey == NULL )
return( FALSE );

char *lpWork = (char *) lpReturnBuffer;
lpWork[0] = 0;
BOOL bRet = Read( lpszValueName, lpReturnBuffer, nSize );

if( pdwLastError != NULL )
*pdwLastError = m_dwLastError;

return( bRet );

}

char **CRegistry::ReadMultipleStrings( const char *lpszValueName, DWORD *pdwLastError )
{
char szEntireString[2500];

if( !ReadString( lpszValueName, szEntireString, 2500, pdwLastError ) )
return( NULL );

if( szEntireString[0] == 0 )
return( NULL );

int nCount = 0;
if( szEntireString[strlen(szEntireString)-1] != ';' )
nCount = 1;
char *pPointer = szEntireString;
do{
pPointer = strstr( pPointer, ";" );
if( pPointer != NULL ){
nCount++;
pPointer++;
}
} while( pPointer != NULL );

int i = 0;
char *pEnd;
char **pList = (char **) new char [(nCount+3)*sizeof(char *)];
if( pList == NULL )
return( NULL );
memset( pList, 0, ( nCount + 3 ) * sizeof(char *) );

pPointer = szEntireString;
do{

pEnd = strstr( pPointer, ";" );
int nSize = strlen( pPointer );
if( pEnd != NULL )
nSize = pEnd - pPointer;

pList[i] = new char [nSize+3];
if( pList[i] != NULL ){
memset( pList[i], 0, nSize + 3 );
memcpy( pList[i], pPointer, nSize );
}
else pEnd = NULL;

pPointer = pEnd;
if( pPointer != NULL )
pPointer++;

i++;

} while( pPointer != NULL && pPointer[0] != 0 );

return( pList );

}

void CRegistry::DeleteList( char **pList )
{

if( pList == NULL )
return;

int i = 0;
while( pList[i] != NULL ){
delete [] pList[i];
i++;
}
delete pList;

}

BOOL CRegistry::WriteDWORD( const char *lpszValueName, DWORD dwData, DWORD *pdwLastError )
{

if( m_hKey == NULL )
return( FALSE );

m_nSize = sizeof( DWORD );
BOOL bRet = Write( lpszValueName, &dwData, REG_DWORD, sizeof( DWORD ) );

if( pdwLastError != NULL )
*pdwLastError = m_dwLastError;

return( bRet );

}

BOOL CRegistry::WriteString( const char *lpszValueName, LPVOID lpData, DWORD *pdwLastError )
{

if( m_hKey == NULL )
return( FALSE );

m_nSize = 2000;
BOOL bRet = Write( lpszValueName, lpData, REG_SZ, (DWORD) strlen( (const char *) lpData ) + 1 );

if( pdwLastError != NULL )
*pdwLastError = m_dwLastError;

return( bRet );

}

BOOL CRegistry::Write( const char *lpszValueName, LPVOID lpData, DWORD dwType, int nSize )
{

if( m_hKey == NULL )
return( FALSE );

DWORD dwSize = (DWORD) m_nSize;
BOOL bRet = ( ::RegSetValueEx( m_hKey, lpszValueName, 0, dwType, (unsigned char *) lpData, nSize ) == ERROR_SUCCESS );

m_dwLastError = GetLastError();

return( bRet );

}

BOOL CRegistry::CreateKey( HKEY hKey, const char *lpszSubKey, const char *lpszClass )
{
HKEY hOpenedKey;
DWORD dwDisposition;
DWORD dwLastError;

BOOL bRet = ( ::RegCreateKeyEx( hKey, lpszSubKey, 0, (char *) lpszClass, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hOpenedKey, &dwDisposition ) == ERROR_SUCCESS );
if( bRet ) ::RegCloseKey( hOpenedKey );
dwLastError = GetLastError();

return( bRet );

}

BOOL CRegistry::DeleteKey( HKEY hKey, const char *lpszSubKey )
{
BOOL bRet;
DWORD dwLastError;

bRet = ( ::RegDeleteKey( hKey, lpszSubKey ) == ERROR_SUCCESS );
dwLastError = GetLastError();

return( bRet );

}

registry.h
registry.cpp

CRegistry
操作注册表类

CString strSavePath;
CString strDrive1;
char cDrive[5];
strSavePath = "Software\\YourPath";
char cSavePath[256];
if(!m_Registry.Open(HKEY_LOCAL_MACHINE,strSavePath))
{
m_Registry.CreateKey(HKEY_LOCAL_MACHINE,strSavePath,"");
m_Registry.Open(HKEY_LOCAL_MACHINE,strSavePath);
m_Registry.WriteString("Display Path","c:\\*.*");
m_Registry.Close();

}
if(m_Registry.Open(HKEY_LOCAL_MACHINE,strSavePath))
{
m_Registry.ReadString("Display Path",cSavePath,256);
CString str = cSavePath;
if(str.IsEmpty())
{
m_Registry.WriteString("Display Path","c:\\*.*");
m_Registry.Close();
}
}
if(m_Registry.Open(HKEY_LOCAL_MACHINE,strSavePath))
{
m_Registry.ReadString("Display Path",cSavePath,256);
m_Registry.Close();
}
无聊客 2001-10-31
  • 打赏
  • 举报
回复
我是一位初学者,,,请问API和MFC在使用时有什么区别啊 ,,,在VC++环境下使用API是直接用的吗??见笑了,呵呵
fly_pig 2001-10-31
  • 打赏
  • 举报
回复
再贴一个

void SetRegValue(HKEY hKey, CString strKey, CString strChildKey, CString strChildKeyValue)
{
HKEY hChildKey=0;
DWORD dwDisposition;

RegCreateKeyEx(hKey,(LPCTSTR)strKey,0,NULL,
REG_OPTION_NON_VOLATILE,KEY_CREATE_SUB_KEY|KEY_ALL_ACCESS,
NULL,&hChildKey,&dwDisposition);
RegSetValueEx(hChildKey,(LPCTSTR)strChildKey,0,REG_SZ,
(const BYTE *)(LPCTSTR)strChildKeyValue,(DWORD)strChildKeyValue.GetLength());
RegCloseKey(hChildKey);
}
jason802 2001-10-31
  • 打赏
  • 举报
回复
我写的时候还没有楼上还没有呢,等我写上去才发现楼上居然比我快。而且如此相像,呵呵
fly_pig 2001-10-31
  • 打赏
  • 举报
回复
int nScrollLines = 3; // reasonable default
HKEY hKey;

if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Control Panel\\Desktop"),
0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
TCHAR szData[128];
DWORD dwKeyDataType;
DWORD dwDataBufSize = sizeof(szData);

if (RegQueryValueEx(hKey, _T("WheelScrollLines"), NULL, &dwKeyDataType,
(LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
{
nScrollLines = _tcstoul(szData, NULL, 10);
}
RegCloseKey(hKey);
}

这段代参考已下喽,它取得了一个键“WheelScrollLines”下的值
jason802 2001-10-31
  • 打赏
  • 举报
回复
一、API:
创建:
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
);

设定值:

RegSetValueEx
The RegSetValueEx function sets the data and type of a specified value under a registry key.

LONG RegSetValueEx(
HKEY hKey, // handle to key to set value for
LPCTSTR lpValueName, // name of the value to set
DWORD Reserved, // reserved
DWORD dwType, // flag for value type
CONST BYTE *lpData, // address of value data
DWORD cbData // size of value data
);

读取:

LONG RegQueryValueEx(
HKEY hKey, // handle to key to query
LPTSTR lpValueName, // address of name of value to query
LPDWORD lpReserved, // reserved
LPDWORD lpType, // address of buffer for value type
LPBYTE lpData, // address of data buffer
LPDWORD lpcbData // address of data buffer size
);

删除键:
LONG RegDeleteKey(
HKEY hKey, // handle to open key
LPCTSTR lpSubKey // address of name of subkey to delete
);

删除值:
LONG RegDeleteValue(
HKEY hKey, // handle to key
LPCTSTR lpValueName // address of value name
);

二、MFC:
使用CWinApp五个函数

1。SetRegistryKey() ;要使用其他四个函数,必须先使用这个函数。设定HkeyCurrent_User\software下的子键

其它四个:
GetProfileInt
GetProfileString
WriteProfileInt
WriteProfileString

MFC的比API的使用起来方便多了,但是也有不便的,比如只能在Hkey_Current_User\SoftWare下面设定子键,还有用两个Get函数去取值,如果不存在指定的子键,它会创建一个,如果你不想这样的话,还要用API函数去删除。


hgw111 2001-10-31
  • 打赏
  • 举报
回复
API函数:RegCreateKeyEx创建指定键
LONG RegCreateKeyEx(
HKEY hKey, // handle to open key
LPCTSTR lpSubKey, // subkey name
DWORD Reserved, // reserved
LPTSTR lpClass, // class string
DWORD dwOptions, // special options
REGSAM samDesired, // desired security access
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // inheritance
PHKEY phkResult, // key handle
LPDWORD lpdwDisposition // disposition value buffer
);
RegSetValueEx设定指定值
LONG RegSetValueEx(
HKEY hKey, // handle to key
LPCTSTR lpValueName, // value name
DWORD Reserved, // reserved
DWORD dwType, // value type
CONST BYTE *lpData, // value data
DWORD cbData // size of value data
);
读取键值可以用先用RegOpenKeyEx,再用RegQueryValueEx,
LONG RegOpenKeyEx(
HKEY hKey, // handle to open key
LPCTSTR lpSubKey, // subkey name
DWORD ulOptions, // reserved
REGSAM samDesired, // security access mask
PHKEY phkResult // handle to open key
);

LONG RegQueryValueEx(
HKEY hKey, // handle to key
LPCTSTR lpValueName, // value name
LPDWORD lpReserved, // reserved
LPDWORD lpType, // type buffer
LPBYTE lpData, // data buffer
LPDWORD lpcbData // size of data buffer
);

无聊客 2001-10-31
  • 打赏
  • 举报
回复
帮帮忙啊

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧