Export (0) Print
Expand All

ModuleBuilder.DefinePInvokeMethod Method (String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

Defines a PInvoke method given its 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,
	string entryName,
	MethodAttributes attributes,
	CallingConventions callingConvention,
	Type returnType,
	Type[] parameterTypes,
	CallingConvention nativeCallConv,
	CharSet nativeCharSet
)
public MethodBuilder DefinePInvokeMethod (
	String name, 
	String dllName, 
	String entryName, 
	MethodAttributes attributes, 
	CallingConventions callingConvention, 
	Type returnType, 
	Type[] parameterTypes, 
	CallingConvention nativeCallConv, 
	CharSet nativeCharSet
)
public function DefinePInvokeMethod (
	name : String, 
	dllName : String, 
	entryName : String, 
	attributes : MethodAttributes, 
	callingConvention : CallingConventions, 
	returnType : Type, 
	parameterTypes : Type[], 
	nativeCallConv : CallingConvention, 
	nativeCharSet : CharSet
) : MethodBuilder

Parameters

name

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

dllName

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

entryName

The name of the entry point in the DLL.

attributes

The attributes of the method.

callingConvention

The method's calling convention.

returnType

The method's return type.

parameterTypes

The types of the method's parameters.

nativeCallConv

The native calling convention.

nativeCharSet

The method's native character set.

Return Value

The defined PInvoke method.

Exception typeCondition

ArgumentException

The method is not static or if the containing type is an interface or if the method is abstract of if the method was previously defined.

ArgumentNullException

name or dllName is a null reference (Nothing in Visual Basic).

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.

The following code sample illustrates the use of DefinePInvokeMethod to implement a MethodBuilder with the same signature as an external unmanaged method for platform invocation.

AppDomain currentDomain;
AssemblyName myAssemblyName;
MethodBuilder myMethodBuilder=null;

// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain.DefineDynamicAssembly
               (myAssemblyName, AssemblyBuilderAccess.Run);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
Type[] myType = {typeof(int),typeof(string),typeof(string),typeof(int)};

// Define a PInvoke method.
myMethodBuilder = myModuleBuilder.DefinePInvokeMethod("MessageBoxA",
   "user32.dll",
   MethodAttributes.Public|MethodAttributes.Static|MethodAttributes.PinvokeImpl,
   CallingConventions.Standard,null,myType,CallingConvention.Winapi,
   CharSet.Ansi);

myModuleBuilder.CreateGlobalFunctions();

AppDomain currentDomain;
AssemblyName myAssemblyName;
MethodBuilder myMethodBuilder = null;

// Get the current application domain for the current thread.
currentDomain = AppDomain.get_CurrentDomain();

myAssemblyName = new AssemblyName();
myAssemblyName.set_Name("TempAssembly");

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName,
    AssemblyBuilderAccess.Run);

// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
Type myType[] =  { int.class.ToType(), String.class.ToType(), 
    String.class.ToType(), int.class.ToType() };

// Define a PInvoke method.
myMethodBuilder = myModuleBuilder.DefinePInvokeMethod("MessageBoxA", 
    "user32.dll", MethodAttributes.Public | MethodAttributes.Static 
    | MethodAttributes.PinvokeImpl, CallingConventions.Standard, null, 
    myType, CallingConvention.Winapi, CharSet.Ansi);

myModuleBuilder.CreateGlobalFunctions();

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft