Click here to Skip to main content
Licence 
First Posted 4 Jan 2005
Views 59,366
Bookmarked 29 times

Dynamic Invoke from Unmanaged DLL

By | 4 Jan 2005 | Article
This article explains invoking unmanaged DLL functions from C#.

Introduction

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 = 
     AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, 
     AssemblyBuilderAccess.Run);
   ModuleBuilder dynamicMod = 
     dynamicAsm.DefineDynamicModule("tempModule");

   // 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, 
     CharSet.Ansi);

   // This global method is now complete
   dynamicMod.CreateGlobalFunctions();

   // 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.
   MessageBox.Show(System.Text.ASCIIEncoding.ASCII.GetString(verstr));
   return retval;
  }

Sample calling of the method is as below:

DynamicDllFunctionInvoke(@"c:\getmyversion.dll","GetDllVersion");

License

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

calaquendi

Team Leader

Turkey Turkey

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmembermerikin4:14 28 Jun '10  
Questionreal dynamic inoke Pinmemberfredarmoni0:10 21 Aug '07  
QuestionHow to Unload a DLL PinmemberRonakkumar Patel13:21 31 May '07  
Questionwhen load dll of vc++6.0 its generated error?? Pinmemberkeyur_patel21:57 26 Mar '07  
AnswerRe: when load dll of vc++6.0 its generated error?? Pinmembercalaquendi22:34 26 Mar '07  
QuestionReturned value does not work Pinmembermczard0:10 31 May '06  
Generalref string PinmemberJohn Orosz6:34 17 Mar '05  
QuestionRe: ref string Pinmembernpat11:11 19 Jan '07  
AnswerRe: ref string PinmemberJorge Branco10:28 29 Aug '08  
GeneralRe: ref string PinmemberJorge Branco10:39 29 Aug '08  
QuestionDllImport Attribute? PinprotectorMarc Clifton5:25 4 Jan '05  
AnswerRe: DllImport Attribute? PinmemberChris Richardson10:40 4 Jan '05  
GeneralRe: DllImport Attribute? PinprotectorMarc Clifton10:56 4 Jan '05  
GeneralRe: DllImport Attribute? PinmemberChris Richardson11:06 4 Jan '05  
AnswerRe: DllImport Attribute? Pinmembercalaquendi20:09 4 Jan '05  
AnswerRe: DllImport Attribute? PinmemberHarkos0:20 5 Jan '05  
GeneralRe: DllImport Attribute? PinsussSuperfan15:01 25 Jan '05  
GeneralRe: DllImport Attribute? PinmemberHarkos23:23 25 Jan '05  
GeneralRe: DllImport Attribute? PinsussSuperfan5:34 26 Jan '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 4 Jan 2005
Article Copyright 2005 by calaquendi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid