Click here to Skip to main content
15,174,684 members
Articles / Programming Languages / C#
Posted 25 Apr 2004


26 bookmarked

Calling Managed .NET Function from Unmanaged Windows Custom DLL.

Rate me:
Please Sign up or sign in to vote.
2.92/5 (11 votes)
25 Apr 20043 min read
It will help you to call a managed function from unmanaged DLL function.


Is it possible?

Yes, it is absolutely possible, but you have to remember one thing. You can’t call a .NET function from a Win32 Process. It is ever never possible until you externally load the CLR. So, in this article, we are going to see how to call a .NET function from a Windows Custom DLL (for example, a DLL written in VC++), within a .NET process.

Is it needful?

Yes, it will be needful. Developers who are working in Win32 API from .NET, some time requires notification from unmanaged code to a managed function in .NET environment (for example, to notify completion of an asynchronous call). At that time, they can use this methodology to complete their task.

Is the Example given useful?

No, we take this code example for demonstration purpose only. In this example, we are going to call an unmanaged code from managed code, and the unmanaged code itself again calls another managed code (you can use this technique to notify the .NET environment about an asynchronous event from unmanaged code).

Basic Idea

Usually, we can call a function through two methods. The first one is using function name, and the next one is through the function pointer. Here, we are going to call a function from another (unmanaged) environment, so we can’t use the function name directly, so we are going to use function pointer. The first step is, we have to give the function pointer to unmanaged code, and then the unmanaged code will use this function pointer to call the managed code.

The core thing is described in the following figure:

Image 1

For checking that, we will create one custom DLL in VC++, and implement two functions. One is SetCallBackPointer, which receives a function pointer from managed code and stores it in a global variable pFunction. Another function InvokeCallBack is to call the managed code using function pointer pFunction. Here is the code residing in the DLL source file.

// MyCustomDll.cpp
//Global Variable for store the function pointer.
void SetCallBackPointer(LPVOID FnAddress);
void InvokeCallBack();
//This function receive the address of the CallBack function as
//parameter and store it in a global variable to enable other function in 
// the DLL to call the CallBack Function.
void SetCallBackPointer(LPVOID FnAddress)
      pFunction = (CALLBACK_FUNCTION)FnAddress;
// For demonstration purpose only. In real World we may call 
// this function in response to a asynchronous message.
// this function will Invoke the callback function 
// through the function pointer.
void InvokeCallBack()
     (pFunction)(); // simply calls the Callback function through pointer.

Then we have to export the SetCallBackPointer to make it callable from out of the DLL.

// MyCustomDll.def

OK, now we can build our DLL and create MyCustomDll.dll. Next, we are going to write the C# code for the callback function:

//       Managed.cs 
//  Our Callback function which will be called by unmanaged code(Form windows dll).
public static void CallbackFunction()
      MessageBox.Show( "CallBack Function Called by Windows DLL");

We can’t directly give the function name as the function pointer as we do it in C, C++ or VC++. Fortunately, we can do that using delegates. (I hope that you are all familiar, with one of .NET’s wonderful future: delegates. If you are not familiar with delegates, please refer MSDN.)

Let’s declare a delegate and instantiate it as follows.

// Declaring Delegate for Callback Function
// note that the Delegate prototype and Callback function 
// prototype are same.
delegate void CallBackDelegate();
public static  CallBackDelegate myDelegate;

Next, we have to associate our callback function with the delegate:

// Associating callback function with delegate
myDelegate =new MyDelegate(CallbackFunction);

OK, now our function pointer is ready. Next step is, we have to send it to the DLL. To render the callback function (that is delegate) to DLL, we have to call SetCallBackPointer function in the custom DLL. For that, we have to declare a function prototype of SetCallBackPointer with DllImport attribute. Here is the declaration:

// this function prototype should match our Custom dll’s 
// ‘SetCallBackPointer’ functioin prototype.
private static extern void SetCallBackPointer(CallBackDelegate delegate);

Everything is ready now; remaining is, we have to call SetCallBackPointer function. The following call will invoke the SetCallBackPointer function in the MyCustomDll with CallBackFunction function pointer (delegate) as the argument:


Yes we got it, CallBackFunction is called from our custom DLL.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Technical Lead GE Healthcare
India India
Hey, I am mohamed hasan. Working as a software engineer in a India based Software Company.
Willing to learn new concepts and technologies.

Contact him at:

Comments and Discussions

GeneralMy vote of 5 Pin
Sivaraman Dhamodharan3-Nov-10 1:51
MemberSivaraman Dhamodharan3-Nov-10 1:51 
Questionthird party dll Pin
AGraca20-Nov-07 6:22
MemberAGraca20-Nov-07 6:22 
GeneralProblem when passing 2 parameters Pin
Chathruwan18-Sep-07 2:09
MemberChathruwan18-Sep-07 2:09 
Generalnice article Pin
Girish Nurani Sankaranarayanan20-May-07 21:35
MemberGirish Nurani Sankaranarayanan20-May-07 21:35 
GeneralAPI problem Pin
t4ure4n7-Aug-06 5:28
Membert4ure4n7-Aug-06 5:28 
GeneralCatching ActiveX callback in C# Pin
anderslundsgard8-May-06 3:15
Memberanderslundsgard8-May-06 3:15 
GeneralThis worked great with some changes... Pin
kayakdog247-Dec-05 9:29
Memberkayakdog247-Dec-05 9:29 
AnswerRe: This worked great with some changes... Pin
kayakdog249-Dec-05 5:28
Memberkayakdog249-Dec-05 5:28 
GeneralDelphi 7 Pin
Cstruter6-Nov-05 21:13
MemberCstruter6-Nov-05 21:13 
QuestionThanks for this article! But how can i call a delegate with params? Pin
CityWolf30-Oct-05 1:21
MemberCityWolf30-Oct-05 1:21 
AnswerRe: Thanks for this article! But how can i call a delegate with params? Pin
danielei30-Nov-05 5:54
Memberdanielei30-Nov-05 5:54 
kayakdog249-Dec-05 10:11
Memberkayakdog249-Dec-05 10:11 
aitzi14-Dec-05 0:44
Memberaitzi14-Dec-05 0:44 
GeneralDoes not work for .net/c++ Pin
Anonymous8-Apr-05 8:44
MemberAnonymous8-Apr-05 8:44 
QuestionWill this work for what I am trying to do? Pin
feldergarbfeldergarb10-Dec-04 10:56
Memberfeldergarbfeldergarb10-Dec-04 10:56 
GeneralDirect calling of managed code Pin
Wraith227-Apr-04 2:05
MemberWraith227-Apr-04 2:05 
GeneralRe: Direct calling of managed code Pin
hasansheik27-Apr-04 3:01
Memberhasansheik27-Apr-04 3:01 
GeneralRe: Direct calling of managed code Pin
Wraith227-Apr-04 3:48
MemberWraith227-Apr-04 3:48 
GeneralRe: Direct calling of managed code Pin
skboey13-Jul-04 0:50
Memberskboey13-Jul-04 0:50 
GeneralRe: Direct calling of managed code Pin
Wraith213-Jul-04 1:21
MemberWraith213-Jul-04 1:21 
GeneralRe: Direct calling of managed code Pin
skboey13-Jul-04 2:51
Memberskboey13-Jul-04 2:51 
GeneralRe: Direct calling of managed code Pin
Wraith213-Jul-04 4:36
MemberWraith213-Jul-04 4:36 
GeneralRe: Direct calling of managed code Pin
skboey14-Jul-04 1:12
Memberskboey14-Jul-04 1:12 
GeneralI doubt this will work correctly Pin
leppie26-Apr-04 8:10
Memberleppie26-Apr-04 8:10 
GeneralRe: I doubt this will work correctly Pin
Heath Stewart26-Apr-04 9:15
protectorHeath Stewart26-Apr-04 9:15 

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

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