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

Dynamic Invoke from Unmanaged DLL

, 4 Jan 2005
Rate this:
Please Sign up or sign in to vote.
This article explains invoking unmanaged DLL functions from C#.


This article demonstrates how to call an unmanaged MFC DLL function from a C# code. Code uses Reflection namespace. To use code snippets below, you must add the following:

using System.Reflection.Emit;
using System.Reflection;

Imagine you have an MFC DLL (getmyversion.dll) with function named int GetDllversion(char* verstr) which returns your DLL's version in verstr.

The below function creates a dynamic assembly object. And DefinePInvokeMethod method creates a method which we will use to access our DLL function. To complete our global ops for our dynamic module, call CreateGlobalFunctions. Using GetMethod function of our previously created method, we can invoke our MFC DLL function.

public  object DynamicDllFunctionInvoke( string DllPath, string EntryPoint ) 
   // Version string definition
   byte[] verstr = new byte[1024];
   //Define return type of your dll function.
   Type returnType = typeof(int);        
   //out or in parameters of your function.   
   Type [] parameterTypes = {typeof(byte[])};
   object[] parameterValues = {verstr};
   string entryPoint = entrypoint;
   // Create a dynamic assembly and a dynamic module
   AssemblyName asmName = new AssemblyName();
   asmName.Name = "tempDll";
   AssemblyBuilder dynamicAsm = 
   ModuleBuilder dynamicMod = 

   // Dynamically construct a global PInvoke signature 
   // using the input information
   MethodBuilder dynamicMethod = dynamicMod.DefinePInvokeMethod(
     entryPoint, DllPath, MethodAttributes.Static | MethodAttributes.Public
     | MethodAttributes.PinvokeImpl , CallingConventions.Standard,
     returnType, parameterTypes, CallingConvention.Winapi, 

   // This global method is now complete

   // Get a MethodInfo for the PInvoke method
   MethodInfo mi = dynamicMod.GetMethod(EntryPoint);
   // Invoke the static method and return whatever it returns
   object retval = mi.Invoke(null, parameterValues);
   // Filled verstr paramter.
   return retval;

Sample calling of the method is as below:



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

Team Leader
Turkey Turkey
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pinmembermerikin28-Jun-10 5:14 
Questionreal dynamic invoke [modified] Pinmemberfredarmoni21-Aug-07 1:10 
AnswerRe: real dynamic inoke PinmemberShawn-USA5-Aug-13 16:21 
QuestionHow to Unload a DLL PinmemberRonakkumar Patel31-May-07 14:21 
AnswerRe: How to Unload a DLL PinmemberShawn-USA5-Aug-13 16:24 
Questionwhen load dll of vc++6.0 its generated error?? Pinmemberkeyur_patel26-Mar-07 22:57 
AnswerRe: when load dll of vc++6.0 its generated error?? Pinmembercalaquendi26-Mar-07 23:34 
QuestionReturned value does not work Pinmembermczard31-May-06 1:10 
Generalref string PinmemberJohn Orosz17-Mar-05 7:34 
i played around with this code and was able to get it to function *almost* the way i wanted. my problem is with sending a ref string so i can get something back. i changed the type to be something like
Type[] paramTypes = { Type.GetType( "System.String&" ) };
and my unmanaged dll gets the string, and changeds it correctly (i've verified this by printing the string within the dll), but i always get the same string i passed in the scope of the C# program that called the dll.
i had to define an implementation flag to get the return type to work, is there a similar flag to get referenced parameters back?
QuestionRe: ref string Pinmembernpat19-Jan-07 12:11 
AnswerRe: ref string PinmemberJorge Branco29-Aug-08 11:28 
GeneralRe: ref string PinmemberJorge Branco29-Aug-08 11:39 
QuestionDllImport Attribute? PinprotectorMarc Clifton4-Jan-05 6:25 
AnswerRe: DllImport Attribute? PinmemberChris Richardson4-Jan-05 11:40 
GeneralRe: DllImport Attribute? PinprotectorMarc Clifton4-Jan-05 11:56 
GeneralRe: DllImport Attribute? PinmemberChris Richardson4-Jan-05 12:06 
AnswerRe: DllImport Attribute? Pinmembercalaquendi4-Jan-05 21:09 
AnswerRe: DllImport Attribute? PinmemberHarkos5-Jan-05 1:20 
GeneralRe: DllImport Attribute? PinsussSuperfan25-Jan-05 16:01 
GeneralRe: DllImport Attribute? PinmemberHarkos26-Jan-05 0:23 
GeneralRe: DllImport Attribute? PinsussSuperfan26-Jan-05 6:34 

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 | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 4 Jan 2005
Article Copyright 2005 by calaquendi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid