473,385 Members | 1,673 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,385 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 10188
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a...
7
by: Kirk McDonald | last post by:
Let's say I have a function that takes a callback function as a parameter, and uses it to describe an iteration: def func(callback): for i in : callback(i) For the sake of argument, assume...
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...
2
by: Pradeep | last post by:
Hi all, Can any one explain me what is callback function.... I have written some code after reading some tutorials from internet... But I am not sure is it a right way to write a call back...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
6
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.