Click here to Skip to main content
11,929,505 members (61,662 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


89 bookmarked

Dynamic Invoke C++ DLL function in C#

, 26 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article introduces why I use dynamic invoke C++ DLL function in C# and how to call it.


This article introduces why I use dynamic invoke C++ DLL function in C# and how to call it.

Why I Use Dynamic Invoke Instead of Static Invoke?

Sometimes when I want to write and call some unmanaged function (C++ DLL function), I do the following:

In the *.cpp file...

int __declspec(dllexport) a(int b)
      return b;

... compile the *.cpp file to a *.dll file and in the C# file:

class Program
   private static extern int a(int b);
   static void Main(string[] args)

In this method, you see that I have to declare a static DLL filename, and when I compile to an excutable file (*.exe), I cannot change the DLL filename.

The second case is, I want to write some C++ DLL function for an ASP.NET/IIS (Internet Information Services) Web site. If I static invoke unmanaged, when I want to update the DLL file, I stop the Web site in IIS and replace the old file with the new file, but .. IIS still keeps the old DLL file. It does not release the file, and I have to stop the entire Web site, all services running in IIS for replacing the file.

So, I want to dynamically invoke an unmanaged file. I can load, invoke and free unmanaged DLL function initiatively.

How to Dynamic Invoke Unmanaged DLL Function?

The first way is by referring to dynamicinvokedll.aspx.

The second way that I want to introduce is how to use the WindowAPI function. We use three functions in kernel32.dll (this is a kernel DLL file and appears in [WindowRoot]\System32) LoadLibrary, GetProcess, and FreeLibrary. You can find the C++ declare type functions on MSDN ( I only show the declare in C#:

class Program
        [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
        static extern int LoadLibrary(
            [MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

        [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
        static extern IntPtr GetProcAddress( int hModule,
            [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

                [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
        static extern bool FreeLibrary(int hModule);

The LoadLibrary function loads an unmanaged file, the GetProcAddress function gets the function pointer of an unmanaged file and the FreeLibrary function frees the unmanaged file. Now I can get the function pointer in a.dll very easily:

int hModule = LoadLibrary(@"c:\a.dll");
if (hModule == 0) return false;
IntPtr intPtr = GetProcAddress(hModule, "a");

And when I want to free the a.dll file, I can call:


But C# doesn’t suport C++ function pointer, so we cannot invoke a C++ function pointer here. C# only has Delegate objects and we have to convert the function pointer to Delegate by Marshal.GetDelegateForFunctionPointer. It is declared in System.Runtime.InteropServices as follows:

public static Delegate GetDelegateForFunctionPointer (
      IntPtr ptr,
      Type t

(You can find more support for this function in MSDN.)

We first declare the Delegate for a function:

delegate int A(int b);

And get the delegate object as follows:

A a = (A)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(A));

Now we can invoke a function as follows:

static void Main(string[] args)

Hope you found this article helpful.


  • 26th June, 2008: Initial post


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


About the Author

Engineer NaisCorp -
Vietnam Vietnam
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWhat if I need to marshal parameters and return value? Pin
Member 789751523-Sep-13 8:14
memberMember 789751523-Sep-13 8:14 
GeneralThis helps me a lot!!!! Pin
Kydz_Leo19-May-12 6:01
memberKydz_Leo19-May-12 6:01 
QuestionIt's very good Pin
Andrewpeter22-Mar-12 22:47
memberAndrewpeter22-Mar-12 22:47 
GeneralMy vote of 5 Pin
Andrewpeter22-Mar-12 22:46
memberAndrewpeter22-Mar-12 22:46 
GeneralMy vote of 5 Pin
cmma8624-Feb-12 5:48
membercmma8624-Feb-12 5:48 
GeneralMy vote of 5 Pin
Morries13-Dec-11 16:38
memberMorries13-Dec-11 16:38 
QuestionPInvoke function unbalanced the stack Pin
Silaghi20-Nov-11 2:29
memberSilaghi20-Nov-11 2:29 
GeneralMy vote of 5 Pin
Xanblax11-Oct-11 7:01
memberXanblax11-Oct-11 7:01 
You explained pretty clear something that was not so obvious.
GeneralMy vote of 4 Pin
JimmyGong24-May-11 17:00
memberJimmyGong24-May-11 17:00 
GeneralMy vote of 5 Pin
Jangjeet Singh25-Apr-11 5:26
memberJangjeet Singh25-Apr-11 5:26 
GeneralMy vote of 5 Pin
LeKoosy31-Mar-11 4:27
memberLeKoosy31-Mar-11 4:27 
GeneralI have a problem Pin
henry369521-Nov-10 1:30
memberhenry369521-Nov-10 1:30 
GeneralRe: I have a problem Pin
Ali Fakoor1-Oct-11 23:16
memberAli Fakoor1-Oct-11 23:16 
GeneralRe: I have a problem Pin
purgatory28-May-12 22:40
memberpurgatory28-May-12 22:40 
GeneralA note about FreeLibrary() Pin
maeloc26-Jun-08 22:26
membermaeloc26-Jun-08 22:26 
GeneralRe: A note about FreeLibrary() Pin
twaindev1-Jul-08 6:18
membertwaindev1-Jul-08 6:18 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 26 Jun 2008
Article Copyright 2008 by Namdn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid