+ -
当前位置:首页 → 问答吧 → 请教:继承CCmdTarget接收自动化带有参数的event遇到问题

请教:继承CCmdTarget接收自动化带有参数的event遇到问题

时间:2011-12-05

来源:互联网

类CMySink继承自CCmdTarget来处理Word自动化的Event。使用中遇到一个问题:自动化打开word文档然后关闭word,可以进入到OnDocumentChange 和OnQuit方法,但是没有进入OnNewDocument方法。
单步调试发现:实际上触发了"NewDocument",但是在COleDispatchImpl类的Invoke方法中执行到下面方法时返回失败。
C/C++ code

// most of the time, named arguments are not supported
    if (pDispParams->cNamedArgs != 0)
    {
        // only special PROPERTYPUT named argument is allowed
        if (pDispParams->cNamedArgs != 1 ||
            pDispParams->rgdispidNamedArgs[0] != DISPID_PROPERTYPUT)
        {
            return DISP_E_NONAMEDARGS;
        }
    }


于是查看ApplicationEvents4的定义,这三个方法定义如下:
C/C++ code

 [id(0x00000002), helpcontext(0x00061a82)]
        void Quit();
        [id(0x00000003), helpcontext(0x00061a83)]
        void DocumentChange();

[id(0x00000009), helpcontext(0x00061a88)]
        void NewDocument([in] Document* Doc);


于是怀疑是NewDocument方法有参数“Document* Doc”导致。后google到下面的内容
TN0029 (3/25/97)
Question
Do MFC's CCmdTarget derived classes support named parameters?
Answer
No, at least not as of VC++ 5.0. The COleDispatchImpl::Invoke method in oledisp1.cpp has this code at line #1192
// most of the time, named arguments are not supported
if (pDispParams->cNamedArgs != 0)
{
// only special PROPERTYPUT named argument is allowed
if (pDispParams->cNamedArgs != 1 ||
pDispParams->rgdispidNamedArgs[0] !=
DISPID_PROPERTYPUT)
{
return DISP_E_NONAMEDARGS;
}
}
This is the code does just what the comment says "named arguments are not supported".


http://www.winwrap.com/web/basic/reference/?p=doc_tn0029_technote.htm

个人理解就是vc6是不支持带有参数的event的?不知道这么理解对不对,如果是这样的话。我应该怎么处理带有参数的event呢?

关键代码如下:
C/C++ code

BEGIN_DISPATCH_MAP(CMySink, CCmdTarget)
    //{{AFX_DISPATCH_MAP(CMySink)
        DISP_FUNCTION_ID(CMySink,"Quit",          2,OnQuit,VT_EMPTY,VTS_NONE)
        DISP_FUNCTION_ID(CMySink,"DocumentChange",3,OnDocumentChange,VT_EMPTY,VTS_NONE)
        DISP_FUNCTION_ID(CMySink,"NewDocument",   9,OnNewDocument,VT_EMPTY,VTS_DISPATCH)
    //}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
BEGIN_INTERFACE_MAP(CMySink, CCmdTarget)
    INTERFACE_PART(CMySink, DIID_ApplicationEvents4, Dispatch)
END_INTERFACE_MAP()
void CMySink::OnQuit()
{
    AfxMessageBox(_T("OnQuit"));
}

void CMySink::OnDocumentChange()
{
    AfxMessageBox(_T("OnDocumentChange"));
}


void CMySink::OnNewDocument(LPDISPATCH lpdisDoc)
{
    AfxMessageBox(_T("OnNewDocument"));
}


作者: zaccheodong   发布时间: 2011-12-05

顶一顶,在线等回复

作者: zaccheodong   发布时间: 2011-12-06

顶贴。Quit和DocumentChange都支持了,为啥OnNewDocument不支持呢?CMySink::OnNewDocument的参数跟接口规定的参数不一致?还是你有意这么写的?欢迎去回答我的问题3ku

作者: Daisy__Ben   发布时间: 2011-12-06