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

Tagged as

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 
Hi, and thanks for the tip.
Further to this, I am looking to solve the following problem and I'll be thankful to you, if you can assist.
A service written in C exposing some normal and call back functions, running under Windows. For this Service, I have its TLB, dll and Lib files. I need to know how can I hook to the service from C# and call its function as well as subscribe to the delegates?
What I have done is successfully DllImported and called the function from the imported dll; however, I am unable to directly call the Running Instance.
-muneeb
A thing of beauty is the joy forever.

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 
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
Web03 | 2.8.140827.1 | Last Updated 27 Jun 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid