Index: src/chext/DropMenuExt.cpp =================================================================== diff -u -rebc7fabbd2d59f9a0f723ea480b5374cc393ec12 -r2f696d06139af4d0fab14dd1613507b66f5169cb --- src/chext/DropMenuExt.cpp (.../DropMenuExt.cpp) (revision ebc7fabbd2d59f9a0f723ea480b5374cc393ec12) +++ src/chext/DropMenuExt.cpp (.../DropMenuExt.cpp) (revision 2f696d06139af4d0fab14dd1613507b66f5169cb) @@ -79,7 +79,7 @@ if(hWnd == nullptr) return E_FAIL; - HRESULT hResult = ReadShellConfig(); + HRESULT hResult = ShellExtensionVerifier::ReadShellConfig(m_piShellExtControl, m_tShellExtMenuConfig, eLocation_DragAndDropMenu); LOG_HRESULT(m_spLog, hResult) << L"Read shell config"; if(SUCCEEDED(hResult)) { @@ -216,40 +216,3 @@ ATLTRACE(_T("CDropMenuExt::HandleMenuMsg2(): uMsg = %lu, wParam = %lu, lParam = %lu\n"), uMsg, wParam, lParam); return S_FALSE; } - -HRESULT CDropMenuExt::ReadShellConfig() -{ - try - { - HWND hWnd = ShellExtensionVerifier::VerifyShellExt(m_piShellExtControl); - if(hWnd == nullptr) - return E_FAIL; - - // get cfg from ch - unsigned long ulSHMID = GetTickCount(); - if(::SendMessage(hWnd, WM_GETCONFIG, eLocation_DragAndDropMenu, ulSHMID) != TRUE) - { - LOG_ERROR(m_spLog) << L"Failed to retrieve configuration from Copy Handler"; - return E_FAIL; - } - - std::wstring strSHMName = IPCSupport::GenerateSHMName(ulSHMID); - - chcore::TSharedMemory tSharedMemory; - chcore::TString wstrData; - chcore::TConfig cfgShellExtData; - - tSharedMemory.Open(strSHMName.c_str()); - tSharedMemory.Read(wstrData); - - cfgShellExtData.ReadFromString(wstrData); - - m_tShellExtMenuConfig.ReadFromConfig(cfgShellExtData, _T("ShellExtCfg")); - - return S_OK; - } - catch(...) - { - return E_FAIL; - } -} Index: src/chext/DropMenuExt.h =================================================================== diff -u -rebc7fabbd2d59f9a0f723ea480b5374cc393ec12 -r2f696d06139af4d0fab14dd1613507b66f5169cb --- src/chext/DropMenuExt.h (.../DropMenuExt.h) (revision ebc7fabbd2d59f9a0f723ea480b5374cc393ec12) +++ src/chext/DropMenuExt.h (.../DropMenuExt.h) (revision 2f696d06139af4d0fab14dd1613507b66f5169cb) @@ -57,9 +57,6 @@ STDMETHOD(HandleMenuMsg2)(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT* plResult); protected: - HRESULT ReadShellConfig(); - -protected: IShellExtControl* m_piShellExtControl; TShellExtData m_tShellExtData; Index: src/chext/MenuExt.cpp =================================================================== diff -u -rebc7fabbd2d59f9a0f723ea480b5374cc393ec12 -r2f696d06139af4d0fab14dd1613507b66f5169cb --- src/chext/MenuExt.cpp (.../MenuExt.cpp) (revision ebc7fabbd2d59f9a0f723ea480b5374cc393ec12) +++ src/chext/MenuExt.cpp (.../MenuExt.cpp) (revision 2f696d06139af4d0fab14dd1613507b66f5169cb) @@ -81,7 +81,7 @@ if(!hWnd) return S_OK; - HRESULT hResult = ReadShellConfig(); + HRESULT hResult = ShellExtensionVerifier::ReadShellConfig(m_piShellExtControl, m_tShellExtMenuConfig, eLocation_ContextMenu); LOG_HRESULT(m_spLog, hResult) << L"Read shell config"; if(SUCCEEDED(hResult)) @@ -368,40 +368,3 @@ return S_OK; } - -HRESULT CMenuExt::ReadShellConfig() -{ - try - { - HWND hWnd = ShellExtensionVerifier::VerifyShellExt(m_piShellExtControl); - if(hWnd == nullptr) - return E_FAIL; - - // get cfg from ch - unsigned long ulSHMID = GetTickCount(); - if(::SendMessage(hWnd, WM_GETCONFIG, eLocation_ContextMenu, ulSHMID) != TRUE) - { - LOG_ERROR(m_spLog) << L"Failed to retrieve configuration from Copy Handler"; - return E_FAIL; - } - - std::wstring strSHMName = IPCSupport::GenerateSHMName(ulSHMID); - - chcore::TSharedMemory tSharedMemory; - chcore::TString wstrData; - chcore::TConfig cfgShellExtData; - - tSharedMemory.Open(strSHMName.c_str()); - tSharedMemory.Read(wstrData); - - cfgShellExtData.ReadFromString(wstrData); - - m_tShellExtMenuConfig.ReadFromConfig(cfgShellExtData, _T("ShellExtCfg")); - - return S_OK; - } - catch(...) - { - return E_FAIL; - } -} Index: src/chext/MenuExt.h =================================================================== diff -u -rebc7fabbd2d59f9a0f723ea480b5374cc393ec12 -r2f696d06139af4d0fab14dd1613507b66f5169cb --- src/chext/MenuExt.h (.../MenuExt.h) (revision ebc7fabbd2d59f9a0f723ea480b5374cc393ec12) +++ src/chext/MenuExt.h (.../MenuExt.h) (revision 2f696d06139af4d0fab14dd1613507b66f5169cb) @@ -63,8 +63,6 @@ protected: HRESULT DrawMenuItem(LPDRAWITEMSTRUCT lpdis); - HRESULT ReadShellConfig(); - private: TShellExtData m_tShellExtData; Index: src/chext/ShellExtensionVerifier.cpp =================================================================== diff -u -rebc7fabbd2d59f9a0f723ea480b5374cc393ec12 -r2f696d06139af4d0fab14dd1613507b66f5169cb --- src/chext/ShellExtensionVerifier.cpp (.../ShellExtensionVerifier.cpp) (revision ebc7fabbd2d59f9a0f723ea480b5374cc393ec12) +++ src/chext/ShellExtensionVerifier.cpp (.../ShellExtensionVerifier.cpp) (revision 2f696d06139af4d0fab14dd1613507b66f5169cb) @@ -19,23 +19,28 @@ #include "stdafx.h" #include "ShellExtensionVerifier.h" #include "Logger.h" +#include "../libchcore/TSharedMemory.h" +#include "../libchcore/TConfig.h" +#include "../liblogger/TLogger.h" +#include "../common/TShellExtMenuConfig.h" +#include HWND ShellExtensionVerifier::VerifyShellExt(IShellExtControl* piShellExtControl) { - logger::TLoggerPtr rLogger = GetLogger(L"ShellExtVerifier"); + logger::TLoggerPtr spLogger = GetLogger(L"ShellExtVerifier"); HRESULT hResult = IsShellExtEnabled(piShellExtControl); if(FAILED(hResult) || hResult == S_FALSE) { - LOG_DEBUG(rLogger) << L"Shell extension is disabled."; + LOG_DEBUG(spLogger) << L"Shell extension is disabled."; return nullptr; } // find CH's window HWND hWnd = ::FindWindow(_T("Copy Handler Wnd Class"), _T("Copy handler")); if(!hWnd) { - LOG_DEBUG(rLogger) << L"Cannot find Copy Handler's window."; + LOG_DEBUG(spLogger) << L"Cannot find Copy Handler's window."; return nullptr; } @@ -57,3 +62,51 @@ return S_FALSE; } + +HRESULT ShellExtensionVerifier::ReadShellConfig(IShellExtControl* piShellExtControl, TShellExtMenuConfig& tShellExtConfig, ELocation eLocation) +{ + logger::TLoggerPtr spLogger = GetLogger(L"ShellExtVerifier"); + try + { + HWND hWnd = ShellExtensionVerifier::VerifyShellExt(piShellExtControl); + if(hWnd == nullptr) + return E_FAIL; + + // generate a random number for naming shared memory + unsigned int uiSHMID = 0; + if(rand_s(&uiSHMID) != 0 || uiSHMID == 0) + { + LOG_WARNING(spLogger) << L"Failed to generate random number for shared memory naming. Falling back to tick count."; + uiSHMID = GetTickCount(); + } + + LOG_DEBUG(spLogger) << L"Requesting CH configuration. Shared memory identifier " << uiSHMID; + + if(::SendMessage(hWnd, WM_GETCONFIG, eLocation, uiSHMID) != TRUE) + { + LOG_ERROR(spLogger) << L"Failed to retrieve configuration from Copy Handler"; + return E_FAIL; + } + + std::wstring strSHMName = IPCSupport::GenerateSHMName(uiSHMID); + + chcore::TSharedMemory tSharedMemory; + chcore::TString wstrData; + chcore::TConfig cfgShellExtData; + + tSharedMemory.Open(strSHMName.c_str()); + tSharedMemory.Read(wstrData); + + LOG_TRACE(spLogger) << L"Retrieved shell ext config: " << wstrData; + + cfgShellExtData.ReadFromString(wstrData); + + tShellExtConfig.ReadFromConfig(cfgShellExtData, _T("ShellExtCfg")); + + return S_OK; + } + catch(...) + { + return E_FAIL; + } +} Index: src/chext/ShellExtensionVerifier.h =================================================================== diff -u -rb79aca0d66b1084f230022efe39cc89307482e6d -r2f696d06139af4d0fab14dd1613507b66f5169cb --- src/chext/ShellExtensionVerifier.h (.../ShellExtensionVerifier.h) (revision b79aca0d66b1084f230022efe39cc89307482e6d) +++ src/chext/ShellExtensionVerifier.h (.../ShellExtensionVerifier.h) (revision 2f696d06139af4d0fab14dd1613507b66f5169cb) @@ -20,12 +20,16 @@ #define __SHELLEXTENSIONVERIFIER_H__ #include "chext.h" +#include "../common/ipcstructs.h" +class TShellExtMenuConfig; + class ShellExtensionVerifier { public: static HWND VerifyShellExt(IShellExtControl* piShellExtControl); static HRESULT IsShellExtEnabled(IShellExtControl* piShellExtControl); + static HRESULT ReadShellConfig(IShellExtControl* piShellExtControl, TShellExtMenuConfig& tShellExtConfig, ELocation eLocation); }; #endif Index: src/chext/StdAfx.h =================================================================== diff -u -ra27d1acf1bda3c25b6dcce0d0eb0278009ce63ae -r2f696d06139af4d0fab14dd1613507b66f5169cb --- src/chext/StdAfx.h (.../StdAfx.h) (revision a27d1acf1bda3c25b6dcce0d0eb0278009ce63ae) +++ src/chext/StdAfx.h (.../StdAfx.h) (revision 2f696d06139af4d0fab14dd1613507b66f5169cb) @@ -20,6 +20,7 @@ #define __STDAFX_H__ #define STRICT +#define _CRT_RAND_S #include "../common/targetver.h"