Delegates - have I got it right?

B

beaker

Hello,

I'm trying to make a user control which acts as a frame grabber, and I'm
using the functionality in avicap32.dll to do this. I need to setup a
callback function which gets called whenever a new frame of video fills
the buffer.

My problem is that the breakpoint in the video callback is never
reached. I realise it's likely to be a problem with the way I'm using
avicap, but just wondered if I'm using the delegates correctly as this
is a completely new area to me, and I've been a little confused about them?

Thanks,

Gary



In my namespace I declare:

public delegate bool CallBack();


In my class I have the following for API calls:

[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam,
int lParam);
// A version of SendMessage where the lparam is a delegate/callback
function
[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendMessageWithLparamDelegate(int hWnd, uint
Msg, int wParam, CallBack lpFunc);


To set up my callback function (after successfully creating a capture
window, mCapHwnd) in one of my methods (return values indicate success):

CallBack vidCallBack = new CallBack(VideoStreamCallBack);
ret = SendMessageWithLparamDelegate(mCapHwnd,
WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, vidCallBack);
CallBack errCallBack = new CallBack(ErrorCallBack);
ret = SendMessageWithLparamDelegate(mCapHwnd,
WM_CAP_SET_CALLBACK_ERROR, 0, errCallBack);


And my callbacks (just got breakpoints on the return lines at the moment)

// Should be called everytime the video buffer is filled
public bool VideoStreamCallBack()
{
// Do something

return true;
}
// Error handling callback for avicap
public bool ErrorCallBack()
{
// Do something

return true;
}
 
B

beaker

Well, I've discovered it seems to work - managed to force an error and
ErrorCallBack() got called as it should.

Still no luck with my VideoStreamCallBack() so if anyone can shed any
light on using avicap I'd be grateful - plenty of examples on the web
that use the clipboard, but I want to avoid that method.
 
G

Guest

Hi Beaker,
I do not have experience using avicap to capture video but I have used a
project that wraps DirectShow into a managed component to capture video,
maybe this will be useful for you. There is an article at the following link:

http://www.codeproject.com/cs/media/Motion_Detection.asp

Mark Dawson
http://www.markdawson.org


beaker said:
Well, I've discovered it seems to work - managed to force an error and
ErrorCallBack() got called as it should.

Still no luck with my VideoStreamCallBack() so if anyone can shed any
light on using avicap I'd be grateful - plenty of examples on the web
that use the clipboard, but I want to avoid that method.

Hello,

I'm trying to make a user control which acts as a frame grabber, and I'm
using the functionality in avicap32.dll to do this. I need to setup a
callback function which gets called whenever a new frame of video fills
the buffer.

My problem is that the breakpoint in the video callback is never
reached. I realise it's likely to be a problem with the way I'm using
avicap, but just wondered if I'm using the delegates correctly as this
is a completely new area to me, and I've been a little confused about them?

Thanks,

Gary



In my namespace I declare:

public delegate bool CallBack();


In my class I have the following for API calls:

[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam,
int lParam);
// A version of SendMessage where the lparam is a delegate/callback
function
[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendMessageWithLparamDelegate(int hWnd, uint
Msg, int wParam, CallBack lpFunc);


To set up my callback function (after successfully creating a capture
window, mCapHwnd) in one of my methods (return values indicate success):

CallBack vidCallBack = new CallBack(VideoStreamCallBack);
ret = SendMessageWithLparamDelegate(mCapHwnd,
WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, vidCallBack);
CallBack errCallBack = new CallBack(ErrorCallBack);
ret = SendMessageWithLparamDelegate(mCapHwnd,
WM_CAP_SET_CALLBACK_ERROR, 0, errCallBack);


And my callbacks (just got breakpoints on the return lines at the moment)

// Should be called everytime the video buffer is filled
public bool VideoStreamCallBack()
{
// Do something

return true;
}
// Error handling callback for avicap
public bool ErrorCallBack()
{
// Do something

return true;
}
 
L

Lebesgue

I think your problem is that you have delegate instances as local fields in
the method body, and they may (and will) get collected, so there is no
chance to invoke the callbacks from the unmanaged code.
What you need to do is have static references to these instances, so they
will not get collected.
This is just my thought based on what I've read somewhere some time ago,
maybe it will help you.

beaker said:
Well, I've discovered it seems to work - managed to force an error and
ErrorCallBack() got called as it should.

Still no luck with my VideoStreamCallBack() so if anyone can shed any
light on using avicap I'd be grateful - plenty of examples on the web that
use the clipboard, but I want to avoid that method.

Hello,

I'm trying to make a user control which acts as a frame grabber, and I'm
using the functionality in avicap32.dll to do this. I need to setup a
callback function which gets called whenever a new frame of video fills
the buffer.

My problem is that the breakpoint in the video callback is never reached.
I realise it's likely to be a problem with the way I'm using avicap, but
just wondered if I'm using the delegates correctly as this is a
completely new area to me, and I've been a little confused about them?

Thanks,

Gary



In my namespace I declare:

public delegate bool CallBack();


In my class I have the following for API calls:

[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam,
int lParam);
// A version of SendMessage where the lparam is a delegate/callback
function
[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendMessageWithLparamDelegate(int hWnd, uint
Msg, int wParam, CallBack lpFunc);


To set up my callback function (after successfully creating a capture
window, mCapHwnd) in one of my methods (return values indicate success):

CallBack vidCallBack = new CallBack(VideoStreamCallBack);
ret = SendMessageWithLparamDelegate(mCapHwnd,
WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, vidCallBack);
CallBack errCallBack = new CallBack(ErrorCallBack);
ret = SendMessageWithLparamDelegate(mCapHwnd,
WM_CAP_SET_CALLBACK_ERROR, 0, errCallBack);


And my callbacks (just got breakpoints on the return lines at the moment)

// Should be called everytime the video buffer is filled
public bool VideoStreamCallBack()
{
// Do something

return true;
}
// Error handling callback for avicap
public bool ErrorCallBack()
{
// Do something

return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top