Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done

Mission:  From C#, P/Invoke C++ with callbacks to C#

//-----------------------------------------------------------------------

// inside unmanaged C# code

//-----------------------------------------------------------------------

//-----------------------------------------------------------------------

//delegates - define a signatures for unmanaged c++ code to call

//-----------------------------------------------------------------------

public delegate void MessageReceivedDelegate(

   int param1,

   string messageString,

   int param3,

   int param4,

   int param5);

 

 

public delegate void ExceptionParsedDelegate(

  string exceptionMessage);

 

//-----------------------------------------------------------------------

// P/invoke mapping with pointers to delegated methods

//-----------------------------------------------------------------------

[DllImport("my.dll"

   , CallingConvention = CallingConvention.StdCall)]

public static extern void an_unmanaged_function(

   string aa,

    int bb,

    string cc,

    string dd,

    int ee,

    int ff,

    MessageReceivedDelegate call,

    ExceptionParsedDelegate exception);

 

 

//-----------------------------------------------------------------------

// perform P/invoke

//-----------------------------------------------------------------------

an_unmanaged_function(  

aa

,bb

,cc

,dd

,ee

,ff

,new MessageReceivedDelegate(OnMessageReceived)

,new ExceptionParsedDelegate(OnExceptionReceived));

 

//-----------------------------------------------------------------------

// delegated methods (called inside unmanaged c++ code)

//-----------------------------------------------------------------------

public void OnExceptionReceived(

   string exceptionMessage)

{

 

    //handle exceptionMessage

}

protected void OnMessageParsed(myMessage theMesssage)

{

    //handle theMesssage

}

 

//-----------------------------------------------------------------------

// inside unmanaged c++ code ("my.dll")

//-----------------------------------------------------------------------

 

//define c++ delegate signatures

 

typedef void (__stdcall *callbackDelegatePointer)(

       int param1,

       char message[2100],

       int param3,

       int param4,

       int param5);

typedef void (__stdcall *exceptionDelegatePointer)(

       char* exceptionMessage);

 

//allow p/invoke by delcaring method “extern "C"”

extern "C" __declspec(dllexport)

void __stdcall an_unmanaged_function(

       char *aa,

       int   bb,

       char *cc,

       char *dd,

       int   ee,

       int   ff,

       callbackDelegatePointer onMessageReceived,

       exceptionDelegatePointer onException)

 

//call delegates

onMessageReceived(param1, message, param3, param4, param5);

             

onException("Hello Exception");

 

 

 

Posted on Tuesday, April 7, 2009 3:32 AM Delegates and Events , Interop & P/Invoke | Back to top


Comments on this post: From C#, P/Invoke C++ with callbacks to C#

# re: From C#, P/Invoke C++ with callbacks to C#
Requesting Gravatar...
Great web site. Than you for the information
his site is amazing. Very well developed with great information.
Left by classic jordan shoes on Dec 18, 2010 1:22 AM

# re: From C#, P/Invoke C++ with callbacks to C#
Requesting Gravatar...
What a wonderful post, thanks so much for sharing this.
Left by large canvas on Nov 17, 2011 9:52 AM

# re: From C#, P/Invoke C++ with callbacks to C#
Requesting Gravatar...
Please explain what is "myMessage" in
protected void OnMessageParsed(myMessage theMesssage)
{
//handle theMesssage
}
Also in dll am getting errors while calling delegates
//call delegates
onMessageReceived(param1, message, param3, param4, param5);

onException("Hello Exception");


Left by Syed on Sep 03, 2012 9:00 AM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net