Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TypeBuilder.DefinePInvokeMethod Method (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)
'Declaration
Public Function DefinePInvokeMethod ( _
	name As String, _
	dllName As String, _
	attributes As MethodAttributes, _
	callingConvention As CallingConventions, _
	returnType As Type, _
	parameterTypes As Type(), _
	nativeCallConv As CallingConvention, _
	nativeCharSet As CharSet _
) As 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.
ExceptionCondition
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 Nothing.

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.

Important noteImportant

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.

Imports System
Imports System.Text
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Public Class Example

    Public Shared Sub Main() 

        ' Create the AssemblyBuilder. 
        Dim asmName As New AssemblyName("PInvokeTest")
        Dim dynamicAsm As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, _
                AssemblyBuilderAccess.RunAndSave)

        ' Create the module. 
        Dim dynamicMod As ModuleBuilder = _
            dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name & ".dll")

        ' Create the TypeBuilder for the class that will contain the  
        ' signature for the PInvoke call. 
        Dim tb As TypeBuilder = dynamicMod.DefineType("MyType", _
            TypeAttributes.Public Or TypeAttributes.UnicodeClass)

        Dim mb As MethodBuilder = tb.DefinePInvokeMethod( _
            "GetTickCount", _
            "Kernel32.dll", _
            MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
            CallingConventions.Standard, _
            GetType(Integer), _
            Type.EmptyTypes, _
            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.
        mb.SetImplementationFlags( _
            mb.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)

        ' The PInvoke method does not have a method body. 

        ' Create the class and test the method. 
        Dim t As Type = tb.CreateType()

        Dim mi As MethodInfo = t.GetMethod("GetTickCount")
        Console.WriteLine("Testing PInvoke method...")
        Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(Nothing, Nothing))

        ' Produce the .dll file.
        Console.WriteLine("Saving: " & asmName.Name & ".dll")
        dynamicAsm.Save(asmName.Name & ".dll")

    End Sub   
End Class  

' This example produces output similar to the following: 

'Testing PInvoke method... 
'GetTickCount returned: 1313078714 
'Saving: PInvokeTest.dll

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.