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

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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)

member DefinePInvokeMethod : 
        name:string *
        dllName:string *
        attributes:MethodAttributes *
        callingConvention:CallingConventions *
        returnType:Type *
        parameterTypes:Type[] *
        nativeCallConv:CallingConvention *
        nativeCharSet:CharSet -> MethodBuilder

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.

Return Value

Type: System.Reflection.Emit.MethodBuilder

The defined PInvoke method.

Exception Condition
ArgumentException

The method is not static.

-or-

The parent type is an interface.

-or-

The method is abstract.

-or-

The method was previously defined.

-or-

The length of name or dllName is zero.

ArgumentNullException

name or dllName is null.

InvalidOperationException

The containing type has been previously created using CreateType.

Some DLL import attributes (see the description of DllImportAttribute) cannot be specified as arguments to this method. For example, the DLL import attribute MethodImplAttributes.PreserveSig must be added after the PInvoke method is created, if the method returns a value. The example shows how to do this.

The following example demonstrates how to use the DefinePInvokeMethod method to create a PInvoke method, and how to add the MethodImplAttributes.PreserveSig flag to the method implementation flags after you create the MethodBuilder, by using the MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags methods.

System_CAPS_importantImportant

To get a non-zero return value, you must add the MethodImplAttributes.PreserveSig flag.

The example creates a dynamic assembly with one dynamic module and a single type, MyType, that contains the PInvoke method. The PInvoke method represents the Win32 GetTickCount function.

When the example is run, it executes the PInvoke method. It also saves the dynamic assembly as PInvokeTest.dll. You can use the Ildasm.exe (IL Disassembler) to examine the MyType class and the static (Shared in Visual Basic) PInvoke method it contains. You can compile a Visual Basic or C# program that uses the static MyType.GetTickCount method by including a reference to the DLL when you run csc.exe or vbc.exe; for example, /r:PInvokeTest.dll.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show: