// MBAddIn.cpp : Implementation of CMBAddIn #include "stdafx.h" #include "MBAddIn.h" #include "BaseFun.h" // CMBAddIn _ATL_FUNC_INFO OnClickButtonInfo ={CC_STDCALL,VT_EMPTY,2,{VT_DISPATCH,VT_BYREF | VT_BOOL}}; void _stdcall CMBAddIn::OnClickButton(IDispatch *Ctrl, VARIANT_BOOL *CancelDefault) { USES_CONVERSION; HRESULT hr; TCHAR szProjectName[MAX_PATH]; memset( szProjectName, 0, MAX_PATH ); wsprintf ( szProjectName, _T("%s"),IDS_PROJNAME ); CComQIPtr pCommandBarButton(Ctrl); CComBSTR bszFileName; //the button that raised the event. Do something with this... if ( m_spWordApp != NULL ) { Word::_DocumentPtr spDoc; hr = m_spWordApp->get_ActiveDocument(&spDoc); if(spDoc != NULL) { spDoc->Save(); hr = spDoc->get_FullName(&bszFileName); SendMbcFile( (LPCTSTR)(_bstr_t)bszFileName); } } else if ( m_spExcelApp != NULL ) { Excel::_WorkbookPtr spWorkbook; hr = m_spExcelApp->get_ActiveWorkbook(&spWorkbook); if(spWorkbook != NULL) { spWorkbook->Save(); spWorkbook->get_FullName(0, &bszFileName ); SendMbcFile( (LPCTSTR)(_bstr_t)bszFileName); } } else if ( m_spPowerPointApp != NULL ) { MSPPT::_PresentationPtr spPresentation; hr = m_spPowerPointApp->get_ActivePresentation(&spPresentation); if(spPresentation != NULL) { spPresentation->Save(); hr = spPresentation->get_FullName(&bszFileName); SendMbcFile( (LPCTSTR)(_bstr_t)bszFileName); } } } STDMETHODIMP CMBAddIn::OnConnection(IDispatch* Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom) { // AFX_MANAGE_STATE(AfxGetStaticModuleState()) CComQIPtr spWordApp(Application); CComQIPtr spExcelApp(Application); CComQIPtr spPowerPointApp(Application); m_spWordApp = spWordApp; m_spExcelApp = spExcelApp; m_spPowerPointApp = spPowerPointApp; CComPtr spCmdBars; HRESULT hr; if ( spWordApp != NULL ) hr = spWordApp->get_CommandBars(&spCmdBars); else if ( spExcelApp != NULL ) hr = spExcelApp->get_CommandBars(&spCmdBars); else if ( spPowerPointApp != NULL ) hr = spPowerPointApp->get_CommandBars(&spCmdBars ); else return E_FAIL; // ATLASSERT(spCmdBars); HBITMAP hBmp; hBmp = ::LoadBitmap( ::GetModuleHandle( _T("mbInside.dll") ), MAKEINTRESOURCE(IDB_MBC)); ::OpenClipboard(NULL); ::SetClipboardData(CF_BITMAP, (HANDLE)hBmp); ::CloseClipboard(); ::DeleteObject( hBmp ); CComVariant vtName(_T("MBC")); CComVariant vtPos(1); // position it below all toolbands CComVariant vtTemp(VARIANT_TRUE); // is temporary CComVariant vtEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR); CComPtr spNewCmdBar; VARIANT_BOOL bVisible = VARIANT_TRUE; hr = spCmdBars->get_Item( vtName, &spNewCmdBar ); if ( spNewCmdBar != NULL ) { spNewCmdBar->get_Visible( &bVisible ); } else { hr = spCmdBars->Add( vtName, vtPos, vtEmpty, vtTemp, &spNewCmdBar); } if(FAILED(hr)) return hr; ATLASSERT(spNewCmdBar); CComPtr spBarControls; hr = spNewCmdBar->get_Controls( &spBarControls ); if(FAILED(hr)) return hr; ATLASSERT(spBarControls); CComVariant vtToolBarType(1); CComPtr spNewBar; CComVariant vtShow(VARIANT_TRUE); // add button CComBSTR bszEasySend; CString strEasySend; strEasySend.Format ( _T("%s"),IDS_EASY_SEND ); bszEasySend = strEasySend.AllocSysString(); //CSTKInsideFun::WriteLog(_T("OnConnection 5.2")); hr = spBarControls->get_Item(CComVariant(bszEasySend), &spNewBar ); //CSTKInsideFun::WriteLog(_T("OnConnection 5.3")); if ( spNewBar == NULL ) { //CSTKInsideFun::WriteLog(_T("OnConnection 5.4")); hr = spBarControls->Add( vtToolBarType, vtEmpty, CComVariant(bszEasySend), vtEmpty, vtShow, &spNewBar); } if(FAILED(hr)) return hr; //CSTKInsideFun::WriteLog(_T("OnConnection 5.5")); ATLASSERT(spNewBar); // get CommandBarButton interface so we can specify button styles and stuff // each button displays a bitmap and caption next to it CComQIPtr spCmdButton(spNewBar); ATLASSERT(spCmdButton); spCmdButton->put_Style(Office::msoButtonIconAndCaption); spCmdButton->PasteFace(); // use own bitmap spCmdButton->put_Caption((BSTR)bszEasySend); spCmdButton->put_Enabled(VARIANT_TRUE); spCmdButton->put_TooltipText((BSTR)bszEasySend); spCmdButton->put_Tag((BSTR)bszEasySend); spCmdButton->put_Visible(VARIANT_TRUE); spNewCmdBar->put_Visible(VARIANT_TRUE); m_spButton = spCmdButton; bszEasySend.Empty(); hr = CommandButtonEvents::DispEventAdvise((IDispatch*)m_spButton); if(FAILED(hr)) return hr; return S_OK; } STDMETHODIMP CMBAddIn::OnDisconnection(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom) { if ( m_spButton != NULL ) CommandButtonEvents::DispEventUnadvise((IDispatch*)m_spButton); return S_OK; } void CMBAddIn::SendMbcFile(CString strFile) { HRESULT hr; CString strMBExe; strMBExe.Format( _T("%s\\mbcU.exe"), CBaseFun::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); ShellExecute( NULL, _T("open"), strMBExe, _T(""), _T(""), SW_SHOW ); //MessageBox(hWnd, strMessage, _T("mbEasySend"), MB_OK | MB_ICONINFORMATION ); return; } HANDLE hShareMemory = OpenFileMapping( PAGE_READONLY,false,_T("mbcSharedMemory") ); if (NULL == 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 ); return; } 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); return; } CString strXmlBuf; strXmlBuf.Format(_T("%s"),strFile); _stprintf(lpBuffer,_T("%s"), strXmlBuf ); UnmapViewOfFile(lpBuffer); SetEvent(hEventShareMemory); CloseHandle(hEventShareMemory); CloseHandle(hShareMemory); return; }