99分询问注册表某个目录下,所有键值数目及所有键名?

hyn_fly 2003-08-19 12:07:56
我想在列表框中列出所有开机自动运行的程序名称,在注册表如下路径:HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run中,现在想列出Run下所有的键的名称到列表框中,不知采用什么函数操作,请高手指教!
...全文
106 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hynxhvc 2003-08-21
  • 打赏
  • 举报
回复
h_flying@sohu.com谢了
sunheroshang 2003-08-21
  • 打赏
  • 举报
回复
还有个例子,索性一并给你好了,留个信箱。给你发过去。
sunheroshang 2003-08-21
  • 打赏
  • 举报
回复
它妈的,上次连续三次,发不了。剩下的在这儿。


BOOL CRegistryEx::Read(LPCTSTR pszKey, LPRECT& rcRect)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 30;
CDWordArray dwcArray;
DWORD dwType;
DWORD dwData = iMaxChars;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType,
byData, &dwData);

if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY)
{
ASSERT(dwData < iMaxChars);
CMemFile file(byData, dwData);
CArchive ar(&file, CArchive::load);
ar.m_bForceFlat = FALSE;
ASSERT(ar.IsLoading());
ASSERT(dwcArray.IsSerializable());
dwcArray.RemoveAll();
dwcArray.SetSize(5);
dwcArray.Serialize(ar);
ar.Close();
file.Close();
rcRect->top = dwcArray.GetAt(0);
rcRect->bottom = dwcArray.GetAt(1);
rcRect->left = dwcArray.GetAt(2);
rcRect->right = dwcArray.GetAt(3);
}

m_Info.lMessage = lReturn;
m_Info.dwType = REG_RECT;
m_Info.dwSize = sizeof(RECT);

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Read(LPCTSTR pszKey, LPPOINT& lpPoint)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 20;
CDWordArray dwcArray;
DWORD dwType;
DWORD dwData = iMaxChars;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType,
byData, &dwData);

if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY)
{
ASSERT(dwData < iMaxChars);
CMemFile file(byData, dwData);
CArchive ar(&file, CArchive::load);
ar.m_bForceFlat = FALSE;
ASSERT(ar.IsLoading());
ASSERT(dwcArray.IsSerializable());
dwcArray.RemoveAll();
dwcArray.SetSize(5);
dwcArray.Serialize(ar);
ar.Close();
file.Close();
lpPoint->x = dwcArray.GetAt(0);
lpPoint->y = dwcArray.GetAt(1);
}

m_Info.lMessage = lReturn;
m_Info.dwType = REG_POINT;
m_Info.dwSize = sizeof(POINT);

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::DeleteValue (LPCTSTR pszValue)
{
ASSERT(m_hKey);
LONG lReturn = RegDeleteValue(m_hKey, pszValue);


m_Info.lMessage = lReturn;
m_Info.dwType = 0L;
m_Info.dwSize = 0L;

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::DeleteValueKey (HKEY hKeyRoot, LPCTSTR pszPath)
{
ASSERT(pszPath);
ASSERT(hKeyRoot);

LONG lReturn = RegDeleteKey(hKeyRoot, pszPath);

m_Info.lMessage = lReturn;
m_Info.dwType = 0L;
m_Info.dwSize = 0L;

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

// for debugging
void ShowSysMsg(LONG res, char* str, char* file, DWORD line)
{
char msg[256];
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
res,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Defaultlanguage
(LPTSTR) &lpMsgBuf,
0,
NULL );
sprintf(msg, "%u %s\n%s %u", res, str, file, line);
MessageBox( NULL, msg, (char*)lpMsgBuf, MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
}

CString CRegistryEx::FindKey (LPCTSTR pszSearchRoot, LPCTSTR pszKey, LPCTSTR pszValue)
{
CString strResult;

ASSERT(m_hKey);
ASSERT(pszSearchRoot);
ASSERT(pszKey);
ASSERT(pszValue);

// read key from registry based on search root; determine value
CRegistryEx registry;
registry.Open( HKEY_LOCAL_MACHINE, pszSearchRoot );
CString strValueFound;
registry.Read( pszKey, strValueFound );
registry.Close();

CString strValueWanted = pszValue;

strValueFound.MakeUpper();
strValueWanted.MakeUpper();

if ( strValueFound == strValueWanted )
{
strResult = pszSearchRoot;
}
else
{
CStringArray aKeys;
if ( ListKey( pszSearchRoot, aKeys ) )
{
int nCount;
const int nSize = aKeys.GetSize();
for ( nCount = 0; nCount < nSize && strResult.IsEmpty(); nCount++ )
{
const CString strKey = CString( pszSearchRoot ) + "\\" + aKeys[ nCount ];
CStringArray aSubKeys;
if ( ListKey( strKey, aSubKeys ) )
{
strResult = FindKey( strKey, pszKey, pszValue );
}
}
}
}

return strResult;
}

BOOL CRegistryEx::ListKey (LPCTSTR pszKey, CStringArray& scArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
DWORD dwSubKeys = 0;
DWORD idw;
LONG lReturn;
HKEY hQKey = NULL;
CString szSubKeyName;
TCHAR szStr[200];
DWORD dwSubKeyNameLen;
FILETIME LastFileTime;
BOOL Ok;

lReturn = RegOpenKeyEx (m_hKey, pszKey, 0L,
KEY_READ, &hQKey);

Ok = (lReturn == ERROR_SUCCESS);

m_Info.lMessage = lReturn;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;

if (Ok) {

lReturn = RegQueryInfoKey( hQKey, NULL, NULL, NULL
, &dwSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL );

m_Info.lMessage = lReturn;
m_Info.dwType = 0;
m_Info.dwSize = 0;

Ok = (lReturn == ERROR_SUCCESS);
}
if (Ok) {
scArray.RemoveAll();
}
for ( idw=0; ((idw<dwSubKeys) && Ok); idw++) {
dwSubKeyNameLen = 200;
lReturn = RegEnumKeyEx(
hQKey
, idw
, szStr
, &dwSubKeyNameLen
, NULL
, NULL
, NULL
, &LastFileTime
);

m_Info.lMessage = lReturn;
m_Info.dwType = 0;
m_Info.dwSize = 0;

Ok = (lReturn == ERROR_SUCCESS);
if (Ok) {
// Add name to array
szSubKeyName = szStr;
scArray.Add( szSubKeyName );
}
else {
ShowSysMsg(lReturn, "RegEnumKeyEx", __FILE__, __LINE__);
}

}
if (hQKey != NULL) {
RegCloseKey (hQKey);
hQKey = NULL;
}
if(Ok)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::ListValues (LPCTSTR pszKey, CStringArray& scArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
DWORD dwValues = 0;
DWORD idw;
LONG lReturn;
HKEY hQKey = NULL;
CString szValueName;
TCHAR szStr[200];
DWORD dwValueNameLen;
DWORD dwType;
BOOL Ok;

lReturn = RegOpenKeyEx (m_hKey, pszKey, 0L,
KEY_READ, &hQKey);

Ok = (lReturn == ERROR_SUCCESS);

m_Info.lMessage = lReturn;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;

if (Ok) {

lReturn = RegQueryInfoKey( hQKey, NULL, NULL, NULL
, NULL, NULL, NULL, &dwValues, NULL, NULL, NULL, NULL );

m_Info.lMessage = lReturn;
m_Info.dwType = 0;
m_Info.dwSize = 0;

Ok = (lReturn == ERROR_SUCCESS);
}
if (Ok) {
scArray.RemoveAll();
}
for ( idw=0; ((idw<dwValues) && Ok); idw++) {
dwValueNameLen = 200;
lReturn = RegEnumValue(
hQKey // handle to key to query
, idw // index of value to query
, szStr // address of buffer for value string
, &dwValueNameLen // address for size of value buffer
, NULL // reserved
, &dwType // address of buffer for type code
, NULL // address of buffer for value data
, NULL // address for size of data buffer
);

m_Info.lMessage = lReturn;
m_Info.dwType = 0;
m_Info.dwSize = 0;

Ok = (lReturn == ERROR_SUCCESS);
if (Ok) {
// Add name to array
szValueName = szStr;
scArray.Add( szValueName );
}
else {
ShowSysMsg(lReturn, "RegEnumValue", __FILE__, __LINE__);
}

}
if (hQKey != NULL) {
RegCloseKey (hQKey);
hQKey = NULL;
}
if(Ok)
return TRUE;

return FALSE;
}
zzqzzq 2003-08-20
  • 打赏
  • 举报
回复
给你个例子
HKEY root=HKEY_LOCAL_MACHINE,hKey;
char path[64]="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
RegOpenKeyEx(root,path,0,KEY_READ,&hKey);
DWORD index=0,ret=0;
char name[128]="",data[218]="";
do{
DWORD dw=128,type=REG_SZ,size=218;
ret=::RegEnumValue(hKey,index,(LPTSTR)name,&dw,0,&type,(LPBYTE)data,&size);
m_name=name;//输出结果
m_data=data;
index++;
}while(ret!= ERROR_NO_MORE_ITEMS);
::RegCloseKey(hKey);
}
hyn_fly 2003-08-20
  • 打赏
  • 举报
回复
To sunheroshang();请给我把剩下地函数粘贴上来好吗?

问题解决立即给分!
hyn_fly 2003-08-20
  • 打赏
  • 举报
回复
怎么没有看到你的ListKey,ListValues函数呢?
hyn_fly 2003-08-19
  • 打赏
  • 举报
回复
To xtuzi(雨人) :
有没有更详细的例子或参数的设置写详细点好吗?
hyn_fly 2003-08-19
  • 打赏
  • 举报
回复
up一下
oopig 2003-08-19
  • 打赏
  • 举报
回复
try this API:
NTSTATUS
IRegistryKey::EnumerateKey(
IN ULONG Index,
IN KEY_INFORMATION_CLASS KeyInformationClass,
OUT PVOID KeyInformation,
IN ULONG Length,
OUT PULONG ResultLength
);
xtuzi 2003-08-19
  • 打赏
  • 举报
回复
先用RegOpenEx这个API,再用RegEnumKey和RegEnumValue这两个API吧
mfq 2003-08-19
  • 打赏
  • 举报
回复
LONG RegQueryInfoKey(
HKEY hKey, // handle to key to query
LPTSTR lpClass, // address of buffer for class string
LPDWORD lpcbClass, // address of size of class string buffer
LPDWORD lpReserved, // reserved
LPDWORD lpcSubKeys, // address of buffer for number of
// subkeys
LPDWORD lpcbMaxSubKeyLen, // address of buffer for longest subkey
// name length
LPDWORD lpcbMaxClassLen, // address of buffer for longest class
// string length
LPDWORD lpcValues, // address of buffer for number of value
// entries
LPDWORD lpcbMaxValueNameLen, // address of buffer for longest
// value name length
LPDWORD lpcbMaxValueLen, // address of buffer for longest value
// data length
LPDWORD lpcbSecurityDescriptor,
// address of buffer for security
// descriptor length
PFILETIME lpftLastWriteTime // address of buffer for last write
// time
);

sunheroshang 2003-08-19
  • 打赏
  • 举报
回复
BOOL CRegistryEx::Read (LPCTSTR pszKey, CStringList& scStringList)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
DWORD dwType;
DWORD dwData = iMaxChars;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType,
byData, &dwData);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwData;

if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY)
{
ASSERT(dwData < iMaxChars);
CMemFile file(byData, dwData);
CArchive ar(&file, CArchive::load);
ar.m_bForceFlat = FALSE;
ASSERT(ar.IsLoading());
ASSERT(scStringList.IsSerializable());
scStringList.RemoveAll();
scStringList.Serialize(ar);
ar.Close();
file.Close();
}

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Read (LPCTSTR pszKey, CByteArray& bcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
int OldSize = bcArray.GetSize();
DWORD dwType;
DWORD dwData = iMaxChars;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType,
byData, &dwData);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwData;

if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY)
{
ASSERT(dwData < iMaxChars);
CMemFile file(byData, dwData);
CArchive ar(&file, CArchive::load);
ar.m_bForceFlat = FALSE;
ASSERT(ar.IsLoading());
ASSERT(bcArray.IsSerializable());
bcArray.RemoveAll();
bcArray.SetSize(10);
bcArray.Serialize(ar);
bcArray.SetSize(OldSize);
ar.Close();
file.Close();
}

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Read (LPCTSTR pszKey, CDWordArray& dwcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
int OldSize = dwcArray.GetSize();
DWORD dwType;
DWORD dwData = iMaxChars;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType,
byData, &dwData);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwData;

if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY)
{
ASSERT(dwData < iMaxChars);
CMemFile file(byData, dwData);
CArchive ar(&file, CArchive::load);
ar.m_bForceFlat = FALSE;
ASSERT(ar.IsLoading());
ASSERT(dwcArray.IsSerializable());
dwcArray.RemoveAll();
dwcArray.SetSize(10);
dwcArray.Serialize(ar);
dwcArray.SetSize(OldSize);
ar.Close();
file.Close();
}

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Read (LPCTSTR pszKey, CWordArray& wcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
int OldSize = wcArray.GetSize();
DWORD dwType;
DWORD dwData = iMaxChars;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType,
byData, &dwData);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwData;

if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY)
{
ASSERT(dwData < iMaxChars);
CMemFile file(byData, dwData);
CArchive ar(&file, CArchive::load);
ar.m_bForceFlat = FALSE;
ASSERT(ar.IsLoading());
ASSERT(wcArray.IsSerializable());
wcArray.RemoveAll();
wcArray.SetSize(10);
wcArray.Serialize(ar);
wcArray.SetSize(OldSize);
ar.Close();
file.Close();
}

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Read (LPCTSTR pszKey, CStringArray& scArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
int OldSize = scArray.GetSize();
DWORD dwType;
DWORD dwData = iMaxChars;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType,
byData, &dwData);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwData;

if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY)
{
ASSERT(dwData < iMaxChars);
CMemFile file(byData, dwData);
CArchive ar(&file, CArchive::load);
ar.m_bForceFlat = FALSE;
ASSERT(ar.IsLoading());
ASSERT(scArray.IsSerializable());
scArray.RemoveAll();
scArray.SetSize(10);
scArray.Serialize(ar);
scArray.SetSize(OldSize);
ar.Close();
file.Close();
}

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}
sunheroshang 2003-08-19
  • 打赏
  • 举报
回复
BOOL CRegistryEx::Write (LPCTSTR pszKey, CDWordArray& dwcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(dwcArray.IsSerializable());
dwcArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);

m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Write (LPCTSTR pszKey, CWordArray& wcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(wcArray.IsSerializable());
wcArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);

m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Write (LPCTSTR pszKey, CStringArray& scArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(scArray.IsSerializable());
scArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);

m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Write(LPCTSTR pszKey, LPCRECT rcRect)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 30;
CDWordArray dwcArray;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

dwcArray.SetSize(5);
dwcArray.SetAt(0, rcRect->top);
dwcArray.SetAt(1, rcRect->bottom);
dwcArray.SetAt(2, rcRect->left);
dwcArray.SetAt(3, rcRect->right);

CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(dwcArray.IsSerializable());
dwcArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);

m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_RECT;

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Write(LPCTSTR pszKey, LPPOINT& lpPoint)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 20;
CDWordArray dwcArray;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

dwcArray.SetSize(5);
dwcArray.SetAt(0, lpPoint->x);
dwcArray.SetAt(1, lpPoint->y);

CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(dwcArray.IsSerializable());
dwcArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);

m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_POINT;

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Read(LPCTSTR pszKey, int& iVal)
{
ASSERT(m_hKey);
ASSERT(pszKey);

DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;

LONG lReturn = RegQueryValueEx (m_hKey, (LPSTR) pszKey, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwSize;

if(lReturn == ERROR_SUCCESS)
{
iVal = (int)dwDest;
return TRUE;
}

return FALSE;
}

BOOL CRegistryEx::Read (LPCTSTR pszKey, LPSTR lpszBuffer, int nBufferSize )
{
ASSERT(m_hKey);
ASSERT(pszKey);

DWORD dwType;
DWORD dwSize = nBufferSize;
char szString[255];

LONG lReturn = RegQueryValueEx (m_hKey, (LPSTR) pszKey, NULL,
&dwType, (BYTE *) szString, &dwSize);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwSize;

if(lReturn == ERROR_SUCCESS)
{
memcpy( lpszBuffer, szString, (int) dwSize );
nBufferSize = (int) dwSize;
return TRUE;
}

return FALSE;
}

BOOL CRegistryEx::Read (LPCTSTR pszKey, DWORD& dwVal)
{
ASSERT(m_hKey);
ASSERT(pszKey);

DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;

LONG lReturn = RegQueryValueEx (m_hKey, (LPSTR) pszKey, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwSize;

if(lReturn == ERROR_SUCCESS)
{
dwVal = dwDest;
return TRUE;
}

return FALSE;
}

BOOL CRegistryEx::Read (LPCTSTR pszKey, CString& sVal)
{
ASSERT(m_hKey);
ASSERT(pszKey);

DWORD dwType;
DWORD dwSize = 200;
char szString[255];

LONG lReturn = RegQueryValueEx (m_hKey, (LPSTR) pszKey, NULL,
&dwType, (BYTE *) szString, &dwSize);

m_Info.lMessage = lReturn;
m_Info.dwType = dwType;
m_Info.dwSize = dwSize;

if(lReturn == ERROR_SUCCESS)
{
sVal = szString;
return TRUE;
}

return FALSE;
}
sunheroshang 2003-08-19
  • 打赏
  • 举报
回复
你的问题不是几句话就可以完成的,我这儿有个类,利用其成员ListKey,ListValues可以完成你的要求,你可以参考一下,也可以直接使用。
要自己劳动啊。

//.h
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998 by Shane Martin
// All rights reserved
//
// Distribute freely, except: don't remove my name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc., and
// I'll try to keep a version up to date. I can be reached as follows:
// shane.kim@kaiserslautern.netsurf.de
/////////////////////////////////////////////////////////////////////////////
#include <winreg.h>

#define REG_RECT 0x0001
#define REG_POINT 0x0002

class CRegistryEx : public CObject
{
// Construction
public:
CRegistryEx(HKEY hKeyRoot = HKEY_LOCAL_MACHINE);
virtual ~CRegistryEx();

struct REGINFO
{
LONG lMessage;
DWORD dwType;
DWORD dwSize;
} m_Info;
// Operations
public:
BOOL VerifyKey (HKEY hKeyRoot, LPCTSTR pszPath);
BOOL VerifyKey (LPCTSTR pszPath);
BOOL VerifyValue (LPCTSTR pszValue);
BOOL CreateKey (HKEY hKeyRoot, LPCTSTR pszPath);
BOOL Open (HKEY hKeyRoot, LPCTSTR pszPath);
void Close();

BOOL DeleteValue (LPCTSTR pszValue);
BOOL DeleteValueKey (HKEY hKeyRoot, LPCTSTR pszPath);

BOOL Write (LPCTSTR pszKey, int iVal);
BOOL Write (LPCTSTR pszKey, DWORD dwVal);
BOOL Write (LPCTSTR pszKey, LPCTSTR pszVal);
BOOL Write (LPCTSTR pszKey, CStringList& scStringList);
BOOL Write (LPCTSTR pszKey, CByteArray& bcArray);
BOOL Write (LPCTSTR pszKey, CStringArray& scArray);
BOOL Write (LPCTSTR pszKey, CDWordArray& dwcArray);
BOOL Write (LPCTSTR pszKey, CWordArray& wcArray);
BOOL Write (LPCTSTR pszKey, LPCRECT rcRect);
BOOL Write (LPCTSTR pszKey, LPPOINT& lpPoint);

BOOL Read (LPCTSTR pszKey, int& iVal);
BOOL Read (LPCTSTR pszKey, DWORD& dwVal);
BOOL Read (LPCTSTR pszKey, LPSTR lpszBuffer, int nBufferSize );
BOOL Read (LPCTSTR pszKey, CString& sVal);
BOOL Read (LPCTSTR pszKey, CStringList& scStringList);
BOOL Read (LPCTSTR pszKey, CStringArray& scArray);
BOOL Read (LPCTSTR pszKey, CDWordArray& dwcArray);
BOOL Read (LPCTSTR pszKey, CWordArray& wcArray);
BOOL Read (LPCTSTR pszKey, CByteArray& bcArray);
BOOL Read (LPCTSTR pszKey, LPPOINT& lpPoint);
BOOL Read (LPCTSTR pszKey, LPRECT& rcRect);


BOOL ListKey (LPCTSTR pszKey, CStringArray& scArray);
BOOL ListValues (LPCTSTR pszKey, CStringArray& scArray);

// added for use with modem information, iterates though registry sections
CString FindKey(LPCTSTR pszSearchRoot, LPCTSTR pszKey, LPCTSTR pszValue );

protected:
HKEY m_hKey;
CString m_sPath;
};


//.cpp

#include "stdafx.h"
#include "RegistryEx.h"

CRegistryEx::CRegistryEx(HKEY hKeyRoot)
{
m_hKey = hKeyRoot;
}

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


BOOL CRegistryEx::VerifyKey (HKEY hKeyRoot, LPCTSTR pszPath)
{
ASSERT (hKeyRoot);
ASSERT (pszPath);

LONG ReturnValue = RegOpenKeyEx (hKeyRoot, pszPath, 0L,
KEY_ALL_ACCESS, &m_hKey);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;

m_Info.lMessage = ReturnValue;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;

return FALSE;
}

BOOL CRegistryEx::VerifyKey (LPCTSTR pszPath)
{
ASSERT (m_hKey);

LONG ReturnValue = RegOpenKeyEx (m_hKey, pszPath, 0L,
KEY_ALL_ACCESS, &m_hKey);

m_Info.lMessage = ReturnValue;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;

if(ReturnValue == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::VerifyValue (LPCTSTR pszValue)
{
ASSERT(m_hKey);
LONG lReturn = RegQueryValueEx(m_hKey, pszValue, NULL,
NULL, NULL, NULL);

m_Info.lMessage = lReturn;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::CreateKey (HKEY hKeyRoot, LPCTSTR pszPath)
{
DWORD dw;

LONG ReturnValue = RegCreateKeyEx (hKeyRoot, pszPath, 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
&m_hKey, &dw);

m_Info.lMessage = ReturnValue;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;

if(ReturnValue == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Open (HKEY hKeyRoot, LPCTSTR pszPath)
{
m_sPath = pszPath;

LONG ReturnValue = RegOpenKeyEx (hKeyRoot, pszPath, 0L,
KEY_ALL_ACCESS, &m_hKey);

m_Info.lMessage = ReturnValue;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;

if(ReturnValue == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

void CRegistryEx::Close()
{
if (m_hKey)
{
RegCloseKey (m_hKey);
m_hKey = NULL;
}
}

BOOL CRegistryEx::Write (LPCTSTR pszKey, int iVal)
{
DWORD dwValue;

ASSERT(m_hKey);
ASSERT(pszKey);

dwValue = (DWORD)iVal;
LONG ReturnValue = RegSetValueEx (m_hKey, pszKey, 0L, REG_DWORD,
(CONST BYTE*) &dwValue, sizeof(DWORD));

m_Info.lMessage = ReturnValue;
m_Info.dwSize = sizeof(DWORD);
m_Info.dwType = REG_DWORD;

if(ReturnValue == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Write (LPCTSTR pszKey, DWORD dwVal)
{
ASSERT(m_hKey);
ASSERT(pszKey);
return RegSetValueEx (m_hKey, pszKey, 0L, REG_DWORD,
(CONST BYTE*) &dwVal, sizeof(DWORD));
}

BOOL CRegistryEx::Write (LPCTSTR pszKey, LPCTSTR pszData)
{
ASSERT(m_hKey);
ASSERT(pszKey);
ASSERT(pszData);
ASSERT(AfxIsValidAddress(pszData, strlen(pszData), FALSE));

LONG ReturnValue = RegSetValueEx (m_hKey, pszKey, 0L, REG_SZ,
(CONST BYTE*) pszData, strlen(pszData) + 1);

m_Info.lMessage = ReturnValue;
m_Info.dwSize = strlen(pszData) + 1;
m_Info.dwType = REG_SZ;

if(ReturnValue == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Write (LPCTSTR pszKey, CStringList& scStringList)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(scStringList.IsSerializable());
scStringList.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);

m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL CRegistryEx::Write (LPCTSTR pszKey, CByteArray& bcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);

CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(bcArray.IsSerializable());
bcArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);

m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;

if(byData)
{
free(byData);
byData = NULL;
}

if(lReturn == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

16,472

社区成员

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

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

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