471,330 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Register Callback function to C++ DLL from C#



Hi

I am developing a wrapper for a C++ SDK DLL built for a video capture
card. After opening all channels on the card i call the function
StartVideoCapture(IntPtr channelHandle) what is supposed to happen after
that is that when the data stream is reday the DLL should raise an event
through a callback that i have registerd with the DLL. In that function
i can then access the data stream. The problem is just that the C++ Dll
uses void* pointers and i haven´t got the slightest clue on how to
Marshal that. I have the full source code of a C++ demo project and all
the DLL specifications. The function specs in the DLL lokks like this:

1.
typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void
*DataBuf,DWORD Length,int FrameType,void *context);

2.
DLLEXPORT_API int __stdcall
RegisterStreamReadCallback(STREAM_READ_CALLBACK StreamReadCallback, void
*Context);
I have tried with something like this but with no luck.

public delegate short STREAM_DIRECT_READ_CALLBACK( uint
channelNumber,[MarshalAs(UnmanagedType.IUnknown)]object DataBuf,uint
Length,int FrameType,[MarshalAs(UnmanagedType.IUnknown)]object Context);

[DllImport("DsSdk.dll", CharSet=CharSet.Ansi)]
public static extern short
RegisterStreamDirectReadCallback(STREAM_DIRECT_REA D_CALLBACK
StreamDirectReadCallback,[MarshalAs(UnmanagedType.IUnknown)]object
Context);

And then:
RegisterStreamDirectReadCallback(new
STREAM_DIRECT_READ_CALLBACK(this.StreamDirectReadC allback),
this.Handle);

But it keeps crashing hard every time i try to run it.
I have struggeld with this for a while now and desperatly need help..
/Henrik Pedersen
Sweden

*** Sent via Developersdex http://www.developersdex.com ***
Dec 10 '05 #1
1 9982
Henrik,

Your delegate should look like this:

public delegate int STREAM_DIRECT_READ_CALLBACK(
uint channelNumber,
IntPtr DataBuf,
uint Length,
int FrameType,
IntPtr Context);

And your method declaration to register the callback should look like
this:

[DllImport("DsSdk.dll", CharSet=CharSet.Ansi)]
public static extern int RegisterStreamDirectReadCallback(
STREAM_DIRECT_READ_CALLBACK StreamDirectReadCallback,
IntPtr Context);

Basically, void pointers are marshaled as IntPtr instance. Of course,
this means that you have to marshal whatever you want to pass yourself. If
you know that the context is always going to be of a more particular type,
then you can be more specific with the declaration.

Also, int in C++ marshals as an int in .NET.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
I am developing a wrapper for a C++ SDK DLL built for a video capture
card. After opening all channels on the card i call the function
StartVideoCapture(IntPtr channelHandle) what is supposed to happen after
that is that when the data stream is reday the DLL should raise an event
through a callback that i have registerd with the DLL. In that function
i can then access the data stream. The problem is just that the C++ Dll
uses void* pointers and i haven´t got the slightest clue on how to
Marshal that. I have the full source code of a C++ demo project and all
the DLL specifications. The function specs in the DLL lokks like this:

1.
typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void
*DataBuf,DWORD Length,int FrameType,void *context);

2.
DLLEXPORT_API int __stdcall
RegisterStreamReadCallback(STREAM_READ_CALLBACK StreamReadCallback, void
*Context);
I have tried with something like this but with no luck.

public delegate short STREAM_DIRECT_READ_CALLBACK( uint
channelNumber,[MarshalAs(UnmanagedType.IUnknown)]object DataBuf,uint
Length,int FrameType,[MarshalAs(UnmanagedType.IUnknown)]object Context);

[DllImport("DsSdk.dll", CharSet=CharSet.Ansi)]
public static extern short
RegisterStreamDirectReadCallback(STREAM_DIRECT_REA D_CALLBACK
StreamDirectReadCallback,[MarshalAs(UnmanagedType.IUnknown)]object
Context);

And then:
RegisterStreamDirectReadCallback(new
STREAM_DIRECT_READ_CALLBACK(this.StreamDirectReadC allback),
this.Handle);

But it keeps crashing hard every time i try to run it.
I have struggeld with this for a while now and desperatly need help..
/Henrik Pedersen
Sweden

*** Sent via Developersdex http://www.developersdex.com ***

Dec 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ma740988 | last post: by
7 posts views Thread by Kirk McDonald | last post: by
3 posts views Thread by ryan.mitchley | last post: by
2 posts views Thread by Pradeep | last post: by
5 posts views Thread by Jef Driesen | last post: by
reply views Thread by rosydwin | last post: by

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.