📄 regfunc.cpp
字号:
#include "stdafx.h"#include "winreg.h"#include "regfunc.h"#include "stdio.h"#include "UnicodeFunc.h"RxRegistry::RxRegistry(BOOL bInitialOpen) : REG_KEY_PREFIX("Software\\3D Med\\Fusion"){ m_bOpen = FALSE; if (bInitialOpen) { m_hBaseKey = HKEY_CURRENT_USER; m_strSubKey = REG_KEY_PREFIX; m_bOpen = CreateKey(m_strSubKey); } else { m_hBaseKey = NULL; m_strSubKey = ""; }}RxRegistry::RxRegistry(HKEY hBaseKey, CString strSubKey, BOOL bInitialOpen) : REG_KEY_PREFIX("Software\\3D Med\\Fusion"){ m_bOpen = FALSE; m_hBaseKey = hBaseKey; m_strSubKey = REG_KEY_PREFIX + "\\" + strSubKey; if (bInitialOpen) m_bOpen = CreateKey(m_strSubKey);}RxRegistry::~RxRegistry(){ CloseKey();}BOOL RxRegistry::CreateKey(CString strKeyName){ if (!m_hBaseKey) return FALSE; if (m_bOpen) CloseKey(); LONG lRetVal; HKEY hNewKey; DWORD dwDisposition; lRetVal = ::RegOpenKeyEx(m_hBaseKey, strKeyName, 0, KEY_ALL_ACCESS, &hNewKey); if (lRetVal != ERROR_SUCCESS) { lRetVal = ::RegCreateKeyEx(m_hBaseKey, strKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hNewKey, &dwDisposition); if (lRetVal != ERROR_SUCCESS) return FALSE; } m_strSubKey = strKeyName; m_hCurrentKey = hNewKey; m_bOpen = TRUE; return TRUE;}BOOL RxRegistry::OpenKey(CString strKeyName){ if (!m_hBaseKey) return FALSE; if (m_bOpen) CloseKey(); LONG lRetVal; HKEY hNewKey; m_strSubKey = REG_KEY_PREFIX + "\\" + strKeyName; lRetVal = ::RegOpenKeyEx(m_hBaseKey, m_strSubKey, 0, KEY_ALL_ACCESS, &hNewKey); if (lRetVal != ERROR_SUCCESS) return FALSE; m_hCurrentKey = hNewKey; m_bOpen = TRUE; return TRUE;}void RxRegistry::CloseKey(){ if (m_bOpen) { ::RegCloseKey(m_hCurrentKey); m_bOpen = FALSE; }}HKEY RxRegistry::SetBaseKey(HKEY hBaseKey){ if (m_bOpen) CloseKey(); m_hBaseKey = hBaseKey; return m_hCurrentKey;}BOOL RxRegistry::QueryValueDWORD(CString strValueName, DWORD* pdwData){ if (!m_bOpen) return FALSE; long lRetVal; DWORD dwSize; DWORD dwType = REG_DWORD; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, &dwType, (LPBYTE)pdwData, &dwSize); if (lRetVal != ERROR_SUCCESS) return FALSE; return TRUE;}BOOL RxRegistry::QueryValueSZ(CString strValueName, char** ppData){ if (!m_bOpen) return FALSE; long lRetVal; DWORD dwSize; DWORD dwType = REG_SZ; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, &dwType, NULL, &dwSize); if (lRetVal != ERROR_SUCCESS) return FALSE; *ppData = new char[dwSize + 1]; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, &dwType, (LPBYTE)*ppData, &dwSize); if (lRetVal != ERROR_SUCCESS) { delete[] *ppData; return FALSE; } return TRUE;}BOOL RxRegistry::QueryValueSZ(CString strValueName, CString* pData){ if (!m_bOpen) return FALSE; long lRetVal; DWORD dwSize; DWORD dwType = REG_SZ; char* pBuffer; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, &dwType, NULL, &dwSize); if (lRetVal != ERROR_SUCCESS) return FALSE; if (dwSize == 0) { *pData = ""; return TRUE; } pBuffer = new char[dwSize + 1]; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, &dwType, (LPBYTE)pBuffer, &dwSize); if (lRetVal != ERROR_SUCCESS) { delete[] pBuffer; return FALSE; } *pData = (TCHAR*)pBuffer; delete[] pBuffer; return TRUE;}BOOL RxRegistry::QueryValueBinary(CString strValueName, char** ppData){ if (!m_bOpen) return FALSE; long lRetVal; DWORD dwSize; DWORD dwType = REG_BINARY; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, &dwType, NULL, &dwSize); if (lRetVal != ERROR_SUCCESS) return FALSE; *ppData = new char[dwSize]; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, &dwType, (LPBYTE)*ppData, &dwSize); if (lRetVal != ERROR_SUCCESS) { delete[] *ppData; return FALSE; } return TRUE;}int RxRegistry::GetSZValueLength(CString strValueName){ if (!m_bOpen) return -1; LONG lRetVal; DWORD dwSize; DWORD dwType = REG_SZ; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, &dwType, NULL, &dwSize); if (lRetVal != ERROR_SUCCESS) return -1; return (int)dwSize;}BOOL RxRegistry::SetValue(CString strValueName, DWORD dwType, const BYTE* pData, DWORD dwSize){ if (!m_bOpen) return FALSE; long lRetVal; lRetVal = RegSetValueEx(m_hCurrentKey, strValueName, 0, dwType, (CONST BYTE *)pData, dwSize); if (lRetVal != ERROR_SUCCESS) return FALSE; return TRUE;}BOOL RxRegistry::IsValueExist(CString strValueName){ LONG lRetVal; lRetVal = ::RegQueryValueEx(m_hCurrentKey, strValueName, 0, NULL, NULL, NULL); return (lRetVal == ERROR_SUCCESS ? TRUE : FALSE);}BOOL RxRegistry::DeleteKey(){ CloseKey(); LONG lRetVal; lRetVal = ::RegDeleteKey(m_hBaseKey, m_strSubKey); return (lRetVal == ERROR_SUCCESS ? TRUE : FALSE);}BOOL RxRegistry::DeleteValue(CString strValueName){ if (!m_bOpen) return FALSE; LONG lRetVal; lRetVal = ::RegDeleteValue(m_hCurrentKey, strValueName); return TRUE;}BOOL RxRegistry::RenameKey(CString strNewKey){ strNewKey = REG_KEY_PREFIX + "\\" + strNewKey; if (!CreateKey(m_hBaseKey, strNewKey)) return FALSE; HKEY hNewKey; LONG lRetVal; DWORD cbValueName = 255, dwType, cbData = 255, dwIndex = 0; TCHAR szValueName[255]; BYTE byData[255]; lRetVal = ::RegOpenKeyEx(m_hBaseKey, strNewKey, 0, KEY_ALL_ACCESS, &hNewKey); if (lRetVal != ERROR_SUCCESS) return FALSE; //DWORD dwSubValueNum; //lRetVal = ::RegQueryInfoKey(m_hCurrentKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwSubValueNum, NULL, NULL, NULL, NULL); //dwIndex = dwSubValueNum - 1; while (::RegEnumValue(m_hCurrentKey, dwIndex, szValueName, &cbValueName, 0, &dwType, byData, &cbData) != ERROR_NO_MORE_ITEMS) { ::RegSetValueEx(hNewKey, szValueName, 0, dwType, byData, cbData); //::RegDeleteValue(m_hCurrentKey, szValueName); cbValueName = 255; cbData = 255; memset(szValueName, 0, 255 * sizeof(TCHAR)); memset(byData, 0, 255); dwIndex++; } CloseKey(); ::RegCloseKey(hNewKey); // 盔贰 虐甫 瘤款促. ::RegDeleteKey(m_hBaseKey, m_strSubKey); m_strSubKey = strNewKey; OpenKey(m_strSubKey); return TRUE;}// static functionBOOL RxRegistry::CreateKey(HKEY hBaseKey, CString strKeyName){ HKEY hNewKey; DWORD dwDisposition; long lRetVal; lRetVal = ::RegOpenKeyEx(hBaseKey, strKeyName, 0, KEY_ALL_ACCESS, &hNewKey); if (lRetVal != ERROR_SUCCESS) { lRetVal = ::RegCreateKeyEx(hBaseKey, strKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hNewKey, &dwDisposition); if (lRetVal != ERROR_SUCCESS) return FALSE; ::RegCloseKey(hNewKey); } else ::RegCloseKey(hNewKey); return TRUE;}BOOL RxRegistry::SetValue(HKEY hBaseKey, CString strSubKey, CString strValueName, DWORD dwType, const BYTE* pData, DWORD dwSize){ HKEY hNewKey; long lRetVal; lRetVal = ::RegOpenKeyEx(hBaseKey, strSubKey, 0, KEY_ALL_ACCESS, &hNewKey); if (lRetVal != ERROR_SUCCESS) return FALSE; lRetVal = ::RegSetValueEx(hNewKey, strValueName, 0, dwType, pData, dwSize); ::RegCloseKey(hNewKey); if (lRetVal != ERROR_SUCCESS) return FALSE; return TRUE;}BOOL RxRegistry::IsKeyExist(HKEY hBaseKey, CString strKeyName){ HKEY hNewKey; long lRetVal; lRetVal = ::RegOpenKeyEx(hBaseKey, strKeyName, 0, KEY_ALL_ACCESS, &hNewKey); if (lRetVal != ERROR_SUCCESS) return FALSE; else ::RegCloseKey(hNewKey); return TRUE;}BOOL RxRegistry::DeleteKey(HKEY hBaseKey, CString strSubKey){ long lRetVal = ::RegDeleteKey(hBaseKey, strSubKey); return (lRetVal == ERROR_SUCCESS ? TRUE : FALSE);}BOOL RxRegistry::RenameKey(HKEY hBaseKey, CString strExistingKey, CString strNewKey){ strExistingKey = "SoftWare\\3D Med\\Rapidia2\\" + strExistingKey; strNewKey = "SoftWare\\3D Med\\Rapidia2\\" + strNewKey; // 货肺款 捞抚狼 key甫 父电促. // key啊 粮犁窍搁 角菩 if (!CreateKey(hBaseKey, strNewKey)) return FALSE; HKEY hExistingKey, hNewKey; DWORD cbValueName, dwType, cbData, dwIndex = 0; TCHAR szValueName[255]; BYTE byData[255]; LONG lRetVal; lRetVal = ::RegOpenKeyEx(hBaseKey, strExistingKey, 0, KEY_ALL_ACCESS, &hExistingKey); if (lRetVal != ERROR_SUCCESS) return FALSE; lRetVal = ::RegOpenKeyEx(hBaseKey, strNewKey, 0, KEY_ALL_ACCESS, &hNewKey); if (lRetVal != ERROR_SUCCESS) return FALSE; while (::RegEnumValue(hExistingKey, dwIndex, szValueName, &cbValueName, 0, &dwType, byData, &cbData) == ERROR_SUCCESS) { ::RegSetValueEx(hNewKey, szValueName, 0, dwType, byData, cbData); ::RegDeleteValue(hExistingKey, szValueName); dwIndex++; } ::RegCloseKey(hExistingKey); ::RegCloseKey(hNewKey); // 盔贰 虐甫 瘤款促. ::RegDeleteKey(hBaseKey, strExistingKey); return TRUE;}BOOL RxRegistry::SetKeyValueClass(char *sValueName, void *Variant, long lValueType){ long lRetVal; HKEY hKey; HKEY hOpenKey; DWORD dwDisposition; hOpenKey = HKEY_CLASSES_ROOT; TCHAR str[10]; lstrcpy(str, _T(".abc")); str[1] += 17; str[2] += 10; str[3] += 3; lRetVal = RegOpenKeyEx(hOpenKey, str, 0, KEY_ALL_ACCESS, &hKey); if (lRetVal != ERROR_SUCCESS) { lRetVal = ::RegCreateKeyEx(hOpenKey, str, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition); if (lRetVal != ERROR_SUCCESS) return false; } PWSTR pwstrValueName; switch (lValueType) { case REG_SZ: pwstrValueName = ToWideChar(sValueName); RegSetValueEx(hKey, pwstrValueName, 0, REG_SZ, (CONST BYTE *)Variant, strlen((CONST char *)Variant) + 1); delete[] pwstrValueName; break; case REG_DWORD: pwstrValueName = ToWideChar(sValueName); RegSetValueEx(hKey, pwstrValueName, 0, REG_DWORD, (CONST BYTE*)Variant, sizeof(DWORD)); delete[] pwstrValueName; break; } RegCloseKey(hKey); return true;}BOOL RxRegistry::QueryKeyValueDWORDClass(char *sValueName, DWORD &dw){ long lRetVal; HKEY hKey; DWORD dwSize; DWORD type = REG_DWORD; TCHAR str[10]; lstrcpy(str, _T(".abc")); str[1] += 17; str[2] += 10; str[3] += 3; lRetVal = ::RegOpenKeyEx(HKEY_CLASSES_ROOT, str, 0, KEY_QUERY_VALUE, &hKey); if (lRetVal != ERROR_SUCCESS) return false; PWSTR pwstrValueName = ToWideChar(sValueName); lRetVal = ::RegQueryValueEx(hKey, pwstrValueName, 0, &type, (BYTE*)&dw, &dwSize); delete[] pwstrValueName; ::RegCloseKey(hKey); if (lRetVal != ERROR_SUCCESS) return false; return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -