比如,读取 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 下面
所有项的内容,(这个文件夹也就是开机运行的程序列表)。PB里面有一个非常轻松的函数:RegistryValues('HKEY_LOCAL_MACHINE\SOFTWARE\Microsof
t\Windows\CurrentVersion\Run',ls_run),这样就得到了一个具有所有项内容的字符串数组。不知道VC有没有??? 请教!一解决就揭帖!:-)
所有项的内容,(这个文件夹也就是开机运行的程序列表)。PB里面有一个非常轻松的函数:RegistryValues('HKEY_LOCAL_MACHINE\SOFTWARE\Microsof
t\Windows\CurrentVersion\Run',ls_run),这样就得到了一个具有所有项内容的字符串数组。不知道VC有没有??? 请教!一解决就揭帖!:-)
解决方案 »
- 应用程序UniCode 和 ANSI 问题(或 wchar_t* 和 char*)
- CStatic用创建的形式产生,显示的一行文字高度不一致,有大神赐教吗?
- 多文档界面初始化的问题
- 鼠标到达窗口边界时变成双箭头,如何消除?
- 现在媒体总是说软件人才过剩,各位所在公司情况究竟怎样?怎样才算是”软件人才“?有条件散分!(不可以只是up,要谈一下自己的看法)
- win98 ADO 数据库连接问题请教
- 求教
- 最近做一个项目,里面需要计算一个功率因素,但不知道具体算法,,,,
- 请教网络高手,如何读取股票服务器上的数据?
- 对话框如何嵌入到视图客户区中?
- ListCtrl中怎么样取得当前选定行的第一列的内容??(在线)
- dispatch问题
#ifndef __REGISTRY_H__
#define __REGISTRY_H__class CRegistry
{
public:
CRegistry();
~CRegistry();int m_nLastError;// CRegistry properties
protected:
HKEY m_hRootKey;
BOOL m_bLazyWrite;
CString m_strCurrentPath;public:
inline BOOL PathIsValid() {
return (m_strCurrentPath.GetLength() > 0); }
inline CString GetCurrentPath() {
return m_strCurrentPath; }
inline HKEY GetRootKey() {
return m_hRootKey; }
//CRegistry methods
public:
BOOL ClearKey();
BOOL SetRootKey(HKEY hRootKey);
BOOL CreateKey(CString strKey);
BOOL DeleteKey(CString strKey);
BOOL DeleteValue(CString strName);
int GetDataSize(CString strValueName);
DWORD GetDataType(CString strValueName);
int GetSubKeyCount();
int GetValueCount();
BOOL KeyExists(CString strKey, HKEY hRootKey = NULL);
BOOL SetKey(CString strKey, BOOL bCanCreate);
BOOL ValueExists(CString strName);
void RenameValue(CString strOldName, CString strNewName); // data reading functions
COleDateTime ReadDateTime(CString strName, COleDateTime dtDefault);
double ReadFloat(CString strName, double fDefault);
CString ReadString(CString strName, CString strDefault);
int ReadInt(CString strName, int nDefault);
BOOL ReadBool(CString strName, BOOL bDefault);
COLORREF ReadColor(CString strName, COLORREF rgbDefault);
BOOL ReadFont(CString strName, CFont* pFont);
BOOL ReadPoint(CString strName, CPoint* pPoint);
BOOL ReadSize(CString strName, CSize* pSize);
BOOL ReadRect(CString strName, CRect* pRect);
DWORD ReadDword(CString strName, DWORD dwDefault); // data writing functions
BOOL WriteBool(CString strName, BOOL bValue);
BOOL WriteDateTime(CString strName, COleDateTime dtValue);
BOOL WriteString(CString strName, CString strValue);
BOOL WriteFloat(CString strName, double fValue);
BOOL WriteInt(CString strName, int nValue);
BOOL WriteColor(CString strName, COLORREF rgbValue);
BOOL WriteFont(CString strName, CFont* pFont);
BOOL WritePoint(CString strName, CPoint* pPoint);
BOOL WriteSize(CString strName, CSize* pSize);
BOOL WriteRect(CString strName, CRect* pRect);
BOOL WriteDword(CString strName, DWORD dwValue);};// end of CRegistry class definition
#endifcpp文件:#include "stdafx.h"
#include <winreg.h>
#include "Registry.h"#define CLASS_NAME_LENGTH 255/* IMPORTANT NOTES ABOUT CREGISTRY:
CRegistry never keeps a key open past the end of a function call.
This is incase the application crashes before the next call to close
the registry
INCLUDE FILES
"winreg.h" and "afxdisp.h" must be included in "stdafx.h" KEY NAMES:
Key names must not begin with a \ and only absolute strings are accepted
*/CRegistry::CRegistry()
{
m_hRootKey = HKEY_CURRENT_USER;
m_bLazyWrite = TRUE;
m_nLastError = ERROR_SUCCESS;
}CRegistry::~CRegistry()
{
ClearKey();
}
BOOL CRegistry::ClearKey()
{
/* Call CloseKey to write the current key to the registry and close the
key. An application should not keep keys open any longer than necessary.
Calling CloseKey when there is no current key has no effect.*/ m_strCurrentPath.Empty();
m_hRootKey = HKEY_CURRENT_USER;
m_bLazyWrite = TRUE;
return TRUE;
}BOOL CRegistry::SetRootKey(HKEY hRootKey)
{
// sets the root key
// make sure to set it to a valid key
if (hRootKey != HKEY_CLASSES_ROOT &&
hRootKey != HKEY_CURRENT_USER &&
hRootKey != HKEY_LOCAL_MACHINE &&
hRootKey != HKEY_USERS) return FALSE; m_hRootKey = hRootKey;
return TRUE;
}
BOOL CRegistry::CreateKey(CString strKey)
{
/* Use CreateKey to add a new key to the registry.
Key is the name of the key to create. Key must be
an absolute name. An absolute key
begins with a backslash (\) and is a subkey of
the root key. */ ASSERT(strKey[0] != '\\');
HKEY hKey; DWORD dwDisposition = 0; if (::RegCreateKeyEx(m_hRootKey, LPCTSTR(strKey), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
&dwDisposition) != ERROR_SUCCESS) return FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
m_strCurrentPath = strKey;
return TRUE;
}
BOOL CRegistry::DeleteKey(CString strKey)
{
/* Call DeleteKey to remove a specified key and its associated data,
if any, from the registry. Returns FALSE is there are subkeys
Subkeys must be explicitly deleted by separate calls to DeleteKey.
DeleteKey returns True if key deletion is successful. On error,
DeleteKey returns False. */
// need to open the key first with RegOpenKeyEx
ASSERT(FALSE); // not yet implemented
ASSERT(strKey[0] != '\\'); if (!KeyExists(strKey)) return TRUE;
if (::RegDeleteKey(m_hRootKey, strKey) != ERROR_SUCCESS) return FALSE;
return TRUE;
}BOOL CRegistry::DeleteValue(CString strName)
{
/* Call DeleteValue to remove a specific data value
associated with the current key. Name is string
containing the name of the value to delete. Keys can contain
multiple data values, and every value associated with a key
has a unique name. */ ASSERT(m_strCurrentPath.GetLength() > 0);
HKEY hKey;
LONG lResult; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_SET_VALUE, &hKey) != ERROR_SUCCESS) return FALSE; lResult = ::RegDeleteValue(hKey, LPCTSTR(strName));
::RegCloseKey(hKey); if (lResult == ERROR_SUCCESS) return TRUE;
return FALSE;
}
int CRegistry::GetDataSize(CString strValueName)
{
/* Call GetDataSize to determine the size, in bytes, of
a data value associated with the current key. ValueName
is a string containing the name of the data value to query.
On success, GetDataSize returns the size of the data value.
On failure, GetDataSize returns -1. */ HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0);
LONG lResult; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) return -1; DWORD dwSize = 1;
lResult = ::RegQueryValueEx(hKey, LPCTSTR(strValueName),
NULL, NULL, NULL, &dwSize);
::RegCloseKey(hKey); if (lResult != ERROR_SUCCESS) return -1;
return (int)dwSize;
}DWORD CRegistry::GetDataType(CString strValueName)
{
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0); m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_QUERY_VALUE, &hKey); if (m_nLastError != ERROR_SUCCESS) return 0; DWORD dwType = 1;
m_nLastError = ::RegQueryValueEx(hKey, LPCTSTR(strValueName),
NULL, &dwType, NULL, NULL);
::RegCloseKey(hKey); if (m_nLastError == ERROR_SUCCESS) return dwType; return 0;
}int CRegistry::GetSubKeyCount()
{
/* Call this function to determine the number of subkeys.
the function returns -1 on error */
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0); if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return -1; LONG lResult;
DWORD dwSubKeyCount, dwValueCount, dwClassNameLength,
dwMaxSubKeyName, dwMaxValueName, dwMaxValueLength;
FILETIME ftLastWritten; _TCHAR szClassBuffer[CLASS_NAME_LENGTH];
dwClassNameLength = CLASS_NAME_LENGTH;
lResult = ::RegQueryInfoKey(hKey, szClassBuffer, &dwClassNameLength,
NULL, &dwSubKeyCount, &dwMaxSubKeyName, NULL, &dwValueCount,
&dwMaxValueName, &dwMaxValueLength, NULL, &ftLastWritten);
::RegCloseKey(hKey);
if (lResult != ERROR_SUCCESS) return -1; return (int)dwSubKeyCount;
}
int CRegistry::GetValueCount()
{
/* Call this function to determine the number of subkeys.
the function returns -1 on error */
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0); if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return -1; LONG lResult;
DWORD dwSubKeyCount, dwValueCount, dwClassNameLength,
dwMaxSubKeyName, dwMaxValueName, dwMaxValueLength;
FILETIME ftLastWritten; _TCHAR szClassBuffer[CLASS_NAME_LENGTH];
dwClassNameLength = CLASS_NAME_LENGTH;
lResult = ::RegQueryInfoKey(hKey, szClassBuffer, &dwClassNameLength,
NULL, &dwSubKeyCount, &dwMaxSubKeyName, NULL, &dwValueCount,
&dwMaxValueName, &dwMaxValueLength, NULL, &ftLastWritten);
::RegCloseKey(hKey);
if (lResult != ERROR_SUCCESS) return -1; return (int)dwValueCount;
}
BOOL CRegistry::KeyExists(CString strKey, HKEY hRootKey)
{
/* Call KeyExists to determine if a key of a specified name exists.
Key is the name of the key for which to search. */ ASSERT(strKey[0] != '\\');
HKEY hKey; if (hRootKey == NULL) hRootKey = m_hRootKey;
LONG lResult = ::RegOpenKeyEx(hRootKey, LPCTSTR(strKey), 0,
KEY_ALL_ACCESS, &hKey);
::RegCloseKey(hKey);
if (lResult == ERROR_SUCCESS) return TRUE;
return FALSE;
}BOOL CRegistry::SetKey(CString strKey, BOOL bCanCreate)
{
/* Call SetKey to make a specified key the current key. Key is the
name of the key to open. If Key is null, the CurrentKey property
is set to the key specified by the RootKey property. CanCreate specifies whether to create the specified key if it does
not exist. If CanCreate is True, the key is created if necessary. Key is opened or created with the security access value KEY_ALL_ACCESS.
OpenKey only creates non-volatile keys, A non-volatile key is stored in
the registry and is preserved when the system is restarted. OpenKey returns True if the key is successfully opened or created */ ASSERT(strKey[0] != '\\');
HKEY hKey;
// close the current key if it is open
if (strKey.GetLength() == 0)
{
m_strCurrentPath.Empty();
return TRUE;
} DWORD dwDisposition;
if (bCanCreate) // open the key with RegCreateKeyEx
{
if (::RegCreateKeyEx(m_hRootKey, LPCTSTR(strKey), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
&dwDisposition) != ERROR_SUCCESS) return FALSE;
m_strCurrentPath = strKey;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return TRUE;
} // otherwise, open the key without creating
// open key requires no initial slash
m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(strKey), 0,
KEY_ALL_ACCESS, &hKey);
if (m_nLastError != ERROR_SUCCESS) return FALSE;
m_strCurrentPath = strKey;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return TRUE;
}
BOOL CRegistry::ValueExists(CString strName)
{
/* Call ValueExists to determine if a particular key exists in
the registry. Calling Value Exists is especially useful before
calling other TRegistry methods that operate only on existing keys. Name is the name of the data value for which to check.
ValueExists returns True if a match if found, False otherwise. */ HKEY hKey;
LONG lResult;
ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return FALSE; lResult = ::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
NULL, NULL, NULL);
::RegCloseKey(hKey); if (lResult == ERROR_SUCCESS) return TRUE;
return FALSE;
}
void CRegistry::RenameValue(CString strOldName, CString strNewName)
{
/* Call RenameValue to change the name of a data value associated
with the current key. OldName is a string containing the current
name of the data value. NewName is a string containing the replacement
name for the data value.
If OldName is the name of an existing data value for the current key,
and NewName is not the name of an existing data value for the current
key, RenameValue changes the data value name as specified. Otherwise
the current name remains unchanged.
*/
ASSERT(FALSE); // functionality not yet implemented
}
{
/* Call ReadDate to read a date value from a specified data value
associated with the current key. Name is the name of the data value to read.
If successful, ReadDate returns a Delphi TDateTime value. The integral part
of a TDateTime value is the number of days that have passed since 12/30/1899.
The fractional part of a TDateTime value is the time of day.
On error, an exception is raised, and the value returned by this function
should be discarded. */ DWORD dwType = REG_BINARY;
COleDateTime dt;
DWORD dwSize = sizeof(dt);
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return dtDefault;
if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)&dt, &dwSize) != ERROR_SUCCESS) dt = dtDefault;
::RegCloseKey(hKey);
return dt;
}
double CRegistry::ReadFloat(CString strName, double fDefault)
{
/* Call ReadFloat to read a float value from a specified
data value associated with the current key. Name is the name
of the data value to read.
If successful, ReadFloat returns a double value.
On error, an exception is raised, and the value returned by
this function should be discarded. */ DWORD dwType = REG_BINARY;
double d;
DWORD dwSize = sizeof(d);
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return fDefault; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)&d, &dwSize) != ERROR_SUCCESS) d = fDefault;
::RegCloseKey(hKey);
return d;
}CString CRegistry::ReadString(CString strName, CString strDefault)
{
DWORD dwType = REG_SZ;
DWORD dwSize = 255;
BOOL bSuccess = TRUE;
_TCHAR sz[255];
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0); // make sure it is the proper type
dwType = GetDataType(strName);
if (dwType != REG_SZ && dwType != REG_EXPAND_SZ)
{
return strDefault;
} m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey);
if (m_nLastError != ERROR_SUCCESS) return strDefault; m_nLastError = ::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)sz, &dwSize);
if (m_nLastError != ERROR_SUCCESS) bSuccess = FALSE;
::RegCloseKey(hKey);
if (!bSuccess) return strDefault;
return CString((LPCTSTR)sz);
}DWORD CRegistry::ReadDword(CString strName, DWORD dwDefault)
{
DWORD dwType = REG_DWORD;
DWORD dw;
DWORD dwSize = sizeof(dw);
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return dwDefault; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)&dw, &dwSize) != ERROR_SUCCESS) dw = dwDefault;
::RegCloseKey(hKey);
return dw;
}int CRegistry::ReadInt(CString strName, int nDefault)
{
DWORD dwType = REG_BINARY;
int n;
DWORD dwSize = sizeof(n);
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return nDefault; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)&n, &dwSize) != ERROR_SUCCESS) n = nDefault;
::RegCloseKey(hKey);
return n;
}BOOL CRegistry::ReadBool(CString strName, BOOL bDefault)
{
DWORD dwType = REG_BINARY;
BOOL b;
DWORD dwSize = sizeof(b);
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return bDefault; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)&b, &dwSize) != ERROR_SUCCESS) b = bDefault;
::RegCloseKey(hKey);
return b;
}
COLORREF CRegistry::ReadColor(CString strName, COLORREF rgbDefault)
{
DWORD dwType = REG_BINARY;
COLORREF rgb;
DWORD dwSize = sizeof(rgb);
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return rgbDefault; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)&rgb, &dwSize) != ERROR_SUCCESS) rgb = rgbDefault;
::RegCloseKey(hKey);
return rgb;
}BOOL CRegistry::ReadFont(CString strName, CFont* pFont)
{
DWORD dwType = REG_BINARY;
DWORD dwSize = sizeof(LOGFONT);
BOOL bSuccess = TRUE;
HKEY hKey;
LOGFONT lf; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)&lf, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE;
::RegCloseKey(hKey);
if (bSuccess)
{
pFont->Detach();
pFont->CreateFontIndirect(&lf);
}
return bSuccess;
}
BOOL CRegistry::ReadPoint(CString strName, CPoint* pPoint)
{
DWORD dwType = REG_BINARY;
DWORD dwSize = sizeof(CPoint);
BOOL bSuccess = TRUE;
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)pPoint, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE;
::RegCloseKey(hKey);
return bSuccess;
}BOOL CRegistry::ReadSize(CString strName, CSize* pSize)
{
DWORD dwType = REG_BINARY;
DWORD dwSize = sizeof(CSize);
BOOL bSuccess = TRUE;
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)pSize, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE;
::RegCloseKey(hKey);
return bSuccess;
}BOOL CRegistry::ReadRect(CString strName, CRect* pRect)
{
DWORD dwType = REG_BINARY;
DWORD dwSize = sizeof(CRect);
BOOL bSuccess = TRUE;
HKEY hKey; ASSERT(m_strCurrentPath.GetLength() > 0);
if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
&dwType, (LPBYTE)pRect, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE;
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteBool(CString strName, BOOL bValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)&bValue, sizeof(bValue))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteDateTime(CString strName, COleDateTime dtValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)&dtValue, sizeof(dtValue))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteString(CString strName, CString strValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey;
_TCHAR sz[255];
if (strValue.GetLength() > 254) return FALSE;#ifdef _UNICODE
wstrcpy(sz, LPCTSTR(strValue));
#else
strcpy(sz, LPCTSTR(strValue));
#endif if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
#ifdef _UNICODE
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_SZ, (LPBYTE)sz, wstrlen(sz) + 1)
!= ERROR_SUCCESS) bSuccess = FALSE;
#else
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_SZ, (LPBYTE)sz, strlen(sz) + 1)
!= ERROR_SUCCESS) bSuccess = FALSE;
#endif
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteFloat(CString strName, double fValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)&fValue, sizeof(fValue))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}BOOL CRegistry::WriteInt(CString strName, int nValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)&nValue, sizeof(nValue))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}BOOL CRegistry::WriteDword(CString strName, DWORD dwValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)&dwValue, sizeof(dwValue))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}BOOL CRegistry::WriteColor(CString strName, COLORREF rgbValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)&rgbValue, sizeof(rgbValue))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteFont(CString strName, CFont* pFont)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; LOGFONT lf;
pFont->GetLogFont(&lf); if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)&lf, sizeof(lf))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WritePoint(CString strName, CPoint* pPoint)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)pPoint, sizeof(CPoint))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteSize(CString strName, CSize* pSize)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)pSize, sizeof(CSize))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}BOOL CRegistry::WriteRect(CString strName, CRect* pRect)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey; if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
REG_BINARY, (LPBYTE)pRect, sizeof(CRect))
!= ERROR_SUCCESS) bSuccess = FALSE;
if (!m_bLazyWrite) ::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
我会尽快解决问题并揭帖,谢谢大家的回复!
在实际编程工作中,我们遇到了如何在Visual C++中对Windows95/98/Me注册表整个树状结构信息进行访问和修改的问题,如查询和修改注册表中用户姓名和公司名称的有关信息。通过编程实践,我们实现了在Visual C++中查询和修改系统注册表的有关信息。下面以一个实例说明具体的编程方法。
在Visual C++ 6.0或5.0环境中新建一基于对话框的工程,设置两个命令按钮,名为“查询用户信息”和“修改用户信息”,用来查询和修改注册表中用户姓名和公司名称。这里须要指出的是,用户的信息位于系统注册表中 \HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\ 的位置,键值名RegisteredOwner和RegisteredOrganization分别表示用户的姓名和用户公司的名称。
1.查询用户信息的代码
HKEY hKEY; //定义有关的 hKEY, 在查询结束时要关闭。
LPCTSTR data_Set="Software\\Microsoft\\Windows\\CurrentVersion\\";
//打开与路径 data_Set 相关的 hKEY,第一个参数为根键名称,第二个参数表。
//表示要访问的键的位置,第三个参数必须为0,KEY_READ表示以查询的方式。
//访问注册表,hKEY则保存此函数所打开的键的句柄。
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set, 0, KEY_READ, &hKEY));
if(ret0!=ERROR_SUCCESS) //如果无法打开hKEY,则终止程序的执行
{MessageBox("错误: 无法打开有关的hKEY!");
return;}
//查询有关的数据 (用户姓名 owner_Get)。
LPBYTE owner_Get=new BYTE[80];
DWORD type_1=REG_SZ ; DWORD cbData_1=80;
//hKEY为刚才RegOpenKeyEx()函数所打开的键的句柄,"RegisteredOwner"。
//表示要查 询的键值名,type_1表示查询数据的类型,owner_Get保存所。
//查询的数据,cbData_1表示预设置的数据长度。
long ret1=::RegQueryValueEx(hKEY, "RegisteredOwner", NULL,
&type_1, owner_Get, &cbData_1);
if(ret1!=ERROR_SUCCESS)
{
MessageBox("错误: 无法查询有关注册表信息!");
return;
}
// 查询有关的数据 (公司名 company_Get)
LPBYTE company_Get=new BYTE [80];
DWORD type_2=REG_SZ; DWORD cbData_2=80;
long ret2=::RegQueryValueEx(hKEY, "RegisteredOrganization", NULL,&type_2,company_Get, &cbData_2);
if(ret2!=ERROR_SUCCESS)
{
MessageBox("错误: 无法查询有关注册表信息!");
return;
}
// 将 owner_Get 和 company_Get 转换为 CString 字符串, 以便显示输出。
CString str_owner=CString(owner_Get);
CString str_company=CString(company_Get);
delete[] owner_Get; delete[] company_Get;
// 程序结束前要关闭已经打开的 hKEY。
::RegCloseKey(hKEY);
……
这样,上述程序执行完毕,字符串str_owner和str_company则表示查询到的用户的姓名和公司的名称,在VC++中便可用对话框的方式将其显示出来。
2.修改用户信息的代码
注意和上述的查询代码属于不同的函数体。
在程序中我们先显示一个对话框,要求用户输入新的用户姓名和公司名称并按确认键,将取得CString类型的有关字符串。要先将其转换为LPBYTE(即unsigned char*)型的数据类型,以便后面的函数调用。下面是程序中用到的将CString型转换为LPBYTE的转换函数:
LPBYTE CString_To_LPBYTE(CString str)
{
LPBYTE lpb=new BYTE[str.GetLength()+1];
for(int i=0; ibr> lpb[str.GetLength()]=0;
return lpb;
}
以下则是具体的修改注册表用户信息的代码:
CString str_owner, str_company;
…… //通过对话框输入新的用户信息,保存到str_owner和str_company
//定义有关的 hKEY, 在程序的最后要关闭。
HKEY hKEY;
LPCTSTR data_Set="Software\\Microsoft\\Windows\\CurrentVersion";
//打开与路径 data_Set 相关的hKEY,KEY_WRITE表示以写的方式打开。
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
data_Set, 0, KEY_WRITE, &hKEY));
if(ret0!=ERROR_SUCCESS)
{
MessageBox("错误: 无法打开有关的hKEY!");
return;
}
//修改有关数据(用户姓名 owner_Set),要先将CString型转换为LPBYTE。
LPBYTE owner_Set=CString_To_LPBYTE(str_owner);
DWORD type_1=REG_SZ;
DWORD cbData_1=str_owner.GetLength()+1;
//与RegQureyValueEx()类似,hKEY表示已打开的键的句柄,"RegisteredOwner"
//表示要访问的键值名,owner_Set表示新的键值,type_1和cbData_1表示新值。
//的数据类型和数据长度
long ret1=::RegSetValueEx(hKEY, "RegisteredOwner", NULL,
type_1, owner_Set, cbData_1);
if(ret1!=ERROR_SUCCESS)
{
MessageBox("错误: 无法修改有关注册表信息!");
return;
}
//修改有关的数据 (公司名 company_Set)
LPBYTE company_Set=CString_To_LPBYTE(str_company);
DWORD type_2=REG_SZ;
DWORD cbData_2=str_company.GetLength()+1;
long ret2=::RegSetValueEx(hKEY, "RegisteredOrganization", NULL,
type_2, company_Set, cbData_2);
if(ret2!=ERROR_SUCCESS)
{
MessageBox("错误: 无法修改有关注册表信息!");
return;
}
执行上面的修改注册表的操作后,可打开注册表查看具体的数值,可以看到已经成功地修改了有关的数据了。
以上实例讲述了如何在VC++中访问Windows98/95的系统注册表,我们可以很方便地查询及修改注册表的任何位置的有关信息。以上的程序在Visual C++ 6.0中调试通过(Visual C++5.0与之类似),且运行结果正确。