Pass a C# function to a COM interface that wants a function pointe

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Travis (Travis_at_discussions.microsoft.com)
Date: 12/10/04


Date: Fri, 10 Dec 2004 14:07:03 -0800

I have a Directshow filter I wrote in C++. I've created an interface to set
a callback function. The app I want to use the filter in is in C#. How do I
pass a function pointer from C# to a COM interface?

I have not been able to find much in the way of examples but I have tried
this:

//COM Object
typdef HRESULT (*MYCALLBACK) (IMediaSample * pSample);
DECLARE_INTERFACE_(IMyFilter, IUknown)
{
    STDMETHOD(SetCallbackPointer)(LPVOID FnAddress);
}
MYCALLBACK m_callback;
STDMETHODIMP SetCallbackPointer(LPVOID FnAddress)
{
    m_callback = (MYCALLBACK)FnAddress;
}
void InvokeCallback(IMediaSample * pSample)
{
    (m_callback)(pSample);
}
HRESULT Transform(IMediaSample *pSample)
{
    InvokeCallback(pSample);
}

//C#
public delegate int CallBackDelegate(ref IMediaSample mediaSample);
public class MyClass
{
    public CallBackDelegate myDelegate;
    public MyClass()
    {
        myDelegate = new CallBackDelegate(CallbackFunction);

        //all the stuff to create the COM object and get the interface
        myInterface.SetCallbackPointer(myDelegate);
    }
    public int CallbackFunction(ref IMediaSample mediaSample)
    {
        //my intention is get a copy of the mediaSample
        MessageBox.Show("Here");
        return 0;
    }
}



Relevant Pages

  • Re: DirecShow Filter and DX9 StretchRect ?
    ... I've a callback function at each frame of a video source where I get a data ... strecthRect function is working. ... > i have a directshow filter in which several zoom ... > DShow and DX3D... ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: whats a callback?
    ... >> A callback function is one that is not invoked explicitly by the ... The COM object interface are ... > pointers to an object-specific array of pointers. ... "prototype" and "function declaration", are synonymous. ...
    (comp.arch.embedded)
  • Re: whats a callback?
    ... >> A callback function is one that is not invoked explicitly by the ... The COM object interface are ... > pointers to an object-specific array of pointers. ... "prototype" and "function declaration", are synonymous. ...
    (sci.electronics.design)
  • Re: how to register a Callback function COM client to a COM server
    ... so that clients can register a callback function and when required COM ... I have an interface and I am ... Registercallback method pass ICallback* instead of the function ...
    (microsoft.public.vc.atl)