473,574 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
StartVideoCaptu re(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,v oid
*DataBuf,DWORD Length,int FrameType,void *context);

2.
DLLEXPORT_API int __stdcall
RegisterStreamR eadCallback(STR EAM_READ_CALLBA CK StreamReadCallb ack, void
*Context);
I have tried with something like this but with no luck.

public delegate short STREAM_DIRECT_R EAD_CALLBACK( uint
channelNumber,[MarshalAs(Unman agedType.IUnkno wn)]object DataBuf,uint
Length,int FrameType,[MarshalAs(Unman agedType.IUnkno wn)]object Context);

[DllImport("DsSd k.dll", CharSet=CharSet .Ansi)]
public static extern short
RegisterStreamD irectReadCallba ck(STREAM_DIREC T_READ_CALLBACK
StreamDirectRea dCallback,[MarshalAs(Unman agedType.IUnkno wn)]object
Context);

And then:
RegisterStreamD irectReadCallba ck(new
STREAM_DIRECT_R EAD_CALLBACK(th is.StreamDirect ReadCallback),
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 10212
Henrik,

Your delegate should look like this:

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

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

[DllImport("DsSd k.dll", CharSet=CharSet .Ansi)]
public static extern int RegisterStreamD irectReadCallba ck(
STREAM_DIRECT_R EAD_CALLBACK StreamDirectRea dCallback,
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.co m
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
StartVideoCaptu re(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,v oid
*DataBuf,DWORD Length,int FrameType,void *context);

2.
DLLEXPORT_API int __stdcall
RegisterStreamR eadCallback(STR EAM_READ_CALLBA CK StreamReadCallb ack, void
*Context);
I have tried with something like this but with no luck.

public delegate short STREAM_DIRECT_R EAD_CALLBACK( uint
channelNumber,[MarshalAs(Unman agedType.IUnkno wn)]object DataBuf,uint
Length,int FrameType,[MarshalAs(Unman agedType.IUnkno wn)]object Context);

[DllImport("DsSd k.dll", CharSet=CharSet .Ansi)]
public static extern short
RegisterStreamD irectReadCallba ck(STREAM_DIREC T_READ_CALLBACK
StreamDirectRea dCallback,[MarshalAs(Unman agedType.IUnkno wn)]object
Context);

And then:
RegisterStreamD irectReadCallba ck(new
STREAM_DIRECT_R EAD_CALLBACK(th is.StreamDirect ReadCallback),
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
2043
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
1782
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 managed class. The unmanaged DLL gets the address of the callback in a struct (someStruct below) that is passed as a parameter. I can't seem to...
7
3587
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 the iteration is something more interesting than this which relies on the callback mechanism. The function is an existing interface, and I cannot...
3
3051
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 shared_ptr<cBaseas a parameter, and return void. The code was working fine, although I have encountered problems (under a Microsoft compiler, of...
2
2627
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 function... I have mentioned my doubts in code comments. My code is... void myfun_callback(void(*fp)(char*)) //is this function is a call back? {
10
6946
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 MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
2100
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 callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call...
6
2153
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 right that if function A is: function A(*function pointer to B aka callback func, other arguments) { call (B); //calls }
5
4301
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); void myfunction (callback_t callback, void *userdata); How do I translate this to C#? I tried with:
0
7831
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7746
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8092
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6497
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5641
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5331
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3785
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2258
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.