473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with imported callback to C#

Hello,

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
interface.

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

/// Interface for callbacks
DECLARE_INTERFACE(IFaceCB)
{
STDMETHOD(BufferCB)
(
LONGLONG FaceTime,
BYTE * pBuffer,
long BufferLen
) PURE;
};

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
CFaceDetectorFilter:

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

...

STDMETHODIMP SetCallback(IFaceCB * pCallback);

void DetectFace();

private:

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
interfaces:

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

[ComImport,
Guid("5E3FA349-566E-49F7-99FE-B9919BB7C414"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface IFaceDetectorFilter
{
...

[PreserveSig]
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?

Jun 27 '07 #1
3 3855
BTW, I used Visual C# 2005 Express to code and build C# application

Jun 27 '07 #2
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
HTH
Alex

"Builder" <pe**********@gmail.comwrote in message
news:11**********************@n2g2000hse.googlegro ups.com...
Hello,

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
interface.

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

/// Interface for callbacks
DECLARE_INTERFACE(IFaceCB)
{
STDMETHOD(BufferCB)
(
LONGLONG FaceTime,
BYTE * pBuffer,
long BufferLen
) PURE;
};

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
CFaceDetectorFilter:

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

...

STDMETHODIMP SetCallback(IFaceCB * pCallback);

void DetectFace();

private:

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
interfaces:

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

[ComImport,
Guid("5E3FA349-566E-49F7-99FE-B9919BB7C414"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface IFaceDetectorFilter
{
...

[PreserveSig]
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?


Jun 27 '07 #3
>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?

If you want to implement IFaceCB in C# you should make it a proper COM
interface and treat the callbac reference as such (calling
AddRef/Release etc).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tomaz Rotovnik | last post by:
Hi I created very simple dll (vc++) which has three functions (start, stop and initialization). it starts capturing sound from soundblaster and when the buffer is filled with the data, dll calls...
1
by: Mohamed Fysal | last post by:
I have written a Regular DLL with many Export Functions and one CALLBACK fun ction . The callback function declared in the .cpp file of the Regular DLL is as fol lows: typedef BOOL...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: kristian.hermansen | last post by:
keherman@ibmlnx20:/tmp$ cat helloworld.py #!/usr/bin/env python import pygtk pygtk.require('2.0')
3
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a...
8
by: Chris Cap | last post by:
I am getting some strange behavior when using RaiseCallbackEvent. I have a form that implements ICallbackEventHandler. During the client callback, a session variable is set that is used on the...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
22
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
1
by: shobram | last post by:
All problems below require some kind of input. You are free to implement any mechanism for feeding input into your solution (for example, using hard coded data within a unit test). You should...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.