www.pudn.com > NetPaw.rar > NetpawHelper.cpp
// NetpawHelper.cpp : CNetpawHelper 的实现
#include "stdafx.h"
#include "dlgconfirm.h"
#include "atlstr.h" // CString is available
#include ".\netpawhelper.h"
#define MAPPING_NAME _T("NETPAW_{A226912B-F4CE-4bd8-A189-C91FC047B848}")
#define BHOEVENT_NAME _T("BHOEVENT_{4A041AB8-F1CC-413C-8D79-8021EA8B5D2D}")
extern HINSTANCE g_hInstDll;
// CNetpawHelper
STDMETHODIMP CNetpawHelper::SetSite(IUnknown* pUnkSite)
{
if( pUnkSite != NULL )
{
// Cache the pointer to IWebBrowser2.
HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&m_spWebBrowser);
if( SUCCEEDED(hr) )
{
// Register to sink events from DWebBrowserEvents2.
hr = DispEventAdvise(m_spWebBrowser);
if( SUCCEEDED(hr) )
{
m_bAdvised = true;
}
}
// create file mapping for sharing data with netpaw.exe
m_obFileMapping.Create(MAPPING_NAME, sizeof(URLDATA_S));
}
else
{
// Unregister event sink.
if( m_bAdvised )
{
DispEventUnadvise(m_spWebBrowser);
m_bAdvised = false;
}
// Release cached pointers and other resources here.
m_spWebBrowser.Release();
}
// Return the base class implementation
return IObjectWithSiteImpl::SetSite(pUnkSite);
}
void STDMETHODCALLTYPE CNetpawHelper::OnFileDownload( VARIANT_BOOL *ActiveDocument, VARIANT_BOOL *Cancel )
{
USES_CONVERSION;
if( !m_spWebBrowser )
{
return;
}
// get the url
BSTR bstrUrl;
HRESULT hr = m_spWebBrowser->get_LocationURL(&bstrUrl);
if( SUCCEEDED(hr) )
{
CString sUrl = OLE2T(bstrUrl);
SysFreeString(bstrUrl);
if( sUrl.IsEmpty() )
{
return;
}
// don't invoke when dealing with a composite URL
int nIndex = sUrl.ReverseFind('=');
if( nIndex != -1 )
{
return;
}
// confirming the download
CDlgConfirm dlgConfirm;
if( dlgConfirm.DoModal() != IDOK )
{
return;
}
// save the url and open netpaw.exe
URLDATA_S *pData = (URLDATA_S *)m_obFileMapping.Open();
if( pData )
{
StrCopy( pData->szDownldUrl, (LPCTSTR)sUrl, MAX_PATH );
pData->szReferer[0] = 0;
pData->szWebInfo[0] = 0;
// triger the netpaw.exe when click the link
TrigerNetPaw();
*Cancel = VARIANT_TRUE; // downloaded successfully, nothing left for Microsoft
}
m_obFileMapping.Close();
}
}
STDMETHODIMP CNetpawHelper::AddUrl(BSTR bsUrl, BSTR bsInfo, BSTR bsReferer)
{
USES_CONVERSION;
// TODO: 在此添加实现代码
URLDATA_S *pData = (URLDATA_S *)m_obFileMapping.Open();
if( pData )
{
StrCopy( pData->szDownldUrl, OLE2T(bsUrl), MAX_PATH );
StrCopy( pData->szReferer, OLE2T(bsReferer), MAX_PATH );
StrCopy( pData->szWebInfo, OLE2T(bsInfo), MAX_PATH/2 );
// now triger the netpaw.exe when click context menu
TrigerNetPaw();
}
m_obFileMapping.Close();
return S_OK;
}
void CNetpawHelper::TrigerNetPaw()
{
// if main process is already existed, notify it and return
HANDLE hBhoEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, BHOEVENT_NAME);
if( hBhoEvent )
{
SetEvent(hBhoEvent);
CloseHandle(hBhoEvent);
return;
}
// get exe file name
CString sExeFile;
::GetModuleFileName( g_hInstDll, sExeFile.GetBuffer(MAX_PATH), MAX_PATH );
sExeFile.ReleaseBuffer();
int nIndex = sExeFile.ReverseFind('\\');
if( nIndex != -1 )
{
sExeFile = sExeFile.Left(nIndex); // cut off the right ride
}
sExeFile += _T("\\NetPaw.exe");
// invoke netpaw.exe
::ShellExecute(NULL, "open", sExeFile, NULL, NULL, SW_SHOWNORMAL);
}