BOOL CXTPRegistryManager::WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue) { ASSERT(lpszSection != NULL); if (!lpszSection) return 0; if (m_strINIFileName.IsEmpty()) { if (lpszEntry == NULL) //delete whole section { CHKey hAppKey(GetAppRegistryKey(FALSE)); if (hAppKey == NULL) return FALSE; m_lResult = ::RegDeleteKey(hAppKey, lpszSection); if (m_lResult != ERROR_SUCCESS) return FALSE; } else if (lpszValue == NULL) { CHKey hSecKey(GetSectionKey(lpszSection)); if (hSecKey == NULL) return FALSE; // necessary to cast away const below m_lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry); if (m_lResult != ERROR_SUCCESS) return FALSE; } else { CHKey hSecKey(GetSectionKey(lpszSection)); if (hSecKey == NULL) return FALSE; m_lResult = ::RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ, (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR)); if (m_lResult != ERROR_SUCCESS) return FALSE; } return TRUE; } else { ASSERT(m_strINIFileName.IsEmpty() == FALSE); ASSERT(m_strINIFileName.GetLength() < 4095); // can't read in bigger return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue, m_strINIFileName); } }
BOOL CXTPRegistryManager::WriteProfileRect(LPCTSTR lpszSection, LPCTSTR lpszEntry, CRect* pValue) { ASSERT(lpszSection != NULL); if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection)); if (hSecKey == NULL) return FALSE; m_lResult = ::RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY, (LPBYTE)pValue, sizeof(CRect)); if (m_lResult != ERROR_SUCCESS) return FALSE; return TRUE; } CString str; str.Format(_T("%i,%i,%i,%i"), pValue->left, pValue->top, pValue->right, pValue->bottom); BOOL bResult = WriteProfileString(lpszSection, lpszEntry, str); return bResult; }
UINT CXTPRegistryManager::GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (!lpszSection || !lpszEntry) return 0; if (m_strINIFileName.IsEmpty()) // use registry { CHKey hSecKey(GetSectionKey(lpszSection, KEY_READ)); if (hSecKey == NULL) return nDefault; DWORD dwValue; DWORD dwType; DWORD dwCount = sizeof(DWORD); m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)&dwValue, &dwCount); if (m_lResult != ERROR_SUCCESS) return nDefault; ASSERT(dwType == REG_DWORD); ASSERT(dwCount == sizeof(dwValue)); return (UINT)dwValue; } else { ASSERT(m_strINIFileName.IsEmpty() == FALSE); return ::GetPrivateProfileInt(lpszSection, lpszEntry, nDefault, m_strINIFileName); } }
BOOL CXTPRegistryManager::WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (!lpszSection) return 0; if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection)); if (hSecKey == NULL) return FALSE; m_lResult = ::RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD, (LPBYTE)&nValue, sizeof(nValue)); if (m_lResult != ERROR_SUCCESS) return FALSE; return TRUE; } else { ASSERT(m_strINIFileName.IsEmpty() == FALSE); TCHAR szT[16]; wsprintf(szT, _T("%d"), nValue); return ::WritePrivateProfileString(lpszSection, lpszEntry, szT, m_strINIFileName); } }
bool CXTPRegistryManager::DeleteValue(LPCTSTR lpszSection, LPCTSTR lpszValue) { CHKey hSecKey(GetSectionKey(lpszSection, KEY_ALL_ACCESS)); if (hSecKey == NULL) return false; m_lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszValue); return (m_lResult == ERROR_SUCCESS); }
BOOL CXTPRegistryManager::GetProfileDouble(LPCTSTR lpszSection, LPCTSTR lpszEntry, double* dResult) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection, KEY_READ)); if (hSecKey == NULL) return FALSE; DWORD dwType, dwCount; m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; ASSERT(dwType == REG_BINARY); m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)dResult, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; ASSERT(dwType == REG_BINARY); return TRUE; } else { ASSERT(m_strINIFileName.IsEmpty() == FALSE); CString str = GetProfileString(lpszSection, lpszEntry, NULL); if (str.IsEmpty()) return FALSE; ASSERT(str.GetLength()%2 == 0); int nLen = str.GetLength(); LPBYTE pData = (LPBYTE) dResult; int i; for (i = 0; i < nLen; i += 2) { (pData)[i/2] = (BYTE) (((str[i + 1] - 'A') << 4) + (str[i] - 'A')); } return TRUE; } }
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; }
CString CXTPRegistryManager::GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection, KEY_READ)); if (hSecKey == NULL) return lpszDefault; CString strValue; DWORD dwType, dwCount; m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); if (m_lResult != ERROR_SUCCESS) return lpszDefault; ASSERT(dwType == REG_SZ); m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount); strValue.ReleaseBuffer(); if (m_lResult != ERROR_SUCCESS) return lpszDefault; ASSERT(dwType == REG_SZ); return strValue; } else { TCHAR chNil = '\0'; ASSERT(m_strINIFileName.IsEmpty() == FALSE); if (lpszDefault == NULL) { lpszDefault = &chNil; // don't pass in NULL } TCHAR szT[4096]; DWORD dw = ::GetPrivateProfileString(lpszSection, lpszEntry, lpszDefault, szT, _countof(szT), m_strINIFileName); ASSERT(dw < 4095); return szT; } }
BOOL CXTPRegistryManager::GetProfileSize(LPCTSTR lpszSection, LPCTSTR lpszEntry, CSize* szResult) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection, KEY_READ)); if (hSecKey == NULL) return FALSE; DWORD dwType, dwCount; m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; ASSERT(dwType == REG_BINARY); m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)szResult, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; ASSERT(dwType == REG_BINARY); return TRUE; } else { ASSERT(m_strINIFileName.IsEmpty() == FALSE); // convert to string and write out CString str = GetProfileString(lpszSection, lpszEntry, NULL); if (str.IsEmpty()) return FALSE; SCANF_S(str, _T("%i,%i"), &szResult->cx, &szResult->cy); return TRUE; } }
BOOL CXTPRegistryManager::GetProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD* dwResult) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection, KEY_READ)); if (hSecKey == NULL) return FALSE; DWORD dwType, dwCount; m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; ASSERT(dwType == REG_DWORD); m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)dwResult, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; ASSERT(dwType == REG_DWORD); return TRUE; } else { ASSERT(m_strINIFileName.IsEmpty() == FALSE); CString str = GetProfileString(lpszSection, lpszEntry, NULL); if (str.IsEmpty()) return FALSE; *dwResult = (DWORD)GetProfileInt(lpszSection, lpszEntry, 0); return TRUE; } }
BOOL CXTPRegistryManager::GetProfilePoint(LPCTSTR lpszSection, LPCTSTR lpszEntry, CPoint* ptResult) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection, KEY_READ)); if (hSecKey == NULL) return FALSE; DWORD dwType, dwCount; m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; ASSERT(dwType == REG_BINARY); m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)ptResult, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; ASSERT(dwType == REG_BINARY); return TRUE; } else { ASSERT(m_strINIFileName.IsEmpty() == FALSE); CString str = GetProfileString(lpszSection, lpszEntry, NULL); if (str.IsEmpty()) return FALSE; SCANF_S(str, _T("%ld,%ld"), &ptResult->x, &ptResult->y); return TRUE; } }
BOOL CXTPRegistryManager::WriteProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD* pValue) { ASSERT(lpszSection != NULL); if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection)); if (hSecKey == NULL) return FALSE; m_lResult = ::RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD, (LPBYTE)pValue, sizeof(*pValue)); if (m_lResult != ERROR_SUCCESS) return FALSE; return TRUE; } BOOL bResult = WriteProfileInt(lpszSection, lpszEntry, int(*pValue)); return bResult; }
BOOL CXTPRegistryManager::WriteProfileDouble(LPCTSTR lpszSection, LPCTSTR lpszEntry, double* pValue) { ASSERT(lpszSection != NULL); if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection)); if (hSecKey == NULL) return FALSE; m_lResult = ::RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY, (LPBYTE)pValue, sizeof(*pValue)); if (m_lResult != ERROR_SUCCESS) return FALSE; return TRUE; } LPBYTE pData = (LPBYTE) pValue; UINT nBytes = sizeof(double); LPTSTR lpsz = new TCHAR[nBytes * 2 + 1]; UINT i; for (i = 0; i < nBytes; i++) { lpsz[i * 2] = (TCHAR)((pData[i] & 0x0F) + 'A'); //low nibble lpsz[i * 2 + 1] = (TCHAR)(((pData[i] >> 4) & 0x0F) + 'A'); //high nibble } lpsz[i * 2] = 0; ASSERT(m_strINIFileName.IsEmpty() == FALSE); BOOL bResult = WriteProfileString(lpszSection, lpszEntry, lpsz); delete[] lpsz; return bResult; }
BOOL CXTPRegistryManager::GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); ASSERT(ppData != NULL); ASSERT(pBytes != NULL); if (!ppData || !pBytes) return FALSE; *ppData = NULL; *pBytes = 0; if (m_strINIFileName.IsEmpty()) { CHKey hSecKey(GetSectionKey(lpszSection, KEY_READ)); if (hSecKey == NULL) return FALSE; DWORD dwType, dwCount; m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); if (m_lResult != ERROR_SUCCESS) return FALSE; *pBytes = dwCount; *ppData = new BYTE[*pBytes]; ASSERT(dwType == REG_BINARY); m_lResult = ::RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, *ppData, &dwCount); if (m_lResult != ERROR_SUCCESS) { SAFE_DELETE_AR(*ppData); return FALSE; } ASSERT(dwType == REG_BINARY); return TRUE; } else { ASSERT(m_strINIFileName.IsEmpty() == FALSE); CString str = GetProfileString(lpszSection, lpszEntry, NULL); if (str.IsEmpty()) return FALSE; ASSERT(str.GetLength()%2 == 0); int nLen = str.GetLength(); *pBytes = nLen/2; *ppData = new BYTE[*pBytes]; int i; for (i = 0; i < nLen; i += 2) { (*ppData)[i/2] = (BYTE) (((str[i + 1] - 'A') << 4) + (str[i] - 'A')); } return TRUE; } }