//------------------------------------------------------------------------- BOOL MRCWriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPVOID pData, DWORD nBufferSize) // Write a binary value into the registry. If the pointer to the buffer // is NULL then the current value is deleted. This can be generally used // for removing any value not just binary ones //------------------------------------------------------------------------- { CWinApp * pApp = AfxGetApp(); ASSERT(pApp != NULL); ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); ASSERT(pApp->m_pszRegistryKey != NULL); // We must be using the registry not // INI files for binary to be supported LONG lRes; HKEY hSecKey = pApp->GetSectionKey(lpszSection); if (hSecKey == NULL) return FALSE; if (pData == NULL) { lRes = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry); } else { lRes = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY, (LPBYTE)pData, nBufferSize); } RegCloseKey(hSecKey); return (lRes == ERROR_SUCCESS) ? TRUE : FALSE; }
//------------------------------------------------------------------------- BOOL MRCGetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPVOID pData, DWORD nBufferSize) // Read the registry for a binary value. // Various assertions fail if the size of the value does not match that // which is asked for. We can assume that the registry data should always // match the current software. Any conversion required between versions // should have taken place when initializing the app //------------------------------------------------------------------------- { CWinApp * pApp = AfxGetApp(); ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); ASSERT(pApp->m_pszRegistryKey != NULL); // We must be using the registry not INI files //for binary to be supported HKEY hSecKey = pApp->GetSectionKey(lpszSection); if (hSecKey == NULL) return FALSE; DWORD dwType; DWORD dwCount = nBufferSize; LONG lRes = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, (unsigned long *)&dwType, (LPBYTE)pData, (unsigned long *)&dwCount); RegCloseKey(hSecKey); ASSERT(lRes != ERROR_MORE_DATA); // Is Data in the registry larger than the buffer? // We should have converted registry data on start up. if (lRes == ERROR_SUCCESS) { ASSERT(dwType == REG_BINARY); ASSERT(dwCount = nBufferSize); // The data should be the expected size return TRUE; } return FALSE; }
BOOL CAuthDlg::OnInitDialog() { CDialog::OnInitDialog(); CWinApp* pApp = AfxGetApp(); if (pApp->m_pszRegistryKey) { CRegKey hSecKey(pApp->GetSectionKey(IDS_R_LOGINS)); if (hSecKey) { int i = 0; TCHAR username[256], password[256]; for (;;) { DWORD unlen = _countof(username); DWORD pwlen = sizeof(password); DWORD type = REG_SZ; if (ERROR_SUCCESS == RegEnumValue(hSecKey, i++, username, &unlen, 0, &type, (BYTE*)password, &pwlen)) { m_logins[username] = DEncrypt(password); m_usernamectrl.AddString(username); } else { break; } } } } else { CAutoVectorPtr<TCHAR> buff; buff.Allocate(32767/sizeof(TCHAR)); DWORD len = GetPrivateProfileSection(IDS_R_LOGINS, buff, 32767/sizeof(TCHAR), pApp->m_pszProfileName); TCHAR* p = buff; while (*p && len > 0) { CString str = p; p += str.GetLength()+1; len -= str.GetLength()+1; CAtlList<CString> sl; Explode(str, sl, '=', 2); if (sl.GetCount() == 2) { m_logins[sl.GetHead()] = DEncrypt(sl.GetTail()); m_usernamectrl.AddString(sl.GetHead()); } } } m_usernamectrl.SetFocus(); return TRUE; }
//----------------------------------------------------------------------------------------- LPVOID MRCGetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, int * pBytesRead) // similar to function above, but allocates a block of data to read into and returns // a pointer to it. User must delete this area when no longer required //----------------------------------------------------------------------------------------- { CWinApp * pApp = AfxGetApp(); ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); ASSERT(pApp->m_pszRegistryKey != NULL); // We must be using the registry not INI files HKEY hSecKey = pApp->GetSectionKey(lpszSection); if (hSecKey == NULL) return NULL; LPBYTE lpValue = NULL; DWORD dwType, dwCount; LONG lRes = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, (unsigned long *)&dwType, NULL, (unsigned long *)&dwCount); if (lRes == ERROR_SUCCESS) { lpValue = new BYTE[dwCount]; ASSERT(dwType == REG_BINARY); lRes = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, (unsigned long *)&dwType, lpValue, (unsigned long *)&dwCount); if (lRes != ERROR_SUCCESS) { delete lpValue; // error reading - deallocate the memory lpValue = NULL; } } RegCloseKey(hSecKey); if (pBytesRead != NULL) // return length if we want this *pBytesRead = dwCount; return lpValue; }