// MBShellExt.cpp : Implementation of CMBShellExt #include "stdafx.h" #include "MBShellExt.h" #pragma comment(lib,"shlwapi.lib") #pragma message("Automatically linking with Shlwapi.dll") // CMBShellExt #define REG_SUBKEY _T( "Software\\Activesoft\\MBClient" ) #define PSW_EMKEY ( _T("P512D790") ) // ÊÇAPM\EMÉϵÄÃÜÔ¿ // CMBShellExt #define AMCADDIN_FLAG_SHELLEX_MENU ( 16384 ) // ÔÊÐí¼ÓÈë×ÊÔ´¹ÜÀíÆ÷ÓÒ¼ü²Ëµ¥ÖÐ HRESULT CMBShellExt::Initialize( LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hProgID ) { FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; STGMEDIUM stg = { TYMED_HGLOBAL }; HDROP hDrop; // Look for CF_HDROP data in the data object. if ( FAILED( pDataObj->GetData( &fmt, &stg ) ) ) { // Nope! Return an "invalid argument" error back to Explorer. return E_INVALIDARG; } // Get a pointer to the actual data. hDrop = (HDROP)GlobalLock( stg.hGlobal ); if ( NULL == hDrop ) return E_INVALIDARG; // Note that it's vitally important to error-check everything, especially pointers. Since our extension runs in Explorer's process space, if we crash we take down Explorer too. On 9x, such a crash might necessitate rebooting the computer. // So, now that we have an HDROP handle, we can get the filename we need. // Sanity check ¨C make sure there is at least one filename. UINT uNumFiles = DragQueryFile( hDrop, 0xFFFFFFFF, NULL, 0 ); if ( 0 == uNumFiles ) { GlobalUnlock( stg.hGlobal ); ReleaseStgMedium( &stg ); return E_INVALIDARG; } HRESULT hr = S_OK; TCHAR szFile[MAX_PATH]; UINT nLen; // get multi files for ( UINT uFile = 0; uFile < uNumFiles; uFile++ ) { // Get the next filename. nLen = DragQueryFile( hDrop, uFile, szFile, MAX_PATH ); if ( nLen == 0 ) continue; if ( PathIsDirectory ( szFile ) ) { szFile[nLen] = '\\'; szFile[nLen+1] = '\0'; } m_lsFiles.AddTail( szFile ); } // end for GlobalUnlock( stg.hGlobal ); ReleaseStgMedium( &stg ); return hr; } HRESULT CMBShellExt::QueryContextMenu( HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags ) { // If the flags include CMF_DEFAULTONLY then we shouldn't do anything. if ( uFlags & CMF_DEFAULTONLY ) return MAKE_HRESULT( SEVERITY_SUCCESS, FACILITY_NULL, 0 ); int nNum = 0; POSITION pos; CString strDispName; CString strMenuName; strMenuName = _T("MBCÉÏ´«");//µ½Îĵµ¹ñ InsertMenu( hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd + nNum, strMenuName ); ++nNum; HBITMAP hBitmap= GetSTKBitmap(1); if(hBitmap) { SetMenuItemBitmaps(hmenu, uMenuIndex, MF_BYPOSITION, hBitmap, hBitmap); } /*strMenuName = _T("MBCÉÏ´«µ½¸öÈËÍøÅÌ"); InsertMenu( hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd + nNum, strMenuName ); ++nNum; hBitmap= GetSTKBitmap(1); if(hBitmap) { SetMenuItemBitmaps(hmenu, uMenuIndex, MF_BYPOSITION, hBitmap, hBitmap); }*/ for ( pos = m_lsAddInDispName.GetHeadPosition(); pos; ) { strDispName = m_lsAddInDispName.GetNext( pos ); InsertMenu( hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd + nNum, strDispName ); ++nNum; } strMenuName = _T("MBCÒ×·¢"); InsertMenu( hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd + nNum, strMenuName ); hBitmap= GetSTKBitmap(); if(hBitmap) { SetMenuItemBitmaps(hmenu, uMenuIndex, MF_BYPOSITION, hBitmap, hBitmap); } return MAKE_HRESULT( SEVERITY_SUCCESS, FACILITY_NULL, nNum + 1 ); } #ifdef _WIN64 HRESULT CMBShellExt::GetCommandString(UINT_PTR idCmd, UINT uFlags, UINT* pwReserved, LPSTR pszName, UINT cchMax) { USES_CONVERSION; // Check idCmd, it must be 0 since we have only one menu item. if ( idCmd < 0 && idCmd > (UINT)m_lsAddInDispName.GetCount() ) return E_INVALIDARG; // If Explorer is asking for a help string, copy our string into the // supplied buffer. if ( uFlags & GCS_HELPTEXT ) { CString strText; LPCTSTR szText = _T(""); if(idCmd == 0) { szText = _T("MBCÒ×·¢"); } else { strText.Format( _T("This is the MBC shell extension's help %d"), idCmd); szText = strText; } if ( uFlags & GCS_UNICODE ) { // We need to cast pszName to a Unicode string, and then use the // Unicode string copy API. lstrcpynW( (LPWSTR) pszName, T2CW(szText), cchMax ); } else { // Use the ANSI string copy API to return the help string. lstrcpynA( pszName, T2CA(szText), cchMax ); } return S_OK; } return E_INVALIDARG; } #else HRESULT CMBShellExt::GetCommandString( UINT idCmd, UINT uFlags, UINT* pwReserved, LPSTR pszName, UINT cchMax ) { USES_CONVERSION; // Check idCmd, it must be 0 since we have only one menu item. if ( idCmd < 0 && idCmd > (UINT)m_lsAddInDispName.GetCount() ) return E_INVALIDARG; // If Explorer is asking for a help string, copy our string into the // supplied buffer. if ( uFlags & GCS_HELPTEXT ) { LPCTSTR szText = _T("This is the MBC shell extension's help"); if ( uFlags & GCS_UNICODE ) { // We need to cast pszName to a Unicode string, and then use the // Unicode string copy API. lstrcpynW( (LPWSTR) pszName, T2CW(szText), cchMax ); } else { // Use the ANSI string copy API to return the help string. lstrcpynA( pszName, T2CA(szText), cchMax ); } return S_OK; } return E_INVALIDARG; } #endif HRESULT CMBShellExt::InvokeCommand( LPCMINVOKECOMMANDINFO pCmdInfo ) { // If lpVerb really points to a string, ignore this function call and bail out. int nMenuID = 0; // Get the command index - the only valid one is 0. nMenuID = LOWORD( pCmdInfo->lpVerb ); if ( 0 != HIWORD( pCmdInfo->lpVerb ) ) { return E_INVALIDARG; } if ( nMenuID >= 0 && nMenuID <= m_lsAddInDispName.GetCount( ) + 2 ) { #ifdef _WIN64 CString strAMViewPath; if ( DoSTKCom( nMenuID, pCmdInfo->hwnd ) ) return S_OK; else return S_FALSE; #else if ( DoSTKCom( nMenuID, pCmdInfo->hwnd ) ) return S_OK; else return S_FALSE; #endif return S_OK; } return E_INVALIDARG; } bool CMBShellExt::DoSTKCom( long nMenuID, HWND hWnd ) { HRESULT hr; CString strMBExe; strMBExe.Format( _T("%s\\mbcU.exe"), GetSTKWorkDir()); HANDLE hEventShareMemory = OpenEvent(EVENT_ALL_ACCESS , FALSE, _T("mbcSharedMemoryEvent")); if (NULL == hEventShareMemory) { long nErrorId = GetLastError(); CString strMessage; strMessage.Format(_T("OpenEvent mbcSharedMemoryEvent ErrorID:%d"),nErrorId); WriteLog(strMessage); ShellExecute( NULL, _T("open"), strMBExe, _T(""), _T(""), SW_SHOW ); //MessageBox(hWnd, strMessage, _T("mbEasySend"), MB_OK | MB_ICONINFORMATION ); return false; } HANDLE hShareMemory = OpenFileMapping( PAGE_READONLY,false,_T("mbcSharedMemory") ); if (NULL == hShareMemory) { /*m_hShareMemory = CreateFileMapping(HANDLE(0xFFFFFFFF), NULL, PAGE_READWRITE,0, 2560, _T("mbcSharedMemory")); if (NULL == m_hShareMemory) {*/ long nErrorId = GetLastError(); CString strMessage; strMessage.Format(_T("OpenFileMapping mbcSharedMemory ErrorID:%d"),nErrorId); //MessageBox(hWnd, strMessage, _T("mbEasySend"), MB_OK | MB_ICONINFORMATION ); ShellExecute( NULL, _T("open"), strMBExe, _T(""), _T(""), SW_SHOW ); WriteLog(strMessage); return false; //} } TCHAR* lpBuffer = (TCHAR*)MapViewOfFile(hShareMemory, FILE_MAP_WRITE, 0, 0, 2560); if (NULL == lpBuffer) { //MessageBox(hWnd, _T("MapViewOfFile mbcSharedMemoryʧ°Ü"), _T("mbEasySend"), MB_OK | MB_ICONINFORMATION ); long nErrorId = GetLastError(); CString strMessage; strMessage.Format(_T("MapViewOfFile mbcSharedMemory ErrorID:%d"),nErrorId); WriteLog(strMessage); return false; } POSITION pos; CString strBuf; CString strXmlBuf; CString strAddInName = _T(""); int nType = 0; if(nMenuID == 0 )//|| nMenuID == 1 nType = 1; if ( nMenuID -1 > m_lsAddInName.GetCount() ) { pos = m_lsAddInName.FindIndex( nMenuID - 1 ); if ( pos ) { strAddInName = m_lsAddInName.GetAt( pos ); } } strXmlBuf.Format(_T("
"),nType,strAddInName); CString strFile; for ( pos = m_lsFiles.GetHeadPosition(); pos; ) { strFile = m_lsFiles.GetNext( pos ); strBuf.Format( _T("