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;
}
}