
Опытный
 
Профиль
Группа: Участник
Сообщений: 295
Регистрация: 10.2.2005
Где: Санкт-Петербург
Репутация: 1 Всего: 3
|
Ну коль нельзя приаттачить соурс, лови код работы с реестром. Будут вопросы задавай. В *.cpp | Код | #include "StdAfx.h" #include ".\elizsoft.h"
extern FILETIME ftCurrent; extern FILETIME ftBegin; extern FILETIME ftEnd;
extern LARGE_INTEGER liCurrent; extern LARGE_INTEGER liBegin; extern LARGE_INTEGER liEnd;
extern HTREEITEM hTree; extern HTREEITEM hTreeSelect;
extern bool bReturn; extern bool bError;
extern HKEY m_hNewKey; extern long m_lReg;
extern CHAR chData[MAX_INI_BUFFER]; extern CHAR chBuff[MAX_INI_BUFFER]; extern CHAR chStr[MAX_INI_BUFFER];
extern unsigned long ulReturn; extern unsigned long ulDataType; extern unsigned long ulData; extern unsigned long ulCount; extern unsigned long ulNum; extern unsigned long ulNumber;
CEsReg::CEsReg(void) { }
CEsReg::CEsReg(HKEY hKey, LPCTSTR Section) { m_hKey=NULL; Reg_SetRegistryBranch(hKey, Section); }
CEsReg::~CEsReg(void) { }
//Private
HKEY CEsReg::Reg_GetSectionKey(HKEY Branch, LPCTSTR lpszSection, long lPermission) // Returns registry key for // Branch\lpszSection // It does not create it if it doesn't exist. // Responsibility of the caller to call RegCloseKey() on the returned HKEY { ASSERT(AfxIsValidString(lpszSection)); HKEY hKey=NULL; if(RegOpenKeyEx(Branch, lpszSection, 0, lPermission, &hKey)==ERROR_SUCCESS) { return hKey; } if (hKey!=NULL) RegCloseKey(hKey); return NULL; }
HKEY CEsReg::Reg_CreateSectionKey(HKEY Branch, LPCTSTR lpszSection) // Returns registry key for: // Branch\lpszSection // creating it if it doesn't exist. // responsibility of the caller to call RegCloseKey() on the returned HKEY { ASSERT(AfxIsValidString(lpszSection));
HKEY hKey=Reg_GetSectionKey(Branch,lpszSection); if(hKey!=NULL) return hKey;
DWORD dw; RegCreateKeyEx(Branch, lpszSection, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hKey, &dw); return hKey; }
//Public
bool CEsReg::Reg_SetRegistryBranch(HKEY hKey, LPCTSTR Section) { ASSERT(hKey); ASSERT(AfxIsValidString(Section)); m_hKey=hKey; m_strSection=Section; ASSERT(!m_strSection.IsEmpty()); return true; }
unsigned long CEsReg::Reg_GetNumber(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(AfxIsValidString(lpszEntry)); // HKEY hKey=Reg_GetSectionKey(Branch,lpszSection,KEY_READ); if (hKey==NULL) return nDefault; DWORD dwValue; DWORD dwType; DWORD dwCount=sizeof(DWORD); LONG lResult=::RegQueryValueEx(hKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)&dwValue, &dwCount); ::RegCloseKey(hKey); if(lResult==ERROR_SUCCESS) { ASSERT(dwType==REG_DWORD); ASSERT(dwCount==sizeof(dwValue)); return (UINT)dwValue; } return nDefault; }
unsigned long CEsReg::Reg_GetNumber(LPCTSTR lpszEntry, int nDefault) { ASSERT(m_hKey); if(m_hKey==NULL) return 0; return Reg_GetNumber(m_hKey, m_strSection, lpszEntry, nDefault); }
CString CEsReg::Reg_GetString(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault) { ASSERT(AfxIsValidString(lpszSection)); // HKEY hKey=Reg_GetSectionKey(Branch,lpszSection,KEY_READ); if(hKey==NULL) return lpszDefault; CString strValue; DWORD dwType, dwCount; LONG lResult=::RegQueryValueEx(hKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); if(lResult==ERROR_SUCCESS) { ASSERT((dwType==REG_SZ) || (dwType==REG_EXPAND_SZ) || (dwType==REG_MULTI_SZ)); lResult=::RegQueryValueEx(hKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount); strValue.ReleaseBuffer(); } ::RegCloseKey(hKey); if(lResult==ERROR_SUCCESS) { ASSERT((dwType==REG_SZ) || (dwType==REG_EXPAND_SZ) || (dwType==REG_MULTI_SZ)); return strValue; } return lpszDefault; }
CString CEsReg::Reg_GetString(LPCTSTR lpszEntry, LPCTSTR lpszDefault) { ASSERT(m_hKey); if(m_hKey==NULL) return CString(); return Reg_GetString(m_hKey, m_strSection, lpszEntry, lpszDefault); }
bool CEsReg::Reg_GetBinary(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(ppData!=NULL); ASSERT(pBytes!=NULL); // *ppData=NULL; *pBytes=0; LPBYTE lpByte=NULL; HKEY hKey=Reg_GetSectionKey(Branch,lpszSection,KEY_READ); if(hKey==NULL) return FALSE;
DWORD dwType, dwCount; LONG lResult=::RegQueryValueEx(hKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); *pBytes=dwCount; if(lResult==ERROR_SUCCESS) { ASSERT(dwType==REG_BINARY); *ppData=new BYTE[*pBytes]; lResult=::RegQueryValueEx(hKey, (LPTSTR)lpszEntry, NULL, &dwType, *ppData, &dwCount); } ::RegCloseKey(hKey); if(lResult==ERROR_SUCCESS) { ASSERT(dwType==REG_BINARY); return true; } else { delete [] *ppData; *ppData=NULL; } return false; }
bool CEsReg::Reg_WriteNumber(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, unsigned long nValue) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(AfxIsValidString(lpszEntry)); HKEY hKey=Reg_CreateSectionKey(Branch,lpszSection); if(hKey==NULL) return false; LONG lResult=::RegSetValueEx(hKey, lpszEntry, NULL, REG_DWORD, (LPBYTE)&nValue, sizeof(nValue)); ::RegCloseKey(hKey); return lResult==ERROR_SUCCESS; }
bool CEsReg::Reg_WriteString(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(AfxIsValidString(lpszEntry)); ASSERT(AfxIsValidString(lpszValue)); LONG lResult; HKEY hKey=Reg_CreateSectionKey(Branch,lpszSection); if(hKey==NULL) return false; lResult=::RegSetValueEx(hKey, lpszEntry, NULL, REG_SZ, (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR)); ::RegCloseKey(hKey); return lResult==ERROR_SUCCESS; }
bool CEsReg::Reg_WriteExpandString(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(AfxIsValidString(lpszEntry)); ASSERT(AfxIsValidString(lpszValue)); LONG lResult; HKEY hKey=Reg_CreateSectionKey(Branch,lpszSection); if(hKey==NULL) return false; lResult=::RegSetValueEx(hKey, lpszEntry, NULL, REG_EXPAND_SZ, (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR)); ::RegCloseKey(hKey); return lResult==ERROR_SUCCESS; }
bool CEsReg::Reg_WriteMultiString(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(AfxIsValidString(lpszEntry)); ASSERT(AfxIsValidString(lpszValue)); LONG lResult; HKEY hKey=Reg_CreateSectionKey(Branch,lpszSection); if(hKey==NULL) return false; lResult=::RegSetValueEx(hKey, lpszEntry, NULL, REG_MULTI_SZ, (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR)); ::RegCloseKey(hKey); return lResult==ERROR_SUCCESS; }
bool CEsReg::Reg_WriteBinary(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(AfxIsValidString(lpszEntry)); ASSERT(pData!=NULL); LONG lResult; HKEY hKey=Reg_CreateSectionKey(Branch,lpszSection); if(hKey==NULL) return false; lResult=::RegSetValueEx(hKey, lpszEntry, NULL, REG_BINARY, pData, nBytes); ::RegCloseKey(hKey); return lResult==ERROR_SUCCESS; }
long CEsReg::Reg_DeleteValue(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszValue) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(AfxIsValidString(lpszValue)); LONG lResult; HKEY hKey=Reg_GetSectionKey(Branch, lpszSection, KEY_WRITE|KEY_READ); lResult=::RegDeleteValue(hKey, lpszValue); ::RegCloseKey(hKey); return lResult; //==ERROR_SUCCESS; };
bool CEsReg::Reg_DeleteKey(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry) { ASSERT(AfxIsValidString(lpszSection)); ASSERT(AfxIsValidString(lpszEntry)); LONG lResult; HKEY hKey=Reg_GetSectionKey(Branch, lpszSection, KEY_WRITE|KEY_READ); lResult=::RegDeleteKey(hKey, lpszEntry); ::RegCloseKey(hKey); return lResult==ERROR_SUCCESS; };
long CEsReg::Reg_DeleteSection(HKEY hParentKey, const CString& strKeyName) // Under Win32, a reg key may not be deleted unless it is empty. // Thus, to delete a tree, one must recursively enumerate and // delete all of the sub-keys. { #ifdef CWinApp__DelRegTree return CWinApp::DelRegTree(hParentKey,strKeyName); #else #define MAX_KEY_LENGTH 300 DWORD dwIndex=0L; TCHAR szSubKeyName[MAX_KEY_LENGTH]; HKEY hCurrentKey; DWORD dwResult;
if((dwResult=::RegOpenKey(hParentKey, strKeyName, &hCurrentKey))==ERROR_SUCCESS) { // Remove all subkeys of the key to delete while((dwResult=::RegEnumKey(hCurrentKey, 0, szSubKeyName, MAX_KEY_LENGTH-1))==ERROR_SUCCESS) { if((dwResult=Reg_DeleteSection(hCurrentKey, szSubKeyName))!=ERROR_SUCCESS) break; }; // If all went well, we should now be able to delete the requested key if ((dwResult==ERROR_NO_MORE_ITEMS) || (dwResult==ERROR_BADKEY)) { dwResult=::RegDeleteKey(hParentKey, strKeyName); }; }; RegCloseKey(hCurrentKey); return dwResult; #endif }
unsigned long CEsReg::Reg_GetNumBranch(HKEY Branch, LPCTSTR Section) { HKEY hKey; unsigned long ulBranch=0; unsigned long ulEnumIndex=0; unsigned long ulSubkey=0; long lResult;
ulEnumIndex=0; RegOpenKeyEx(Branch, Section, 0, KEY_READ, &hKey); while(true) { ZeroMemory(chData, MAX_INI_BUFFER); ulSubkey=sizeof(chData); lResult=RegEnumKeyEx(hKey, ulEnumIndex++, chData, &ulSubkey, 0, 0, 0, 0);
if(ERROR_NO_MORE_ITEMS==lResult) break;
if(lResult==0) { ulBranch+=1; } } RegCloseKey(hKey);
return ulBranch; }
|
В *.h | Код | #pragma once
#include "resource.h"
class AFX_EXT_CLASS CEsReg { public: CEsReg(void); CEsReg(HKEY hKey, LPCTSTR Section); virtual ~CEsReg(void);
// Implementation private: HKEY Reg_GetSectionKey(HKEY Branch, LPCTSTR lpszSection, long lPermission=KEY_WRITE|KEY_READ); HKEY Reg_CreateSectionKey(HKEY Branch, LPCTSTR lpszSection);
// Attributes protected: HKEY m_hKey; // The current registry root. CString m_strSection; // The current registry branch.
public: // Sets the current registry branch. bool Reg_SetRegistryBranch(HKEY hKey, LPCTSTR Section); // // Получить числовой параметр из реестра unsigned long Reg_GetNumber(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault=0); // Получить числовой параметр из реестра unsigned long Reg_GetNumber(LPCTSTR lpszEntry, int nDefault=0); // Получить строковый параметр из реестра CString Reg_GetString(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault=NULL); // Получить строковый параметр из реестра CString Reg_GetString(LPCTSTR lpszEntry, LPCTSTR lpszDefault=NULL); // Получить двоичный параметр из реестра bool Reg_GetBinary(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes); // Записать числовой параметр в реестр bool Reg_WriteNumber(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, unsigned long nValue); // Записать строковый параметр в реестр bool Reg_WriteString(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue); // Записать расширенный строковый параметр в реестр bool Reg_WriteExpandString(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue); // Записать мульти строковый параметр в реестр bool Reg_WriteMultiString(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue); // Записать двоичный параметр в реестр bool Reg_WriteBinary(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes); // Delete a registry key. bool Reg_DeleteKey(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszEntry); // Deletes a value long Reg_DeleteValue(HKEY Branch, LPCTSTR lpszSection, LPCTSTR lpszValue); // Delete an entire registry branch. long Reg_DeleteSection(HKEY hParentKey, const CString& strKeyName); //Получить количество папок в секции unsigned long Reg_GetNumBranch(HKEY Branch, LPCTSTR Section); };
|
--------------------
"Нужно делать так, как нужно, а как не нужно, делать не нужно" (с) Винни-Пух.
|