第二个问题:
首先使用RegCreateKeyEx创建一个key,
然后用RegSetValueEx设置值,
二进制值 dwType = REG_BINARY
DWORD值 dwType = REG_DWORD删除一个值使用RegDeleteValue详细参照MSDN的Platform SDK: Registry
首先使用RegCreateKeyEx创建一个key,
然后用RegSetValueEx设置值,
二进制值 dwType = REG_BINARY
DWORD值 dwType = REG_DWORD删除一个值使用RegDeleteValue详细参照MSDN的Platform SDK: Registry
字符串值 dwType = REG_SZ
删除
void DeleteRegistryKey(HKEY hKeyRoot,char *szSubKey)
{
LONG lErr;
HKEY hkey = NULL;
//Open the key check sub-keys
lErr = RegOpenKeyEx(hKeyRoot,szSubKey,NULL,KEY_ALL_ACCESS,&hkey);
if(lErr==ERROR_SUCCESS) {
FILETIME ftKey;
char achName[MAX_PATH];
DWORD dwcbName;
DWORD dwiKey=0;
//check for subkeys to delete
while(lErr==ERROR_SUCCESS) {
dwcbName = sizeof(achName);
lErr=RegEnumKeyEx(hkey,dwiKey,achName,&dwcbName,0,NULL,NULL,&ftKey);
if(lErr==ERROR_SUCCESS) {
DeleteRegistryKey(hkey,achName);
// ++dwiKey;
}
}
//close the subkey and try to delete it
RegCloseKey(hkey);
lErr = RegDeleteKey(hKeyRoot,szSubKey);
if(lErr!=ERROR_SUCCESS) {
char *szBuf,*szTitle;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
FORMAT_MESSAGE_IGNORE_INSERTS|
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
0,
lErr,
(LPTSTR)&szBuf,
1,
NULL);
szTitle=new char[512];
sprintf(szTitle,"Unable to delete registry key %s",szBuf);
MessageBox(NULL,szBuf,szTitle,MB_OK);
delete szTitle;
LocalFree(szBuf);
}
}
}
添加
BOOL CRegistry::SetEntry(const int majorKey,
const CString& subKey,
const CString& value)
{
HKEY hMajorKey = 0;
switch (majorKey)
{
case CREGISTRY_SYSTEM:
{
hMajorKey = HKEY_CLASSES_ROOT;
break;
}
case CREGISTRY_USER:
{
hMajorKey = HKEY_CURRENT_USER;
break;
}
case CREGISTRY_MACHINE:
{
hMajorKey = HKEY_LOCAL_MACHINE;
break;
}
default:
return FALSE; } CString actualKey, name;
SplitKeyName(subKey, actualKey, name); HKEY hKey = 0;
long ret = ::RegOpenKeyEx(hMajorKey, (const char *) actualKey,
0, KEY_ALL_ACCESS, & hKey); if (ret != ERROR_SUCCESS)
{
// Create key
char classType[1];
classType[0] = 0;
DWORD disposition = 0;
ret = ::RegCreateKeyEx(hMajorKey, (const char *) actualKey,
0, classType, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
& hKey, & disposition); if (ret != ERROR_SUCCESS)
return FALSE;
} DWORD reserved = 0;
DWORD type = REG_SZ;
BYTE *buf = new BYTE[value.GetLength() + 1];
strcpy((char *)buf, (const char *) value);
DWORD sz = value.GetLength() + 1;
ret = ::RegSetValueEx(hKey, (const char *) name, reserved, type, buf, sz);
delete[] buf;
::RegCloseKey(hKey);
if (ret == ERROR_SUCCESS)
{
return TRUE;
}
else
return FALSE;
}
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
);PHKEY phkResult,这个参数应该写什么?
HKEY m_mykey;
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 &m_mykey, // 在这呢!
LPDWORD lpdwDisposition // disposition value buffer
);
,0,"aa",REG_OPTION_NON_VOLATILE,KEY_CREATE_SUB_KEY,
NULL,&mHkey,REG_CREATED_NEW_KEY);
error C2664: 'RegCreateKeyExA' : cannot convert parameter 9 from 'const long' to 'unsigned long *'按照MSDN写的,为什么有错误?
DWORD m_dwDispoistion, m_dwLength, m_dwType, m_dwRdLen;
int m_nError;
CString m_strKeyPath;
m_strKeyPath.Format("Software\\123\\789");
m_nError=RegCreateKeyEx(HKEY_CURRENT_USER, m_strKeyPath, NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &m_key, &m_dwDispoistion);
在错我就没法了.
D:\FILES\VC++\Registry\Registry.cpp(59) : warning C4101: 'm_dwLength' : unreferenced local variable
D:\FILES\VC++\Registry\Registry.cpp(61) : warning C4101: 'm_dwRdLen' : unreferenced local variable
REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &m_key, &m_dwDispoistion);
怎么用也教教我吧
REG_OPTION_NON_VOLATILE 和 REG_OPTION_BACKUP_RESTORE
DWORD m_l;
DWORD m_dwLength;
m_dwLength=4;
m_l=123;
m_nError=RegSetValueEx( m_key, _T("HelloWorld"), NULL, REG_DWORD, (BYTE*)&m_l, m_dwLength);