Example #1
0
BOOL UpdateHighlights(BOOL fAlways)
{
	HWND hwndGridView;

	if ((hwndGridView = GetDlgItem(GetBookmarkWnd(), IDC_HIGHLIGHT_GRIDVIEW)) == 0)
		return FALSE;

	SendMessage(hwndGridView, WM_SETREDRAW, FALSE, 0);

	if(fAlways)
	{
		WIN32_FIND_DATA win32fd;
		HANDLE hFind;
		TCHAR szBookPath[MAX_PATH];
		TCHAR szFilePath[MAX_PATH];

		GetProgramDataPath(szBookPath, MAX_PATH);
		lstrcat(szBookPath, TEXT("\\Bookmarks\\*"));

		// enumerate all files in the bookmarks directory
		if((hFind = FindFirstFile(szBookPath, &win32fd)) != 0)
		{
			do 
			{
				if((win32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
				{
					GetProgramDataPath(szBookPath, MAX_PATH);
					wsprintf(szFilePath, TEXT("%s\\Bookmarks\\%s"), szBookPath, win32fd.cFileName);				

					UpdateHighlight(0, szFilePath, TRUE);
				}

			} while(FindNextFile(hFind, &win32fd));

			FindClose(hFind);
		}
	}
	//else
	{
		int i;
		HWND hwndHexView;

		// add bookmarks for all open files
		for(i = 0; (hwndHexView = EnumHexView(g_hwndMain, i)) != 0; i++)
		{
			TCHAR szFilePath[MAX_PATH];
			HexView_GetFileName(hwndHexView, szFilePath, MAX_PATH);
			UpdateHighlight(szFilePath, 0, FALSE);
		}
	}

	SendMessage(hwndGridView, WM_SETREDRAW, TRUE, 0);
	return TRUE;
}
Example #2
0
BOOL GetBookmarkFileName(HWND hwndHexView, LPCTSTR szFileName, TCHAR *szBookPath)
{
	TCHAR szBase[MAX_PATH] = {0};
	TCHAR szAppData[MAX_PATH];
	TCHAR *pszName;
	UINT64 bookid;
	
	if(szFileName == 0)
	{
		szFileName = szBase;

		if(!HexView_GetFileName(hwndHexView, szBase, MAX_PATH))
			return FALSE;
	}

	// has this file got a bookmark id already??
	if(LoadFileData(szFileName, TEXT("HexEdit.bookmark"), &bookid, sizeof(UINT64)) != sizeof(UINT64))
	{
		//BY_HANDLE_FILE_INFORMATION bhfi;
		//HANDLE hFile = CreateFile(szBase, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);

		//if(hFile == INVALID_HANDLE_VALUE)
		//	return FALSE;

		// generate one from the base NTFS fileid
		//GetFileInformationByHandle(hFile, &bhfi);
		//CloseHandle(hFile);

		bookid = GetTickCount();//(UINT64)bhfi.nFileIndexLow | ((UINT64)bhfi.nFileIndexHigh << 32);

		// only save if the file exists
		if(GetFileAttributes(szFileName) != INVALID_FILE_ATTRIBUTES)
		{
			SaveFileData(szFileName, TEXT("HexEdit.bookmark"), &bookid, sizeof(UINT64));
		}
	}

	if((pszName = _tcsrchr(szFileName, '\\')) == 0)
		return FALSE;

	// get location of application data

	//
	// get/create the following directory:
	//
	//	{ApplicationData}\Local\Catch22\Bookmarks 
	//
	GetProgramDataPath(szAppData, MAX_PATH);
	lstrcat(szAppData, TEXT("\\Bookmarks"));
	SHCreateDirectory(NULL, szAppData);

	//
	//	Formulate the bookmark filename
	//
	wsprintf(szBookPath, TEXT("%s\\%s-%08x-bookmark.xml"), szAppData, pszName+1, bookid);
	//lstrcat(szBookPath, pszName+1);
	//lstrcat(szBookPath, TEXT(".bookmark"));

	return TRUE;
}
Example #3
0
BOOL CCopyHandlerApp::InitInstance()
{
	// ================================= Crash handling =======================================
	SetUnhandledExceptionFilter(&MyUnhandledExceptionFilter);

	// ================================= Configuration ========================================
	CString strPath;
	CString strCfgPath;
	CString strLogPath;

	// note that the GetProgramDataPath() below should create a directory; ExpandPath() could
	// depend on the directory to be created earlier
	if(!GetProgramDataPath(strPath))
	{
		AfxMessageBox(_T("Cannot initialize Copy Handler (data path cannot be established)."), MB_ICONERROR | MB_OK);
		return FALSE;
	}

	strCfgPath = strPath + _T("\\ch.ini");

	// initialize configuration file
	m_config.set_callback(ConfigPropertyChangedCallback, NULL);

	// read the configuration
	try
	{
		m_config.read(strCfgPath);
	}
	catch(...)
	{
	}

	// set working dir for the engine
	icpf::config& rConfig = GetConfig();

//	rConfig.SetBasePath(strPath);
	// register all properties
	RegisterProperties(&rConfig);

	// ================================= Logging ========================================
	// initialize the global log file if it is requested by configuration file
	strLogPath = strPath +  + _T("\\ch.log");

	chcore::TLogger& rLogger = chcore::TLogger::Acquire();
	try
	{
		rLogger.init(strLogPath, (int_t)m_config.get_signed_num(PP_LOGMAXSIZE), (int_t)rConfig.get_unsigned_num(PP_LOGLEVEL), false, false);
		rLogger.Enable(m_config.get_bool(PP_LOGENABLELOGGING));
	}
	catch(...)
	{
		BOOST_ASSERT(false);
	}

	LOG_INFO(_T("============================ Initializing Copy Handler ============================"));
	LOG_INFO(_T(""));

	// ================================= COM ========================================
	LOG_INFO(_T("Initializing COM"));

	HRESULT hResult = CoInitializeEx(NULL, COINIT_MULTITHREADED);
	if(FAILED(hResult))
	{
		CString strMsg;
		strMsg.Format(_T("Cannot initialize COM, the application will now exit (result = 0x%lx)"), hResult);

		LOG_ERROR(strMsg);
		AfxMessageBox(strMsg, MB_ICONERROR | MB_OK);
		return FALSE;
	}

	// ================================= Resource manager ========================================
	LOG_INFO(_T("Initializing resource manager..."));

	ictranslate::CResourceManager& rResManager = ictranslate::CResourceManager::Acquire();

	// set current language
	TCHAR szPath[_MAX_PATH];

	rResManager.Init(AfxGetInstanceHandle());
	rResManager.SetCallback(ResManCallback);
	rConfig.get_string(PP_PLANGUAGE, szPath, _MAX_PATH);
	TRACE(_T("Help path=%s\n"), szPath);
	if(!rResManager.SetLanguage(ExpandPath(szPath)))
	{
		TCHAR szData[2048];
		_sntprintf(szData, 2048, _T("Couldn't find the language file specified in configuration file:\n%s\nPlease correct this path to point the language file to use.\nProgram will now exit."), szPath);
		LOG_ERROR(szData);
		AfxMessageBox(szData, MB_ICONSTOP | MB_OK);
		return FALSE;
	}

	UpdateHelpPaths();

	// for dialogs
	ictranslate::CLanguageDialog::SetResManager(&rResManager);

	EnableHtmlHelp();

	// ================================= Checking for running instances of CH ========================================
	// check instance - return false if it's the second one
	LOG_INFO(_T("Checking for other running instances of Copy Handler"));
	if(!IsFirstInstance())
	{
		LOG_WARNING(_T("Other instance of Copy Handler is already running. Exiting."));
		MsgBox(IDS_ONECOPY_STRING);
		return FALSE;
	}

	// ================================= Common controls ========================================
	LOG_INFO(_T("Initializing GUI common controls"));

	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	// ================================= Shell extension ========================================
	LOG_INFO(_T("Initializing shared memory for communication with shell extension"));

	m_hMapObject = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(CSharedConfigStruct), _T("CHLMFile"));
	if (m_hMapObject == NULL)
		return FALSE; 
	
	// Get a pointer to the file-mapped shared memory.
	g_pscsShared=(CSharedConfigStruct*)MapViewOfFile(m_hMapObject, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
	if (g_pscsShared == NULL) 
		return FALSE; 

	LOG_INFO(_T("Checking shell extension compatibility"));

	// calculate ch version
	long lCHVersion = PRODUCT_VERSION1 << 24 | PRODUCT_VERSION2 << 16 | PRODUCT_VERSION3 << 8 | PRODUCT_VERSION4;

	// check the version of shell extension
	LONG lVersion = 0;
	BSTR bstrVersion = NULL;

	hResult = CoCreateInstance(CLSID_CShellExtControl, NULL, CLSCTX_ALL, IID_IShellExtControl, (void**)&m_piShellExtControl);
	if(SUCCEEDED(hResult) && !m_piShellExtControl)
		hResult = E_FAIL;
	if(SUCCEEDED(hResult))
		hResult = m_piShellExtControl->GetVersion(&lVersion, &bstrVersion);
	if(SUCCEEDED(hResult) && lVersion == lCHVersion)
		hResult = m_piShellExtControl->SetFlags(eShellExt_Enabled, eShellExt_Enabled);
	if(FAILED(hResult) || lCHVersion != lVersion)
	{
		CString strMsg;
		strMsg.Format(_T("Shell extension has different version (0x%lx) than Copy Handler (0x%lx). Shell extension will be disabled."), lVersion, lCHVersion);

		LOG_WARNING(strMsg);
		MsgBox(IDS_SHELL_EXTENSION_MISMATCH_STRING);

		if(m_piShellExtControl)
			m_piShellExtControl->SetFlags(0, eShellExt_Enabled);
	}

	if(bstrVersion)
		::SysFreeString(bstrVersion);

	// ================================= Initial settings ========================================
	LOG_INFO(_T("Applying initial settings"));

	// set this process priority class
	HANDLE hProcess=GetCurrentProcess();
	::SetPriorityClass(hProcess, (DWORD)rConfig.get_signed_num(PP_PPROCESSPRIORITYCLASS));

#ifndef _DEBUG		// for easier writing the program - doesn't collide with std CH
	// set "run with system" registry settings
	SetAutorun(rConfig.get_bool(PP_PRELOADAFTERRESTART));
#endif

	// ================================= Main window ========================================
	LOG_INFO(_T("Creating main application window"));
	// create main window
	m_pMainWindow=new CMainWnd;
	if (!((CMainWnd*)m_pMainWindow)->Create())
		return FALSE;				// will be deleted at destructor

	m_pMainWnd = m_pMainWindow;
	CWinApp::InitInstance();

	LOG_INFO(_T("Copy Handler initialized successfully"));

	return TRUE;
}
Example #4
0
BOOL UpdateHighlights2(HWND hwndHexView, HWND hwndGridView)
{

	HGRIDITEM hGridItem;
	hGridItem = GVI_ROOT;//GridView_InsertChild(hwndGridView, GVI_ROOT, &gvitem);

	SendMessage(hwndGridView, WM_SETREDRAW, FALSE, 0);
	GridView_DeleteAll(hwndGridView);

	if(hwndHexView == 0)
	{
		// just add bookmarks for specified hexview
		FillHexViewHighlights(GVI_ROOT, hwndHexView, hwndGridView, 0);
	}
	else if(hwndHexView != (HWND)-1)
	{
		int i;

		// add bookmarks for all open files
		for(i = 0; (hwndHexView = EnumHexView(g_hwndMain, i)) != 0; i++)
		{
			AddHexViewHighlights(GVI_ROOT, hwndHexView, hwndGridView, 0);
		}
	}
	else 
	{
		WIN32_FIND_DATA win32fd;
		HANDLE hFind;
		TCHAR szBookPath[MAX_PATH];
		TCHAR szFilePath[MAX_PATH];
		HCONFIG hConf ;

		GetProgramDataPath(szBookPath, MAX_PATH);
		lstrcat(szBookPath, TEXT("\\Bookmarks\\*"));

		// enumerate all files in the bookmarks directory
		if((hFind = FindFirstFile(szBookPath, &win32fd)) != 0)
		{
			do 
			{
				if((win32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
				{
					HWND hwndHV; 
					int i;
					HCONFIG hBookmarks, bookmark;
					HGRIDITEM hItem;

					GetProgramDataPath(szBookPath, MAX_PATH);
					wsprintf(szFilePath, TEXT("%s\\Bookmarks\\%s"), szBookPath, win32fd.cFileName);				

					hConf = OpenConfig(szFilePath);
					GetConfigStr(hConf, TEXT("hexFileData\\filePath"), szFilePath, MAX_PATH, 0);

					if((hwndHV = HexIsOpen(g_hwndMain, szFilePath, 0)) != 0)
					{
						AddHexViewHighlights(GVI_ROOT, hwndHV, hwndGridView, 0);
					}
					else
					{
						hItem = CreateBookmarkFileRoot(GVI_ROOT, szFilePath, hwndGridView, FALSE);
					//EnumHighlights(szBookPath, addgv, hwndGridView);

						hBookmarks = OpenConfigSection(hConf, TEXT("hexFileData\\bookmarks"));

						for(i = 0; bookmark = EnumConfigSection(hBookmarks, TEXT("bookmark"), i); i++)
						{
							TCHAR name[100], desc[100];
							BOOKMARK bm = { 0, 0, 0, 0, 0, name, 100, desc, 100 };

							GetConfigBookmark(bookmark, &bm);

							FillGridView2(hwndGridView, hItem, NULL, &bm, 0);
						//callback(&bm, param);	
						}
					}

					CloseConfig(hConf);
				}

			} while(FindNextFile(hFind, &win32fd));

			FindClose(hFind);
		}


	}

/*	for(hbm = 0; (hbm = HexView_EnumBookmark(hwndHexView, hbm, &bm)) != 0; )
	{
		TCHAR szOffset[32];
		TCHAR szTitle[32];

		_stprintf(szTitle, TEXT("%s"), bm.pszTitle);
		_stprintf(szOffset, TEXT("%08I64X  (%d bytes)"), bm.offset, bm.length);

		FillGridView2(hwndGridView, hGridItem, hbm, szTitle, szOffset, bm.pszText);
	}*/

	SendMessage(hwndGridView, WM_SETREDRAW, TRUE, 0);
	GridView_Update(hwndGridView);

	//ForceClientResize(hwndGridView);
	//InvalidateRect(hwndGridView, 0, 0);

	return TRUE;
}
Example #5
0
void DeleteBookmarkFromConfig(LPCTSTR szFilename, LPCTSTR szBookmarkName)
{
	WIN32_FIND_DATA win32fd;
	HANDLE hFind;
	TCHAR szBookPath[MAX_PATH];
	TCHAR szFilePath[MAX_PATH];
	TCHAR szRefFilePath[MAX_PATH] = { 0 };

	GetProgramDataPath(szBookPath, MAX_PATH);
	lstrcat(szBookPath, TEXT("\\Bookmarks\\*"));

	// enumerate all files in the bookmarks directory
	if ((hFind = FindFirstFile(szBookPath, &win32fd)) != 0)
	{
		do
		{
			if ((win32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
			{
				GetProgramDataPath(szBookPath, MAX_PATH);
				wsprintf(szFilePath, TEXT("%s\\Bookmarks\\%s"), szBookPath, win32fd.cFileName);

				HCONFIG configFile = OpenConfig(szFilePath);
				if (configFile)
				{
					if (GetConfigStr(configFile, TEXT("hexFileData\\filePath"), szRefFilePath, MAX_PATH, 0) && _tcscmp(szRefFilePath, szFilename) == 0)
					{
						HCONFIG bookmarksSection = OpenConfigSection(configFile, TEXT("hexFileData\\bookmarks"));
						DWORD idx = 0;
						HCONFIG bookmark = 0;
						DWORD countedBookmarks = 0;
						while (bookmark = EnumConfigSection(bookmarksSection, TEXT("bookmark"), idx++))
						{
							TCHAR title[100];
							BOOKMARK bm = { 0 };
							bm.pszTitle = title;
							bm.nMaxTitle = ARRAYSIZE(title);

							if (GetConfigBookmark(bookmark, &bm) && _tcscmp(title, szBookmarkName) == 0)
							{
								DeleteConfigSection(bookmark);
							}
							else
							{
								countedBookmarks++;
							}
						}

						SaveConfig(szFilePath, configFile);
						CloseConfig(configFile);
						if (countedBookmarks == 0)
						{
							DeleteFile(szFilePath);
						}
						break;
					}
					CloseConfig(configFile);
				}
			}

		} while (FindNextFile(hFind, &win32fd));

		FindClose(hFind);
	}
}