王海龙 2009年08月03日 星期一 16:21 | 2657次浏览 | 0条评论
环境:VS
采用连接
另
目前正在做一个客户程序,写了一个事件接收器对WORD的事件进行接收处理.
当前想采用连接点的方式,客户程序部分代码如下:
_Application app;
if (!app.CreateDispatch("Word.Application", NULL))
{
AfxMessageBox("Cannot start Word");
return;
}
Documents docs = app.GetDocuments();
COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Document doc = docs.Add(vOpt, vOpt, vOpt, vOpt);
app.SetVisible(TRUE);
// Declare the events you want to catch.
static const GUID IID_IDocAppEvents =
{0x000209F7, 0x0000, 0x0000,
{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
// Get the object's IConnectionPointContainer
// interface to set up the advisory connection
HRESULT hr;
IConnectionPointContainer *pCPCtr = NULL;
hr = app.m_lpDispatch->QueryInterface(
IID_IConnectionPointContainer,
(void **)&pCPCtr);
// Find connection point for events you're interested in.
hr = pCPCtr->FindConnectionPoint(IID_IDocAppEvents,
&m_pCPEvents);
// Setup advisory connection!
hr = m_pCPEvents->Advise(&m_sink, &m_dwEventsCookie);
if (FAILED(hr))
{
m_pCPEvents = NULL;
}
事件接收器类如下,是自己写的一个简单的小例子:
// IDispatch implementation to catch WORD Application's events.
class CEventSink : public IDispatch
{
public:
ULONG refCount;
CEventSink::CEventSink() {
refCount = 1;
}
CEventSink::~CEventSink() {
}
// IUnknown methods.
virtual HRESULT __stdcall QueryInterface(REFIID riid, void **ppvObject)
{
if(IsEqualGUID(riid, IID_IDispatch) || IsEqualGUID(riid, IID_IUnknown))
{
this->AddRef();
*ppvObject = this;
return S_OK;
}
*ppvObject = NULL;
return E_NOINTERFACE;
}
virtual ULONG _stdcall AddRef(void)
{
return ++refCount;
}
virtual ULONG _stdcall Release(void)
{
if(--refCount <= 0)
{
//Delete this;
return 0;
}
return refCount;
}
// IDispatch methods.
virtual HRESULT _stdcall GetTypeInfoCount(UINT *pctinfo)
{
if(pctinfo)
{
*pctinfo = 0;
}
return E_NOTIMPL;
}
virtual HRESULT _stdcall GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
return E_NOTIMPL;
}
virtual HRESULT _stdcall GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames,
UINT cNames, LCID lcid, DISPID *rgDispId)
{
return E_NOTIMPL;
}
virtual HRESULT _stdcall Invoke(
DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags,
DISPPARAMS *pDispParams, VARIANT *pVarResult,
EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
switch(dispIdMember)
{
case 0x1a83: // Worksheet Change Event
{
break;
}
}
return S_OK;
}
这个接收器用来接收Excel的事件就没有问题,但是当我用来接收WORD的事件时,
在客户程序中,FindConnectionPoint成功,但Advise失败?
不知为何?
问题的关键是这个接收器能正确接收Excel的事件,用在WORD上就出错了.
极可能是因为上面对WORD的连接点操作哪个地方不对?
我电脑上查到如下四个WORD的Application对应的连接点:
ApplicationEvents //对应GUID为 000209F7-0000-0000-C000-000000000046
ApplicationEvents2 //对应GUID为 000209FE-0000-0000-C000-000000000046
ApplicationEvents3 //对应GUID为 00020A00-0000-0000-C000-000000000046
ApplicationEvents4 //对应GUID为 00020A01-0000-0000-C000-000000000046
都全部试了一遍,结果都会出现上述问题.
而查到的Excel的事件连接点只有一个,不知是否跟这个有关系?
Zeuux © 2024
京ICP备05028076号
暂时没有评论