Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Creating delegate from a function pointer

, 26 Jun 2011
Rate this:
Please Sign up or sign in to vote.
This tip shows you how to create a delegate from a C++ function pointer
//Assume you have managed to somehow pass a function pointer from C++ as Cplussplusfunctionptr to C#. In my case I was lucky, becoz I had managed C++ as a mid layer to convert void* to IntPtr;

IntPtr func = Cplussplusfunctionptr;
 
GuiFactoryOnNotify delegateOnNotify = (GuiFactoryOnNotify)Marshal.GetDelegateForFunctionPointer
                (func, typeof(GuiFactoryOnNotify)); 
 
 
Thanks to cory nelson in stack overflow for the original tip.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
You don't learn patterns, you just code it.
Follow on   Twitter

Comments and Discussions

 
QuestionCalling Service written in C, from C# PinmemberMuneeb R. Baig4-Jul-11 11:54 
AnswerRe: Calling Service written in C, from C# PinmemberMukit, Ataul4-Jul-11 19:44 
QuestionRe: Calling Service written in C, from C# PinmemberMuneeb R. Baig4-Jul-11 19:58 
AnswerRe: Calling Service written in C, from C# PinmemberMukit, Ataul4-Jul-11 20:08 
One solution can be, which might sound a little complicated (atleast to me), create a COM enabled C# project and through COM maintain the IPC.
It is possible to create and communicate through COM in C# applications. So from your service app, you can CoCreate the C# COM and communicate directly with the service.

Another easy solution I can think of is, use a Managed C++ project which should be able to communicate with your service application directly. Instead of the application executable, put the managed C++ dll (which uses IPC mechanizms to directly communicate to the running instance of the application) in the same folder of your C# application and call some exported managed C++ routines from your C# application (through dllimport in C#) which in turn does the Interprocess Communication kind of stuff.
 

I hope it helps.
GeneralRe: Calling Service written in C, from C# PinmemberMuneeb R. Baig4-Jul-11 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 27 Jun 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid