BOOL CRegistry::Read (LPCTSTR pszKey, CByteArray& bcArray) { ASSERT(m_hKey); const int iMaxChars = 4096; int OldSize = (int) 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 CRegistry::Write (LPCTSTR pszKey, CByteArray& bcArray) { if (m_bReadOnly) { ASSERT (FALSE); return FALSE; } ASSERT(m_hKey); 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 = (DWORD) 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; }