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


Type: System.String

The name of the PInvoke method. name cannot contain embedded nulls.

Type: System.String

The name of the DLL in which the PInvoke method is defined.

Type: System.Reflection.MethodAttributes

The attributes of the method.

Type: System.Reflection.CallingConventions

The method's calling convention.

Type: System.Type

The method's return type.

Type: System.Type[]

The types of the method's parameters.

Type: System.Runtime.InteropServices.CallingConvention

The native calling convention.

Type: System.Runtime.InteropServices.CharSet

The method's native character set.

Return Value

Type: System.Reflection.Emit.MethodBuilder

The defined PInvoke method.

Exception Condition

The method is not static or if the containing type is an interface.


The method is abstract.


The method was previously defined.


name or dllName is null.


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.


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.


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 = 
                        myAssemblyName, AssemblyBuilderAccess.Run);

         // Define a dynamic module in "TempAssembly" assembly.
         ModuleBuilder myModuleBuilder = 

         Type[] paramTypes = { typeof(int), typeof(string), typeof(string), typeof(int) };

         // Define a PInvoke method.
         MethodBuilder piMethodBuilder = myModuleBuilder.DefinePInvokeMethod(
            MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,

         // 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.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);

         // Create global methods.

         // 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

.NET Framework
Available since 1.1
Available since 2.0
Return to top