By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Using C++ function pointers in C#

P: n/a
I have written a library in ('native') C++, and I have made a lot of use
of function pointers e.g.:

/* Example C++ callbacks:

typedef void (*CBFUNC1)(unsigned int, const char*, object&);
typedef object* (*CBFUNC2)(object*, struct _mystruct* );

*/

How may I call back into C# code? - ideally, I would like the callbacks
to 'raise events' (so that more than one event sinks may 'get in on the
act') but failing that, simply executing code on the C# side will suffice.

Mar 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Bart,

You can define a delegate in .NET, and then pass that to your unmanaged
code. The CLR will expose the delegate as a function pointer which you can
call from your unmanaged code.

You have a slight problem with your callback. The interop layer does
not know how to handle references in C++. You need to expose the object as
a pointer, not a reference. Additionally, .NET doesn't know what a C++
object is (it's not the same as System.Object). That will need to be passed
as an IntPtr, and I don't think there is much you can do with it in .NET.
The same thing goes for the second callback, you would have to return an
IntPtr, and it seems you are expecting something off the unmanaged heap (an
object allocated with new in C++, it would seem).

Your signatures for the delegates would be:

delegate void CbFunc1(uint param1, [MarshalAs(UnmanagedType.LPStr)] string
param2, IntPtr param3);
delegate IntPtr CbFunc2(IntPtr param1, ref _mystruct param2);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bart Simpson" <ea*********@springfield.com> wrote in message
news:dv**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
I have written a library in ('native') C++, and I have made a lot of use of
function pointers e.g.:

/* Example C++ callbacks:

typedef void (*CBFUNC1)(unsigned int, const char*, object&);
typedef object* (*CBFUNC2)(object*, struct _mystruct* );

*/

How may I call back into C# code? - ideally, I would like the callbacks to
'raise events' (so that more than one event sinks may 'get in on the act')
but failing that, simply executing code on the C# side will suffice.

Mar 14 '06 #2

P: n/a


Nicholas Paldino [.NET/C# MVP] wrote:
Bart,

You can define a delegate in .NET, and then pass that to your unmanaged
code. The CLR will expose the delegate as a function pointer which you can
call from your unmanaged code.

You have a slight problem with your callback. The interop layer does
not know how to handle references in C++. You need to expose the object as
a pointer, not a reference. Additionally, .NET doesn't know what a C++
object is (it's not the same as System.Object). That will need to be passed
as an IntPtr, and I don't think there is much you can do with it in .NET.
The same thing goes for the second callback, you would have to return an
IntPtr, and it seems you are expecting something off the unmanaged heap (an
object allocated with new in C++, it would seem).

Your signatures for the delegates would be:

delegate void CbFunc1(uint param1, [MarshalAs(UnmanagedType.LPStr)] string
param2, IntPtr param3);
delegate IntPtr CbFunc2(IntPtr param1, ref _mystruct param2);

Hope this helps.


Thanks. I notice you are passing the pointer toa struct as a ref
(instead of a IntPtr) in the 2nd delegate. Why is that (i.e. why not use
an IntPtr?)

Mar 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.