0

I'm having real troubles trying to use a c++ callback in C# and any help from you would be really appreciated.

The first thing that the code do is to create an event:

uEvent = CreateEvent(NULL, true, false, "EventName");

After that, I've got that c++ function that implements a callback in c++:

int RegisterCallback(TCallbackType CallbackType, void *pLLTProfileCallback, void *pUserData)

The CallbackType is used to specify a stdcall. Then, the pLLTProfileCallback would be the name of the function to call, and finally, the user data.

With that function, I'm able to make a callback to another function by typing the following:

RegisterCallback(STD_CALL, (void*)FunctionName, 0)

Where the other function is:

void __stdcall FunctionName(const unsigned char* pucData, unsigned int uiSize, void* pUserData)

And then, I've got the code to wait for the event:

WaitForSingleObject(uEvent, 1000)

My problem comes when I try to do that in C#. The first thing I do is import the RegisterCallback Function from the dll:

[DllImport(DRIVER_DLL_NAME, EntryPoint = "s_RegisterCallback",CallingConvention = CallingConvention.StdCall)]
    internal static extern int RegisterCallback(uint pLLT, TCallbackType CallbackType, IntPtr pLLTProfileCallback, IntPtr pUserData);

But then, I don't know how to proceed. Could you please help me?

  • Is this [managed c++/CLI](http://en.wikipedia.org/wiki/C%2B%2B/CLI) or unmanaged, native c++? – dbc Aug 18 '14 at 14:45
  • It's native c++, thanks – user1410870 Aug 18 '14 at 14:48
  • Well that's much more difficult. Just to confirm: you are trying to attach a c# callback to some unmanaged c++ code? Been a long time since I have done this, but maybe this will work: http://stackoverflow.com/questions/2167895/howto-implement-callback-interface-from-unmanaged-dll-to-net-app – dbc Aug 18 '14 at 15:41
  • See also here: http://www.codeproject.com/Tips/318140/How-to-make-a-callback-to-Csharp-from-C-Cplusplus – dbc Aug 18 '14 at 15:47
  • Well, What I want to do is to implement, in C#, the c++ example that I've provided, using the dll. I think it is the opposite of your examples :-S Sorry and thanks for your help. – user1410870 Aug 18 '14 at 16:10
  • More like this then? http://stackoverflow.com/questions/14325802/pass-unmanaged-c-method-to-c-sharp-dll-for-callback – dbc Aug 18 '14 at 16:32
  • Basically, What I do know is how to load the c++ function from the c++ dll using C♯. After that, what I need to know is how to implement the RegisterCallback function in C♯ and also the __stdcall FunctionName. – user1410870 Aug 18 '14 at 17:41

1 Answers1

0

Delegates in C# which is equivalent to C++ callback.

Please refer to MSDN Introduction & Syntax and usage for same

Kunal Shivalkar
  • 111
  • 3
  • 3
  • Thanks, I've tried with delegates following the example in MSDN, but still get an error when putting the delegate in the function RegisterCallback, in the parameter void *pLLTProfileCallback – user1410870 Aug 18 '14 at 14:50