IConnectionPoint建议尝试引用不存在的令牌

时间:2014-06-09 13:12:55

标签: c++ events browser connection token

我目前的代码 类:

class BrowserEvents: public IDispatch
{ 
public:
    STDMETHODIMP Invoke(DISPID dispIdMember,REFIID riid,LCID lcid,WORD wFlags,DISPPARAMS *pDispParams,VARIANT *pVarResult,EXCEPINFO *pExcepInfo,UINT *puArgErr){
        switch(dispIdMember){
            case DISPID_NEWWINDOW2:
                //NewWindow2( pDispParams->rgvarg[ 0 ] , pDispParams );
                break;
        }

        return S_OK;
    }

    STDMETHODIMP QueryInterface(REFIID riid, void ** ppvObj){
            return E_NOTIMPL;
    } 

    STDMETHODIMP_(ULONG) AddRef(){
            return E_NOTIMPL;
    }
    STDMETHODIMP_(ULONG) Release(){
            return E_NOTIMPL;
    }

    STDMETHODIMP GetTypeInfoCount(UINT *iTInfo){
            return E_NOTIMPL;
    }
    STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo){
            return E_NOTIMPL;
    }
    STDMETHODIMP GetIDsOfNames(REFIID riid, OLECHAR **rgszNames, 
                          UINT cNames,  LCID lcid, DISPID *rgDispId){
            return E_NOTIMPL;
    }
private:
    void NewWindow2( IDispatch** ppDisp , bool cancel )  {
        printf( "test" );
    }
};

与ConnectionPoint的代码连接并添加侦听器

 IConnectionPointContainer* pServiceProvider2 = NULL;

        DWORD cookie;

        BrowserEvents eventsHandle;
        IConnectionPoint* connectPoint;

        if (SUCCEEDED(pBrowser2->QueryInterface(IID_IConnectionPointContainer, (void**)&pServiceProvider2))){

            hr = pServiceProvider2 -> FindConnectionPoint( DIID_DWebBrowserEvents2  , &connectPoint );

            if( SUCCEEDED( hr ) ){

                    hr = connectPoint -> Advise( &eventsHandle , &cookie );

                    if( SUCCEEDED( hr ) ){
                        printf( "yeeaah ;)");
                    }
                    else{
                        LPVOID lpMsgBuf;
                        LPVOID lpDisplayBuf;

                        DWORD dw = GetLastError(); 

                        FormatMessage(
                            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                            FORMAT_MESSAGE_FROM_SYSTEM |
                            FORMAT_MESSAGE_IGNORE_INSERTS,
                            NULL,
                            dw,
                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                            (LPTSTR) &lpMsgBuf,
                            0, NULL );


                        printf( reinterpret_cast<LPCTSTR>( lpMsgBuf ) );
                    }

                    connectPoint -> Release();
            }

            pServiceProvider2 -> Release();
        }

运行后

  hr = connectPoint -> Advise( &eventsHandle , &cookie );

上一个错误是

  An attempt was made to reference a token that does not exist.

我尝试以管理员身份运行它并且效果是一样的。

我想要实现的目标是监听DWebBrowserEvents http://msdn.microsoft.com/en-us/library/aa768283(v=vs.85).aspx

操作系统:Windows 7专业版

开发环境:Dev-C ++ 5.6.3 Orwell Edition

编译器:MinGW32 4.8.1

0 个答案:

没有答案