ModuleBuilder.DefinePInvokeMethod 메서드

정의

메서드를 PInvoke 정의합니다.

오버로드

Name Description
DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

PInvoke 지정된 이름, 메서드가 정의된 DLL의 이름, 메서드의 특성, 메서드의 호출 규칙, 메서드의 반환 형식, 메서드의 매개 변수 형식 및 PInvoke 플래그를 사용하여 메서드를 정의합니다.

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

PInvoke 지정된 이름, 메서드가 정의된 DLL의 이름, 메서드의 특성, 메서드의 호출 규칙, 메서드의 반환 형식, 메서드의 매개 변수 형식 및 PInvoke 플래그를 사용하여 메서드를 정의합니다.

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

PInvoke 지정된 이름, 메서드가 정의된 DLL의 이름, 메서드의 특성, 메서드의 호출 규칙, 메서드의 반환 형식, 메서드의 매개 변수 형식 및 PInvoke 플래그를 사용하여 메서드를 정의합니다.

public:
 System::Reflection::Emit::MethodBuilder ^ DefinePInvokeMethod(System::String ^ name, System::String ^ dllName, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Runtime::InteropServices::CallingConvention nativeCallConv, System::Runtime::InteropServices::CharSet nativeCharSet);
public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet);
member this.DefinePInvokeMethod : string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * System.Runtime.InteropServices.CallingConvention * System.Runtime.InteropServices.CharSet -> System.Reflection.Emit.MethodBuilder
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

매개 변수

name
String

메서드의 PInvoke 이름입니다. name 은 포함된 null을 포함할 수 없습니다.

dllName
String

메서드가 정의된 DLL의 PInvoke 이름입니다.

attributes
MethodAttributes

메서드의 특성입니다.

callingConvention
CallingConventions

메서드의 호출 규칙입니다.

returnType
Type

메서드의 반환 형식입니다.

parameterTypes
Type[]

메서드 매개 변수의 형식입니다.

nativeCallConv
CallingConvention

네이티브 호출 규칙입니다.

nativeCharSet
CharSet

메서드의 네이티브 문자 집합입니다.

반품

정의된 PInvoke 메서드입니다.

예외

메서드가 정적이지 않거나 포함하는 형식이 인터페이스인 경우

-또는-

메서드는 추상입니다.

-또는-

메서드가 이전에 정의되었습니다.

name 또는 dllName .입니다 null.

포함 형식이 이전에

예제

다음 예제에서는 DefinePInvokeMethod 메서드를 사용하여 Windows API에서 MethodBuilder 외부 관리되지 않는 메서드 MessageBoxA 만드는 방법을 보여 줍니다. 다음은 다시 시도취소 단추가 있는 메시지 상자를 표시하고 메시지 상자의 반환 값을 표시하는 예제입니다.

Important

0이 아닌 반환 값을 얻으려면 메서드 및 MethodBuilder.SetImplementationFlags 메서드를 사용하여 MethodBuilder.GetMethodImplementationFlags 메서드를 만든 MethodBuilder후 메서드 구현 플래그에 추가 MethodImplAttributes.PreserveSig 해야 합니다.

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
 */
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Namespace PInvoke

   Public Class Example
   
      Const MB_RETRYCANCEL As Integer = 5

      Shared Sub Main()
      
         Dim myAssemblyName As New AssemblyName("TempAssembly")

         ' Define a dynamic assembly in the current application domain.
         Dim myAssemblyBuilder As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                        myAssemblyName, AssemblyBuilderAccess.Run)

         ' Define a dynamic module in "TempAssembly" assembly.
         Dim myModuleBuilder As ModuleBuilder = _
            myAssemblyBuilder.DefineDynamicModule("TempModule")

         Dim paramTypes() As Type = _
            { GetType(Integer), GetType(string), GetType(string), GetType(Integer) }

         ' Define a PInvoke method.
         Dim piMethodBuilder As MethodBuilder = myModuleBuilder.DefinePInvokeMethod( _
            "MessageBoxA", _
            "user32.dll", _
            MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
            CallingConventions.Standard, _
            GetType(Integer), _
            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() Or MethodImplAttributes.PreserveSig)

         ' Create global methods.
         myModuleBuilder.CreateGlobalFunctions()

         ' Arguments for calling the method.
         Dim arguments() As Object= { 0, "Hello World", "Title", MB_RETRYCANCEL }

         Dim pinvokeMethod As MethodInfo = _
            myModuleBuilder.GetMethod("MessageBoxA")
         Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...")
         Console.WriteLine("Message box returned: {0}", _
            pinvokeMethod.Invoke(Nothing, arguments))

      End Sub
   End Class
End Namespace

' This code example produces input similar to the following:
'
'Testing module-level PInvoke method created with DefinePInvokeMethod...
'Message box returned: 4

설명

일부 DLL 가져오기 특성(System.Runtime.InteropServices.DllImportAttribute에 대한 설명 참조)은 이 메서드에 대한 인수로 지정할 수 없습니다. 이러한 특성은 메서드에 대한 사용자 지정 특성을 내보내서 설정해야 합니다. 예를 들어 DLL 가져오기 특성 PreserveSig 은 사용자 지정 특성을 내보내서 설정합니다.

적용 대상

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

PInvoke 지정된 이름, 메서드가 정의된 DLL의 이름, 메서드의 특성, 메서드의 호출 규칙, 메서드의 반환 형식, 메서드의 매개 변수 형식 및 PInvoke 플래그를 사용하여 메서드를 정의합니다.

public:
 System::Reflection::Emit::MethodBuilder ^ DefinePInvokeMethod(System::String ^ name, System::String ^ dllName, System::String ^ entryName, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Runtime::InteropServices::CallingConvention nativeCallConv, System::Runtime::InteropServices::CharSet nativeCharSet);
public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet);
member this.DefinePInvokeMethod : string * string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * System.Runtime.InteropServices.CallingConvention * System.Runtime.InteropServices.CharSet -> System.Reflection.Emit.MethodBuilder
Public Function DefinePInvokeMethod (name As String, dllName As String, entryName As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), nativeCallConv As CallingConvention, nativeCharSet As CharSet) As MethodBuilder

매개 변수

name
String

메서드의 PInvoke 이름입니다. name 은 포함된 null을 포함할 수 없습니다.

dllName
String

메서드가 정의된 DLL의 PInvoke 이름입니다.

entryName
String

DLL의 진입점 이름입니다.

attributes
MethodAttributes

메서드의 특성입니다.

callingConvention
CallingConventions

메서드의 호출 규칙입니다.

returnType
Type

메서드의 반환 형식입니다.

parameterTypes
Type[]

메서드 매개 변수의 형식입니다.

nativeCallConv
CallingConvention

네이티브 호출 규칙입니다.

nativeCharSet
CharSet

메서드의 네이티브 문자 집합입니다.

반품

정의된 PInvoke 메서드입니다.

예외

메서드가 정적이지 않거나 포함하는 형식이 인터페이스이거나 메서드가 이전에 정의된 경우의 추상 메서드인 경우입니다.

name 또는 dllName .입니다 null.

포함 형식이 이전에

예제

다음 예제에서는 DefinePInvokeMethod 메서드를 사용하여 Windows API에서 MethodBuilder 외부 관리되지 않는 메서드 MessageBoxA 만드는 방법을 보여 줍니다. 다음은 다시 시도취소 단추가 있는 메시지 상자를 표시하고 메시지 상자의 반환 값을 표시하는 예제입니다.

Important

0이 아닌 반환 값을 얻으려면 메서드 및 MethodBuilder.SetImplementationFlags 메서드를 사용하여 MethodBuilder.GetMethodImplementationFlags 메서드를 만든 MethodBuilder후 메서드 구현 플래그에 추가 MethodImplAttributes.PreserveSig 해야 합니다.

이 예제에서는 메서드의 다른 오버로드를 DefinePInvokeMethod 사용하지만 기술은 동일합니다.

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
 */
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Namespace PInvoke

   Public Class Example
   
      Const MB_RETRYCANCEL As Integer = 5

      Shared Sub Main()
      
         Dim myAssemblyName As New AssemblyName("TempAssembly")

         ' Define a dynamic assembly in the current application domain.
         Dim myAssemblyBuilder As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                        myAssemblyName, AssemblyBuilderAccess.Run)

         ' Define a dynamic module in "TempAssembly" assembly.
         Dim myModuleBuilder As ModuleBuilder = _
            myAssemblyBuilder.DefineDynamicModule("TempModule")

         Dim paramTypes() As Type = _
            { GetType(Integer), GetType(string), GetType(string), GetType(Integer) }

         ' Define a PInvoke method.
         Dim piMethodBuilder As MethodBuilder = myModuleBuilder.DefinePInvokeMethod( _
            "MessageBoxA", _
            "user32.dll", _
            MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
            CallingConventions.Standard, _
            GetType(Integer), _
            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() Or MethodImplAttributes.PreserveSig)

         ' Create global methods.
         myModuleBuilder.CreateGlobalFunctions()

         ' Arguments for calling the method.
         Dim arguments() As Object= { 0, "Hello World", "Title", MB_RETRYCANCEL }

         Dim pinvokeMethod As MethodInfo = _
            myModuleBuilder.GetMethod("MessageBoxA")
         Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...")
         Console.WriteLine("Message box returned: {0}", _
            pinvokeMethod.Invoke(Nothing, arguments))

      End Sub
   End Class
End Namespace

' This code example produces input similar to the following:
'
'Testing module-level PInvoke method created with DefinePInvokeMethod...
'Message box returned: 4

설명

일부 DLL 가져오기 특성(설명 참조)은 이 메서드에 DllImportAttribute대한 인수로 지정할 수 없습니다. 이러한 특성은 메서드에 대한 사용자 지정 특성을 내보내서 설정해야 합니다. 예를 들어 DLL 가져오기 특성 PreserveSig 은 사용자 지정 특성을 내보내서 설정합니다.

적용 대상