c++wmp

Why my IWMPEvents functions are never called?


I have problem with IWMPEvents, which I advised successfully but never raised. Here is how I create embeded Windows Media Player:

HRESULT MyWMP::setURL(wchar_t* url)
{
    return pMediaPlayer->put_URL(url);   // Load and play songs successfully;
}

bool MyWMP::CreatePlayer()
{
    HRESULT hr;
    const CLSID CLSID_WindowsMediaPlayer = { 0x6BF52A52, 0x394A, 0x11d3,{ 0xB1, 0x53, 0x00, 0xC0, 0x4F, 0x79, 0xFA, 0xA6 } };
    hr = ::OleCreate(CLSID_WindowsMediaPlayer, IID_IOleObject, OLERENDER_DRAW, 0, this, this, (void**)&oleObject);
    if (SUCCEEDED(hr) && oleObject)
    {
        if (SUCCEEDED(hr)) hr = oleObject->SetClientSite(this);
        if (SUCCEEDED(hr)) hr = OleSetContainedObject(oleObject, TRUE);

        if (SUCCEEDED(hr))
        {
            RECT posRect;
            ::SetRect(&posRect, -300, -300, 300, 300);
            hr = oleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, this, -1, this->hwnd, &posRect);
        }
        hr = oleObject->QueryInterface(&pMediaPlayer);
    }
    if (FAILED(hr) || !pMediaPlayer)
    {
        MessageBox(NULL, L"Create Browser failed!", L"Error", MB_ICONERROR);
        return false;
    }
    return true;
}

MyWMP::MyWMP(HWND parentHWND, HINSTANCE hInstance) :
    isFullyCreated(false)
{
    OleInitialize(NULL);
    HRESULT hr = E_FAIL;
    IConnectionPointContainer* container = nullptr;
    IUnknown* punk = nullptr;

    this->parentHWND = parentHWND;
    this->hwnd = CreateWindow(L"Static", NULL, WS_CHILD | WS_VISIBLE, 221, 0, 300, 300, parentHWND, NULL, hInstance, 0);

    iComRefCount = 0;
    ::SetRect(&rObject, 0, 0, 300, 300);

    if (CreatePlayer())
    {
        hr = pMediaPlayer->get_settings(&pMediaPlayerSettings);
        if (SUCCEEDED(hr) && pMediaPlayerSettings)
        {
            pMediaPlayerSettings->put_autoStart(VARIANT_TRUE);
            pMediaPlayerSettings->put_volume(100);
        }
        hr = pMediaPlayer->QueryInterface(IID_IConnectionPointContainer, (void**)&container);
        if (SUCCEEDED(hr) && container) hr = container->FindConnectionPoint(__uuidof(IWMPEvents), &callback);
        if (FAILED(hr) && container)
        {
            hr = container->FindConnectionPoint(__uuidof(_WMPOCXEvents), &callback);
        }
        if (SUCCEEDED(hr) && callback)
        {
            CWMPEventDispatch *cw = new CWMPEventDispatch();
            IUnknown *cwUnk = NULL;
            if (SUCCEEDED(cw->QueryInterface(IID_IUnknown, (void**)&cwUnk)))
            {
                if (SUCCEEDED(hr) && container)
                {
                    hr = callback->Advise(cwUnk, &eventCookie);
                    if (SUCCEEDED(hr) && eventCookie)
                    {
                        isFullyCreated = true; // Set breakpoint here and is triggered
                                               // by debugger
                                               // hr = S_OK
                    }
                }
            }
            if (cwUnk) cwUnk->Release();
        }
        if (punk) punk->Release();
        if (container) container->Release();
    }
}

Bellow is the CWMPEventDispatch class. I set breakpoint at all IDispatch functions and none of them get triggered by debugger. When I load new songs, play/pause (on the embeded UI control buttons), these functions is never called.

// CWMPEventDispatch.h : Declaration of the event dispatcher
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//

#include "stdafx.h"
#include "wmpids.h"
#include "wmp.h"

class CWMPEventDispatch :
    public IWMPEvents,
    public _WMPOCXEvents
{
public:

    // ----- IUnknown -----
    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void**ppvObject) override;
    virtual ULONG STDMETHODCALLTYPE AddRef(void);
    virtual ULONG STDMETHODCALLTYPE Release(void);

    // IDispatch methods
    STDMETHOD(GetIDsOfNames)(REFIID /*riid*/,
        __in_ecount(cNames) LPOLESTR FAR * /*rgszNames*/,
        unsigned int /*cNames*/,
        LCID /*lcid*/,
        DISPID FAR * /*rgDispId*/)
    {
        return(E_NOTIMPL);
    }

    STDMETHOD(GetTypeInfo)(unsigned int /*iTInfo*/,
        LCID /*lcid*/,
        ITypeInfo FAR *FAR * /*ppTInfo*/)
    {
        return(E_NOTIMPL);
    }

    STDMETHOD(GetTypeInfoCount)(unsigned int FAR * /*pctinfo*/)
    {
        return(E_NOTIMPL);
    }

    STDMETHOD(Invoke)(DISPID  dispIdMember,
        REFIID  /*riid*/,
        LCID  /*lcid*/,
        WORD  /*wFlags*/,
        DISPPARAMS FAR*  pDispParams,
        VARIANT FAR*  /*pVarResult*/,
        EXCEPINFO FAR*  /*pExcepInfo*/,
        unsigned int FAR*  /*puArgErr*/);


private:
    int iComRefCount;
};

#include "stdafx.h"
#include "myWMPEventDispatch.h"

#pragma region ----- IUnknown -----
ULONG STDMETHODCALLTYPE CWMPEventDispatch::AddRef(void) { return ++iComRefCount; }
ULONG STDMETHODCALLTYPE CWMPEventDispatch::Release(void) { return --iComRefCount; }

HRESULT STDMETHODCALLTYPE CWMPEventDispatch::QueryInterface(REFIID riid, void**ppvObject)
{
    if (riid == __uuidof(IUnknown))        *ppvObject = static_cast<IDispatch*>(this);
    else if (riid == __uuidof(IDispatch))       *ppvObject = static_cast<IDispatch*>(this);
    else if (riid == __uuidof(IWMPEvents))  *ppvObject = static_cast<IWMPEvents*>(this);
    else if (riid == __uuidof(_WMPOCXEvents)) *ppvObject = static_cast<_WMPOCXEvents*>(this);
    else
        return E_NOINTERFACE;

    AddRef();
    return S_OK;
}
#pragma endregion

HRESULT CWMPEventDispatch::Invoke(
    DISPID  dispIdMember,
    REFIID  /*riid*/,
    LCID  /*lcid*/,
    WORD  /*wFlags*/,
    DISPPARAMS FAR*  pDispParams,
    VARIANT FAR*  /*pVarResult*/,
    EXCEPINFO FAR*  /*pExcepInfo*/,
    unsigned int FAR*  /*puArgErr*/)
{
    if (!pDispParams)
        return E_POINTER;

    if (pDispParams->cNamedArgs != 0)
        return DISP_E_NONAMEDARGS;

    HRESULT hr = DISP_E_MEMBERNOTFOUND;


    return(hr);
}

Question:

Why my IWMPEvents functions are never called, and how to fix it?

I attack full source code as a 7z archive. You can download it here.

Environment: Win 10 x64, VS 2017 Community


Solution

  • This piece of code:

    if (SUCCEEDED(hr) && container) hr = container->FindConnectionPoint(__uuidof(IWMPEvents), &callback);
    if (FAILED(hr) && container)
    {
        hr = container->FindConnectionPoint(__uuidof(_WMPOCXEvents), &callback);
    }
    

    Will succeed on the first line, so you will hook events on the IWMPEvents only, you will not continue and hook _WMPOCXEvents.

    IWMPEvents are early-bound (IUnknown derived) events, so the Media Player will indeed call IWMPEvents::PlayStateChange(...) IWMPEvents::StatusChange(...) etc. but it won't call IDispatch::Invoke with corresponding DISPIDs.

    If you want IDispatch events, just remove the first FindConnectionPoint, or call both.