Index: src/chext/DropMenuExt.cpp
===================================================================
diff -u -N -r3c248d4f6d0fdb1e487cc868b2f0b219eec37ef4 -r134983cab6122e3fff73994a6f9c417aaeab3bc2
--- src/chext/DropMenuExt.cpp	(.../DropMenuExt.cpp)	(revision 3c248d4f6d0fdb1e487cc868b2f0b219eec37ef4)
+++ src/chext/DropMenuExt.cpp	(.../DropMenuExt.cpp)	(revision 134983cab6122e3fff73994a6f9c417aaeab3bc2)
@@ -51,157 +51,221 @@
 
 STDMETHODIMP CDropMenuExt::Initialize(LPCITEMIDLIST pidlFolder, IDataObject* piDataObject, HKEY /*hkeyProgID*/)
 {
-	LOG_DEBUG(m_spLog) << L"Initializing";
+	try
+	{
+		LOG_DEBUG(m_spLog) << L"Initializing";
 
-	// When called:
-	// 1. R-click on a directory
-	// 2. R-click on a directory background
-	// 3. Pressed Ctrl+C, Ctrl+X on a specified file/directory
+		// When called:
+		// 1. R-click on a directory
+		// 2. R-click on a directory background
+		// 3. Pressed Ctrl+C, Ctrl+X on a specified file/directory
 
-	if(!pidlFolder && !piDataObject)
-	{
-		LOG_ERROR(m_spLog) << L"Missing both pointers.";
-		return E_FAIL;
-	}
+		if(!pidlFolder && !piDataObject)
+		{
+			LOG_ERROR(m_spLog) << L"Missing both pointers.";
+			return E_FAIL;
+		}
 
-	if(!pidlFolder || !piDataObject)
-		LOG_WARNING(m_spLog) << L"Missing at least one parameter - it's unexpected.";
+		if(!pidlFolder || !piDataObject)
+			LOG_WARNING(m_spLog) << L"Missing at least one parameter - it's unexpected.";
 
-	if(!piDataObject)
+		if(!piDataObject)
+		{
+			LOG_ERROR(m_spLog) << L"Missing piDataObject.";
+			return E_FAIL;
+		}
+
+		// check options
+		HWND hWnd = ShellExtensionVerifier::VerifyShellExt(m_piShellExtControl);
+		if(hWnd == nullptr)
+			return E_FAIL;
+
+		HRESULT hResult = ShellExtensionVerifier::ReadShellConfig(m_piShellExtControl, m_tShellExtMenuConfig);
+		LOG_HRESULT(m_spLog, hResult) << L"Read shell config";
+		if(SUCCEEDED(hResult))
+		{
+			hResult = m_tShellExtData.GatherDataFromInitialize(pidlFolder, piDataObject);
+			LOG_HRESULT(m_spLog, hResult) << L"Gather data from initialize";
+		}
+
+		return hResult;
+	}
+	catch(const std::exception& e)
 	{
-		LOG_ERROR(m_spLog) << L"Missing piDataObject.";
+		LOG_CRITICAL(m_spLog) << L"Unexpected std exception encountered in " << __FUNCTION__ << L": " << e.what();
 		return E_FAIL;
 	}
-
-	// check options
-	HWND hWnd = ShellExtensionVerifier::VerifyShellExt(m_piShellExtControl);
-	if(hWnd == nullptr)
-		return E_FAIL;
-
-	HRESULT hResult = ShellExtensionVerifier::ReadShellConfig(m_piShellExtControl, m_tShellExtMenuConfig);
-	LOG_HRESULT(m_spLog, hResult) << L"Read shell config";
-	if(SUCCEEDED(hResult))
+	catch(...)
 	{
-		hResult = m_tShellExtData.GatherDataFromInitialize(pidlFolder, piDataObject);
-		LOG_HRESULT(m_spLog, hResult) << L"Gather data from initialize";
+		LOG_CRITICAL(m_spLog) << L"Unexpected other exception encountered in " << __FUNCTION__ << L".";
+		return E_FAIL;
 	}
-
-	return hResult;
 }
 
 STDMETHODIMP CDropMenuExt::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT /*idCmdLast*/, UINT /*uFlags*/)
 {
-	LOG_DEBUG(m_spLog) << L"Querying context menu";
+	try
+	{
+		LOG_DEBUG(m_spLog) << L"Querying context menu";
 
-	// check options
-	HWND hWnd = ShellExtensionVerifier::VerifyShellExt(m_piShellExtControl);
-	if(!hWnd)
-		return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
+		if(!hMenu)
+		{
+			LOG_ERROR(m_spLog) << L"Parameter hMenu is null";
+			return E_INVALIDARG;
+		}
 
-	// retrieve the default menu item; if not available, fallback to the default heuristics
-	m_tShellExtData.ReadDefaultSelectionStateFromMenu(hMenu);
+		// check options
+		HWND hWnd = ShellExtensionVerifier::VerifyShellExt(m_piShellExtControl);
+		if(!hWnd)
+			return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
 
-	// retrieve the action information to be performed
-	TShellExtData::EActionSource eActionSource = m_tShellExtData.GetActionSource();
+		// retrieve the default menu item; if not available, fallback to the default heuristics
+		m_tShellExtData.ReadDefaultSelectionStateFromMenu(hMenu);
 
-	// determine if we want to perform override based on user options and detected action source
-	bool bIntercept = (m_tShellExtMenuConfig.GetInterceptDragAndDrop() && eActionSource == TShellExtData::eSrc_DropMenu ||
-						m_tShellExtMenuConfig.GetInterceptKeyboardActions() && eActionSource == TShellExtData::eSrc_Keyboard ||
-						m_tShellExtMenuConfig.GetInterceptCtxMenuActions() && eActionSource == TShellExtData::eSrc_CtxMenu);
+		// retrieve the action information to be performed
+		TShellExtData::EActionSource eActionSource = m_tShellExtData.GetActionSource();
 
-	TShellMenuItemPtr spRootMenuItem = m_tShellExtMenuConfig.GetDragAndDropRoot();
-	m_tContextMenuHandler.Init(spRootMenuItem, hMenu, idCmdFirst, indexMenu, m_tShellExtData, m_tShellExtMenuConfig.GetFormatter(), 
-		m_tShellExtMenuConfig.GetShowFreeSpace(), m_tShellExtMenuConfig.GetShowShortcutIcons(), bIntercept);
+		// determine if we want to perform override based on user options and detected action source
+		bool bIntercept = (m_tShellExtMenuConfig.GetInterceptDragAndDrop() && eActionSource == TShellExtData::eSrc_DropMenu ||
+			m_tShellExtMenuConfig.GetInterceptKeyboardActions() && eActionSource == TShellExtData::eSrc_Keyboard ||
+			m_tShellExtMenuConfig.GetInterceptCtxMenuActions() && eActionSource == TShellExtData::eSrc_CtxMenu);
 
-	return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, m_tContextMenuHandler.GetLastCommandID() - idCmdFirst + 1);
+		TShellMenuItemPtr spRootMenuItem = m_tShellExtMenuConfig.GetDragAndDropRoot();
+		m_tContextMenuHandler.Init(spRootMenuItem, hMenu, idCmdFirst, indexMenu, m_tShellExtData, m_tShellExtMenuConfig.GetFormatter(),
+			m_tShellExtMenuConfig.GetShowFreeSpace(), m_tShellExtMenuConfig.GetShowShortcutIcons(), bIntercept);
+
+		return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, m_tContextMenuHandler.GetLastCommandID() - idCmdFirst + 1);
+	}
+	catch(const std::exception& e)
+	{
+		LOG_CRITICAL(m_spLog) << L"Unexpected std exception encountered in " << __FUNCTION__ << L": " << e.what();
+		return E_FAIL;
+	}
+	catch(...)
+	{
+		LOG_CRITICAL(m_spLog) << L"Unexpected other exception encountered in " << __FUNCTION__ << L".";
+		return E_FAIL;
+	}
 }
 
 STDMETHODIMP CDropMenuExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpici)
 {
-	LOG_DEBUG(m_spLog) << L"Invoking command";
+	try
+	{
+		LOG_DEBUG(m_spLog) << L"Invoking command";
 
-	HWND hWnd = ShellExtensionVerifier::VerifyShellExt(m_piShellExtControl);
-	if(hWnd == nullptr)
-		return E_FAIL;
+		if(!lpici)
+		{
+			LOG_ERROR(m_spLog) << L"Parameter lpici is null";
+			return E_INVALIDARG;
+		}
 
-	// find command to be executed, if not found - fail
-	TShellMenuItemPtr spSelectedItem = m_tContextMenuHandler.GetCommandByMenuItemOffset(LOWORD(lpici->lpVerb));
-	if(!spSelectedItem)
-		return E_FAIL;
+		HWND hWnd = ShellExtensionVerifier::VerifyShellExt(m_piShellExtControl);
+		if(hWnd == nullptr)
+			return E_FAIL;
 
-	// data retrieval and validation
-	if(!m_tShellExtData.VerifyItemCanBeExecuted(spSelectedItem))
-		return E_FAIL;
+		// find command to be executed, if not found - fail
+		TShellMenuItemPtr spSelectedItem = m_tContextMenuHandler.GetCommandByMenuItemOffset(LOWORD(lpici->lpVerb));
+		if(!spSelectedItem)
+			return E_FAIL;
 
-	chcore::TPathContainer vSourcePaths;
-	chcore::TSmartPath spDestinationPath;
-	chcore::EOperationType eOperationType = chcore::eOperation_None;
+		// data retrieval and validation
+		if(!m_tShellExtData.VerifyItemCanBeExecuted(spSelectedItem))
+			return E_FAIL;
 
-	if(!m_tShellExtData.GetSourcePathsByItem(spSelectedItem, vSourcePaths))
-		return E_FAIL;
-	if(!m_tShellExtData.GetDestinationPathByItem(spSelectedItem, spDestinationPath))
-		return E_FAIL;
-	if(!m_tShellExtData.GetOperationTypeByItem(spSelectedItem, eOperationType))
-		return E_FAIL;
+		chcore::TPathContainer vSourcePaths;
+		chcore::TSmartPath spDestinationPath;
+		chcore::EOperationType eOperationType = chcore::eOperation_None;
 
-	chcore::TTaskDefinition tTaskDefinition;
-	tTaskDefinition.SetSourcePaths(vSourcePaths);
-	tTaskDefinition.SetDestinationPath(spDestinationPath);
-	tTaskDefinition.SetOperationType(eOperationType);
+		if(!m_tShellExtData.GetSourcePathsByItem(spSelectedItem, vSourcePaths))
+			return E_FAIL;
+		if(!m_tShellExtData.GetDestinationPathByItem(spSelectedItem, spDestinationPath))
+			return E_FAIL;
+		if(!m_tShellExtData.GetOperationTypeByItem(spSelectedItem, eOperationType))
+			return E_FAIL;
 
-	// get task data as xml
-	chcore::TString wstrData;
-	tTaskDefinition.StoreInString(wstrData);
+		chcore::TTaskDefinition tTaskDefinition;
+		tTaskDefinition.SetSourcePaths(vSourcePaths);
+		tTaskDefinition.SetDestinationPath(spDestinationPath);
+		tTaskDefinition.SetOperationType(eOperationType);
 
-	// fill struct
-	COPYDATASTRUCT cds;
-	cds.dwData = spSelectedItem->IsSpecialOperation() ? eCDType_TaskDefinitionContentSpecial : eCDType_TaskDefinitionContent;
-	cds.lpData = (void*)wstrData.c_str();
-	cds.cbData = (DWORD)((wstrData.GetLength() + 1) * sizeof(wchar_t));
+		// get task data as xml
+		chcore::TString wstrData;
+		tTaskDefinition.StoreInString(wstrData);
 
-	// send a message
-	::SendMessage(hWnd, WM_COPYDATA, reinterpret_cast<WPARAM>(lpici->hwnd), reinterpret_cast<LPARAM>(&cds));
+		// fill struct
+		COPYDATASTRUCT cds;
+		cds.dwData = spSelectedItem->IsSpecialOperation() ? eCDType_TaskDefinitionContentSpecial : eCDType_TaskDefinitionContent;
+		cds.lpData = (void*)wstrData.c_str();
+		cds.cbData = (DWORD)((wstrData.GetLength() + 1) * sizeof(wchar_t));
 
-	return S_OK;
+		// send a message
+		::SendMessage(hWnd, WM_COPYDATA, reinterpret_cast<WPARAM>(lpici->hwnd), reinterpret_cast<LPARAM>(&cds));
+
+		return S_OK;
+	}
+	catch(const std::exception& e)
+	{
+		LOG_CRITICAL(m_spLog) << L"Unexpected std exception encountered in " << __FUNCTION__ << L": " << e.what();
+		return E_FAIL;
+	}
+	catch(...)
+	{
+		LOG_CRITICAL(m_spLog) << L"Unexpected other exception encountered in " << __FUNCTION__ << L".";
+		return E_FAIL;
+	}
 }
 
 STDMETHODIMP CDropMenuExt::GetCommandString(UINT_PTR idCmd, UINT uFlags, UINT* /*pwReserved*/, LPSTR pszName, UINT cchMax)
 {
-	LOG_DEBUG(m_spLog) << L"Retrieving command string for cmd: " << idCmd;
+	try
+	{
+		LOG_DEBUG(m_spLog) << L"Retrieving command string for cmd: " << idCmd;
 
-	memset(pszName, 0, cchMax);
+		memset(pszName, 0, cchMax);
 
-	if(uFlags != GCS_HELPTEXTW && uFlags != GCS_HELPTEXTA)
-		return S_OK;
+		if(uFlags != GCS_HELPTEXTW && uFlags != GCS_HELPTEXTA)
+			return S_OK;
 
-	// check options
-	HRESULT hResult = ShellExtensionVerifier::IsShellExtEnabled(m_piShellExtControl);
-	if(FAILED(hResult))
-		return hResult;
-	else if(hResult == S_FALSE)
-		return S_OK;
+		// check options
+		HRESULT hResult = ShellExtensionVerifier::IsShellExtEnabled(m_piShellExtControl);
+		if(FAILED(hResult))
+			return hResult;
+		else if(hResult == S_FALSE)
+			return S_OK;
 
-	TShellMenuItemPtr spSelectedItem = m_tContextMenuHandler.GetCommandByMenuItemOffset(LOWORD(idCmd));
-	if(!spSelectedItem || !spSelectedItem->SpecifiesDestinationPath())
-		return E_FAIL;
+		TShellMenuItemPtr spSelectedItem = m_tContextMenuHandler.GetCommandByMenuItemOffset(LOWORD(idCmd));
+		if(!spSelectedItem || !spSelectedItem->SpecifiesDestinationPath())
+			return E_FAIL;
 
-	switch(uFlags)
-	{
-	case GCS_HELPTEXTW:
+		switch(uFlags)
 		{
+		case GCS_HELPTEXTW:
+		{
 			wcsncpy(reinterpret_cast<wchar_t*>(pszName), spSelectedItem->GetItemTip().c_str(), spSelectedItem->GetItemTip().GetLength() + 1);
 			break;
 		}
-	case GCS_HELPTEXTA:
+		case GCS_HELPTEXTA:
 		{
 			USES_CONVERSION;
 			CT2A ct2a(spSelectedItem->GetItemTip().c_str());
 			strncpy(reinterpret_cast<char*>(pszName), ct2a, strlen(ct2a) + 1);
 			break;
 		}
-	}
+		}
 
-	return S_OK;
+		return S_OK;
+	}
+	catch(const std::exception& e)
+	{
+		LOG_CRITICAL(m_spLog) << L"Unexpected std exception encountered in " << __FUNCTION__ << L": " << e.what();
+		return E_FAIL;
+	}
+	catch(...)
+	{
+		LOG_CRITICAL(m_spLog) << L"Unexpected other exception encountered in " << __FUNCTION__ << L".";
+		return E_FAIL;
+	}
 }
 
 STDMETHODIMP CDropMenuExt::HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
@@ -213,6 +277,5 @@
 STDMETHODIMP CDropMenuExt::HandleMenuMsg2(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT* /*plResult*/)
 {
 	uMsg; wParam; lParam;
-	ATLTRACE(_T("CDropMenuExt::HandleMenuMsg2(): uMsg = %lu, wParam = %lu, lParam = %lu\n"), uMsg, wParam, lParam);
 	return S_FALSE;
 }