ModuleBuilder.DefinePInvokeMethod Method (String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
Defines a PInvoke method with the specified name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
public MethodBuilder DefinePInvokeMethod( string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet )
Parameters
- name
- Type: System.String
The name of the PInvoke method. name cannot contain embedded nulls.
- dllName
- Type: System.String
The name of the DLL in which the PInvoke method is defined.
- attributes
- Type: System.Reflection.MethodAttributes
The attributes of the method.
- callingConvention
- Type: System.Reflection.CallingConventions
The method's calling convention.
- returnType
- Type: System.Type
The method's return type.
- parameterTypes
- Type: System.Type[]
The types of the method's parameters.
- nativeCallConv
- Type: System.Runtime.InteropServices.CallingConvention
The native calling convention.
- nativeCharSet
- Type: System.Runtime.InteropServices.CharSet
The method's native character set.
| Exception | Condition |
|---|---|
| ArgumentException | The method is not static or if the containing type is an interface. -or- The method is abstract. -or- The method was previously defined. |
| ArgumentNullException | name or dllName is null. |
| InvalidOperationException | The containing type has been previously created using CreateType |
Some DLL import attributes (see the description of System.Runtime.InteropServices.DllImportAttribute) cannot be specified as arguments to this method. Such attributes should be set by emitting a custom attribute for the method. For example, the DLL import attribute PreserveSig is set by emitting a custom attribute.
Note |
|---|
Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later. |
The following example illustrates the use of the DefinePInvokeMethod method to create a MethodBuilder for an external unmanaged method, MessageBoxA, in the Win32 API. The example displays a message box with Retry and Cancel buttons, and displays the return value from the message box.
Important |
|---|
To get a non-zero return value, you must add MethodImplAttributes.PreserveSig to the method implementation flags after you create the MethodBuilder, by using the MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags methods. |
using System; using System.Reflection; using System.Reflection.Emit; using System.Runtime.InteropServices; namespace PInvoke { public class Example { const int MB_RETRYCANCEL = 5; static void Main() { AssemblyName myAssemblyName = new AssemblyName("TempAssembly"); // Define a dynamic assembly in the current application domain. AssemblyBuilder myAssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess.Run); // Define a dynamic module in "TempAssembly" assembly. ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule"); Type[] paramTypes = { typeof(int), typeof(string), typeof(string), typeof(int) }; // Define a PInvoke method. MethodBuilder piMethodBuilder = myModuleBuilder.DefinePInvokeMethod( "MessageBoxA", "user32.dll", MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl, CallingConventions.Standard, typeof(int), paramTypes, CallingConvention.Winapi, CharSet.Ansi); // Add PreserveSig to the method implementation flags. NOTE: If this line // is commented out, the return value will be zero when the method is // invoked. piMethodBuilder.SetImplementationFlags( piMethodBuilder.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig); // Create global methods. myModuleBuilder.CreateGlobalFunctions(); // Arguments for calling the method. Object[] arguments = { 0, "Hello World", "Title", MB_RETRYCANCEL }; MethodInfo pinvokeMethod = myModuleBuilder.GetMethod("MessageBoxA"); Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod..."); Console.WriteLine("Message box returned: {0}", pinvokeMethod.Invoke(null, arguments)); } } } /* This code example produces input similar to the following: Testing module-level PInvoke method created with DefinePInvokeMethod... Message box returned: 4 */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note