Callback from a C++ COM DLL to a C# application

2019-07-09 09:38发布

问题:

This is going to be a long post, as I want to expose you all the steps I tried to make this work :)

I have C++ COM dll which contains a VideoPlayer class which uses the Media Foundation API to display a video.

The VideoPlayer class is defined using an IDL file:

[
    object,
    uuid(74FDBBB1-BFFB-4F7E-ACA3-ADB0C7232790),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IVideoPlayer : IDispatch {

    [id(1)] HRESULT Initialize([in] HWND* video_hwnd, [in] HWND* event_hwnd);
    [id(2)] HRESULT OpenUrl([in] BSTR url_path);
    [id(3)] HRESULT Play();
    [id(4)] HRESULT HandleEvent([in] INT pEventPtr);
    [id(5)] HRESULT Repaint(void);
    [id(6)] HRESULT Resize([in] LONG width, [in] LONG height);
};

This class internally uses a custom presenter (which is based on the WPFMediaKit project), which outputs the video frames inside a IDirect3DSurface9 object.

The custom presenter needs a callback of type IEVRPresenterCallback, which is defined as follow:

MIDL_INTERFACE("B92D8991-6C42-4e51-B942-E61CB8696FCB")
IEVRPresenterCallback : public IUnknown
{
public:
    virtual HRESULT STDMETHODCALLTYPE PresentSurfaceCB(IDirect3DSurface9 *pSurface) = 0;
};

As you can see, it is not defined in a IDL file, but is declared in a header file.

I need to add a new function to the VideoPlayer class, which allows the calling C# code to pass an instance of a class inheriting from IEVRPresenterCallback, which will be set to the custom presenter.

I've tried to add this line to the IDL file of the VideoPlayer:

[id(7)] HRESULT RegisterCallback2([in] IEVRPresenterCallback * p_PresenterCallback);

But I get an error:

error MIDL2025: syntax error : expecting a type specification near "IEVRPresenterCallback"

I guess it is normal, because I didn't import anything in the IDL. Which is normal, as IEVRPresenterCallback is defined in a header file.

I tried to import the header file, but the MIDL_INTERFACE macro of the IEVRPresenterCallback definition generates an error:

error MIDL2025: syntax error : expecting an interface name or DispatchInterfaceName or CoclassName or ModuleName or LibraryName or ContractName or a type specification near "MIDL_INTERFACE"

I then tried to forward declare the interface, but I got this error:

error MIDL2011: unresolved type declaration : IEVRPresenterCallback [ Parameter 'p_PresenterCallback' of Procedure 'RegisterCallback2' ( Interface 'IVideoPlayer' ) ]

My last attempt was to change the definition of RegisterCallback, to have a pointer to IUnknown instead of IEVRPresenterCallback, and in the declaration of the function, I cast the pointer to the correct interface.

This makes the C++ dll compile correctly.

In the C# application, I set the callback as follow:

[ComVisible(true), ComImport, SuppressUnmanagedCodeSecurity, Guid("B92D8991-6C42-4e51-B942-E61CB8696FCB"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IEVRPresenterCallback
{
    [PreserveSig]
    int PresentSurfaceCB(IntPtr pSurface);
}

internal class EVRPresenterCallback : IEVRPresenterCallback
{
    public int PresentSurfaceCB(IntPtr pSurface)
    {
        return 0;
    }
}

public partial class MainWindow : Window
{
    private EmideeMediaFoundationLib.IVideoPlayer videoPlayer = new EmideeMediaFoundationLib.VideoPlayer();
    private EVRPresenterCallback callback = new EVRPresenterCallback();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        videoHost.VideoPlayer.RegisterCallback(callback);
        videoHost.VideoPlayer.OpenUrl(@"C:\Users\Public\Videos\Sample Videos\wildlife.wmv");
    }
}

The problem I get is despite the custom presenter calling the callback, I never get back in the C# PresentSurfaceCB function.

I'm completely stuck right now, and I don't know where the problem is, nor how to solve it :(

Any ideas?

Thanks in advance

回答1:

Thanks to Hans, I could make it work.

I moved the interface in the IDL file, and instead of returning a ID3D9Surface pointer in the callback, I return a DOWRD_PTR:

[
    uuid(B92D8991-6C42-4e51-B942-E61CB8696FCB),
]
interface IEVRPresenterCallback : IUnknown {
    [id(1)] HRESULT PresentSurfaceCB( DWORD_PTR pSurface);
}

[
    object,
    uuid(74FDBBB1-BFFB-4F7E-ACA3-ADB0C7232790),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IVideoPlayer : IDispatch {

    [id(1)] HRESULT Initialize([in] HWND* video_hwnd, [in] HWND* event_hwnd);
    [id(2)] HRESULT OpenUrl([in] BSTR url_path);
    [id(3)] HRESULT Play();
    [id(4)] HRESULT HandleEvent([in] INT pEventPtr);
    [id(5)] HRESULT Repaint(void);
    [id(6)] HRESULT Resize([in] LONG width, [in] LONG height);
    [id(7)] HRESULT RegisterCallback([in] IEVRPresenterCallback * p_PresenterCallback);
};

In my WPF application, I create a class deriving from IEVRCallback:

internal class EVRPresenterCallback : EmideeMediaFoundationLib.IEVRPresenterCallback
{
    public void PresentSurfaceCB(uint pSurface)
    {
    }
}

and I give this instance to the VideoPlayer object.