ModuleBuilder.DefinePInvokeMethod 메서드

정의

PInvoke 메서드를 정의합니다.

오버로드

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

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

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

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

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

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

이름, 메서드가 정의되어 있는 DLL의 이름, 해당 메서드의 특성, 호출 규칙, 반환 형식, 매개 변수 형식 및 PInvoke 플래그를 지정하여 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);
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

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

attributes
MethodAttributes

메서드의 특성입니다.

callingConvention
CallingConventions

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

returnType
Type

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

parameterTypes
Type[]

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

nativeCallConv
CallingConvention

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

nativeCharSet
CharSet

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

반환

정의된 PInvoke 메서드입니다.

예외

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

또는

abstract 메서드입니다.

또는

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

name 또는 dllNamenull인 경우

포함하는 형식이 CreateType()을 사용하여 이미 만들어져 있는 경우

예제

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

중요

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

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

const int MB_RETRYCANCEL = 5;

void main()
{
   AssemblyName^ myAssemblyName = gcnew 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");

   array<Type^>^ paramTypes = 
      { int::typeid, String::typeid, String::typeid, int::typeid };

   // Define a PInvoke method.
   MethodBuilder^ piMethodBuilder = myModuleBuilder->DefinePInvokeMethod(
      "MessageBoxA",
      "user32.dll",
      MethodAttributes::Public | MethodAttributes::Static | MethodAttributes::PinvokeImpl,
      CallingConventions::Standard,
      int::typeid,
      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.
   array<Object^>^ arguments = 
      { (Object^)(int) 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(nullptr, arguments));
};


/* This code example produces input similar to the following:

Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
 */
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 은 사용자 지정 특성을 내보내서 설정됩니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 멤버는 ReflectionPermission 더 이상 플래그가 ReflectionPermissionFlag.ReflectionEmit 필요하지 않습니다. (리플렉션 내보내기의 보안 문제를 참조하세요.) 이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상

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

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

이름, 메서드가 정의되어 있는 DLL의 이름, 해당 메서드의 특성, 호출 규칙, 반환 형식, 매개 변수 형식 및 PInvoke 플래그를 지정하여 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);
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

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

entryName
String

DLL의 진입점 이름입니다.

attributes
MethodAttributes

메서드의 특성입니다.

callingConvention
CallingConventions

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

returnType
Type

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

parameterTypes
Type[]

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

nativeCallConv
CallingConvention

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

nativeCharSet
CharSet

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

반환

정의된 PInvoke 메서드입니다.

예외

비정적 메서드이거나, 포함하는 형식이 인터페이스인 경우, 추상 메서드인 경우 또는 이미 정의되어 있는 경우

name 또는 dllNamenull인 경우

포함하는 형식이 CreateType()을 사용하여 이미 만들어져 있는 경우

예제

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

중요

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

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

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

const int MB_RETRYCANCEL = 5;

void main()
{
   AssemblyName^ myAssemblyName = gcnew 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");

   array<Type^>^ paramTypes = 
      { int::typeid, String::typeid, String::typeid, int::typeid };

   // Define a PInvoke method.
   MethodBuilder^ piMethodBuilder = myModuleBuilder->DefinePInvokeMethod(
      "MessageBoxA",
      "user32.dll",
      MethodAttributes::Public | MethodAttributes::Static | MethodAttributes::PinvokeImpl,
      CallingConventions::Standard,
      int::typeid,
      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.
   array<Object^>^ arguments = 
      { (Object^)(int) 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(nullptr, arguments));
};


/* This code example produces input similar to the following:

Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
 */
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 은 사용자 지정 특성을 내보내서 설정됩니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 멤버는 ReflectionPermission 더 이상 플래그가 ReflectionPermissionFlag.ReflectionEmit 필요하지 않습니다. (리플렉션 내보내기의 보안 문제를 참조하세요.) 이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상