Re: Problem with imported callback to C#

Usually it happens when callback declaration is using non-standard
marshalling - see CallingConvention flags defining how to deal with stack
and what are defaults. If stack doesn't correspond to calling model, you
might see this king of behavior

"Builder" <peter.gejgus@xxxxxxxxx> wrote in message

I created a DirectShow filter which detects faces from video. Filter
exposes interface, which enables to set a callback, which is called
when a face is detected. I made an application in C# which creates a
DirectShow graph with mentioned face detector filter and calls its

Excerpt of the face detector interface declaration is as follows
(method SetCallback is important):

/// Interface for callbacks
BYTE * pBuffer,
long BufferLen

DEFINE_GUID(IID_IFaceDetectorFilter, 0x5e3fa349, 0x566e, 0x49f7, 0x99,
0xfe, 0xb9, 0x91, 0x9b, 0xb7, 0xc4, 0x14);
DECLARE_INTERFACE_(IFaceDetectorFilter, IUnknown)

// Listener (Callback)
STDMETHOD(SetCallback)(IFaceCB * pCallback) PURE;

Interface IFaceDetectorFilter is implemented in the class

class CFaceDetectorFilter : public CTransInPlaceFilter,
public IFaceDetectorFilter,
public ISpecifyPropertyPages,
public CPersistStream


STDMETHODIMP SetCallback(IFaceCB * pCallback);

void DetectFace();


IFaceCB* m_pCallback;

And implementation of the SetCallback method looks like this:

STDMETHODIMP CFaceDetectorFilter::SetCallback(IFaceCB * pCallback)

m_pCallback = pCallback;

return NOERROR;

and implementation of the method DetectFace looks like this:

void CFaceDetectorFilter::DetectFace()
LONGLONG timestamp; // time, when face was detected
BYTE* data; // image data of the detected face
long data_lenght; // size of the image data

if (m_pCallback != NULL)
m_pCallback->BufferCB(timestamp, data, data_lenght);

OK, and now follows code in the C# application, which calls interface
of the Face detector filter. First I had to import filter's

public interface IFaceCB
int BufferCB(long FaceTime, IntPtr pBuffer, int BufferLen);

public interface IFaceDetectorFilter

int SetCallback(IFaceCB pCallback);

Of course, I implemented IFaceCB interface (and its method BufferCB)
and register it with method SetCallback():

public class FaceDetector : IFaceCB
public int BufferCB(long theStartTime, IntPtr theDstBuffer,
int theSize)

return 0;


public int Init()
int hr;
IBaseFilter ibfFaceDetector = null;
IFaceDetectorFilter faceDetectorFilter = null;


// create face detector filter
ibfFaceDetector = (IBaseFilter) new FaceDetectorFilter();
faceDetectorFilter = (IFaceDetectorFilter)ibfFaceDetector;

// register callback
hr = faceDetectorFilter.SetCallback(this);

public int Run()
// start the DirectShow grapg, start face detection


After running the DirectShow graph (after call Run method), face
detection started. But when first face was detected, callback BufferCB
was called from native code and application freeze. I don't know the
reason. Can anybody help me? Did you already encounter a similar
problem when imported calleback to C# is called from native code?


Relevant Pages

  • Re: Function pointers (Callback functions) in Java ?
    ... object to a local var called 'callback'. ... > In the above I don't see the Opps interface. ... The code was a very simple example of creating a named inner class ... Its may be worth knowing that you can create anonymous inner classes ...
  • Re: Callback Interface functions NOT being Called
    ... a callback interface interface ICallbackInterface which implements the ... callback functions of the buttons I create in Ribbon interface. ... The ribbon callbacks use IDispatch, always, so you must return ... You need COM_INTERFACE_ENTRY2(IDispatch, ICallbackInterface) ...
  • Re: Using .NET callback via COM
    ... possible solution is to "redefine" the interface as a managed __interface and ... > I can implement this interface in an unmanaged class and pass this ... The callback occurs successfully. ... > managed class as a member, how do I let my managed class know when the ...
  • Re: NT service and connection points
    ... A callback interface is an interface you specify in your server ... Connection points are just a standard ... Microsoft MVP, MCSD ...
  • Re: CEPTCL: Doesnt RX multicast, has anyone seen it work before?
    ... proc udpEventHandler {sock interface} { ... decodePdu $datapacket RX $interface ... ## callback should optionally rebuild the hello to use dynamic ...