BOOL COXHistoryCombo::RestoreContents(LPCTSTR pszValueName, 
									  LPCTSTR pszCompany /* = NULL */, 
									  LPCTSTR pszApplication /* = NULL*/ , 
									  HKEY hKeyRoot /* = HKEY_CURRENT_USER */, 
									  BOOL bRestoreFirstItem /* = TRUE */)
{
	ASSERT_VALID(this);

	CString sValueName(pszValueName);
	CString sCompany(pszCompany);
	CString sApplication(pszApplication);
	CString sContents;

	if (sValueName.IsEmpty())
	{
		TRACE0("COXHistoryCombo::SaveContents : No value name provided, failing\n");
		return FALSE;
	}

	if (sCompany.IsEmpty())
		sCompany = AfxGetApp()->m_pszRegistryKey;
	if (sCompany.IsEmpty())
	{
		TRACE0("COXHistoryCombo::SaveContents : No valid company name is provided, failing\n");
		return FALSE;
	}

	if (sApplication.IsEmpty())
		sApplication = AfxGetApp()->m_pszProfileName;

	BOOL bSuccess = LoadContentsFromRegistry(hKeyRoot, sCompany, sApplication, sValueName, sContents);
	if (bSuccess)
	{
		// ... Set the contents in the list box
		SetContents(sContents);

		// Select the first item from the list
		if (bRestoreFirstItem)
		{
			SetCurSel(0);

			if (::IsWindow(GetSafeHwnd())
				&& GetFocus()==this && ::IsWindowEnabled(m_hWnd))
			SetEditSel(0, -1);
		}
	}

	ASSERT_VALID(this);
	return bSuccess;
}
示例#2
0
void TestRegUtils::TestSetDWORDValue()
{
	CStdString sKey(_T("SOFTWARE\\Workshare\\Testing\\TestKeyRegUtils"));
	assertMessage(RegUtils::CreateKey(HKEY_LOCAL_MACHINE, sKey),_T("Failed to create key for test"));
	
	HKEY hOpenedKey = RegUtils::OpenKey(HKEY_LOCAL_MACHINE, sKey);
	CStdString sValueName(_T("NewDWORDValue"));

	assertMessage(RegUtils::SetRegDWORDValue(HKEY_LOCAL_MACHINE, sKey, sValueName, 0),_T("Failed to set the DWORD value for the test key"));
	assertMessage(0 == RegUtils::GetDWORDFromRegistry(hOpenedKey, sValueName),_T("Failed to get the correct value from the registry"));

	assertMessage(RegUtils::SetRegDWORDValue(HKEY_LOCAL_MACHINE, sKey, sValueName, 1),_T("Failed to set the DWORD value for the test key"));
	assertMessage(1 == RegUtils::GetDWORDFromRegistry(hOpenedKey, sValueName),_T("Failed to get the correct value from the registry"));

	assertMessage( RegUtils::RecursiveDeleteKey(HKEY_LOCAL_MACHINE, sKey), _T("Unable to delete test key when cleaning up test"));
}
BOOL COXHistoryCombo::SaveContents(LPCTSTR pszValueName, 
								   LPCTSTR pszCompany /* = NULL */, 
								   LPCTSTR pszApplication /* = NULL*/ , 
								   HKEY hKeyRoot /* = HKEY_CURRENT_USER */, 
								   BOOL bAddNewItem /* = TRUE */)
{
	ASSERT_VALID(this);

	CString sValueName(pszValueName);
	CString sCompany(pszCompany);
	CString sApplication(pszApplication);
	CString sContents;

	if (sValueName.IsEmpty())
	{
		TRACE0("COXHistoryCombo::SaveContents : No value name provided, failing\n");
		return FALSE;
	}

	if (sCompany.IsEmpty())
		sCompany = AfxGetApp()->m_pszRegistryKey;
	if (sCompany.IsEmpty())
	{
		TRACE0("COXHistoryCombo::SaveContents : No valid company name is provided, failing\n");
		return FALSE;
	}

	if (sApplication.IsEmpty())
		sApplication = AfxGetApp()->m_pszProfileName;

	// Add the current contents of the dit control to the list (if not yet one)
	if (bAddNewItem)
		AddNewItem();

	// Get the contents from the list
	sContents = GetContents();

	BOOL bSuccess = SaveContentsToRegistry(hKeyRoot, sCompany, sApplication, sValueName, sContents);

	ASSERT_VALID(this);
	return bSuccess;
}
示例#4
0
// Get system font file path
std::string GetSystemFontFile(const std::string &faceName) {

	static const LPCSTR fontRegistryPath = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";
	HKEY hKey;
	LONG result;
	std::string sFaceName(faceName.begin(), faceName.end());

	// Open Windows font registry key
	result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, fontRegistryPath, 0, KEY_READ, &hKey);
	if (result != ERROR_SUCCESS) {
		return "";
	}

	DWORD maxValueNameSize, maxValueDataSize;
	result = RegQueryInfoKey(hKey, 0, 0, 0, 0, 0, 0, 0, &maxValueNameSize, &maxValueDataSize, 0, 0);
	if (result != ERROR_SUCCESS) {
		return "";
	}

	DWORD valueIndex = 0;
	LPSTR valueName = new TCHAR[maxValueNameSize];
	LPBYTE valueData = new BYTE[maxValueDataSize];
	DWORD valueNameSize, valueDataSize, valueType;
	std::string sFontFile;

	// Look for a matching font name
	do {

		sFontFile.clear();
		valueDataSize = maxValueDataSize;
		valueNameSize = maxValueNameSize;

		result = RegEnumValue(hKey, valueIndex, valueName, &valueNameSize, 0, &valueType, valueData, &valueDataSize);

		valueIndex++;

		if (result != ERROR_SUCCESS || valueType != REG_SZ) {
			continue;
		}

		std::string sValueName(valueName, valueNameSize);
		
		// Found a match
		if (strncmp(sFaceName.c_str(), sValueName.c_str(), sFaceName.length()) == 0) {

			sFontFile.assign((LPSTR)valueData, valueDataSize);
			break;
		}
	} while (result != ERROR_NO_MORE_ITEMS);

	delete[] valueName;
	delete[] valueData;

	RegCloseKey(hKey);

	if (sFontFile.empty()) {
		return "";
	}

	// Build full font file path
	TCHAR winDir[MAX_PATH];
	GetWindowsDirectory(winDir, MAX_PATH);

	std::stringstream ss;
	ss << winDir << "\\Fonts\\" << sFontFile;
	sFontFile = ss.str();

	return std::string(sFontFile.begin(), sFontFile.end());
}