473,224 Members | 1,324 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,224 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 3849
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.