次の方法で共有


TypeBuilder.DefinePInvokeMethod メソッド (String, String, MethodAttributes, CallingConventions, Type, Type , CallingConvention, CharSet)

メソッド名、メソッドが定義される DLL の名前、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメータの型、および PInvoke フラグを指定して、 PInvoke メソッドを定義します。

Overloads Public Function DefinePInvokeMethod( _
   ByVal name As String, _   ByVal dllName As String, _   ByVal attributes As MethodAttributes, _   ByVal callingConvention As CallingConventions, _   ByVal returnType As Type, _   ByVal parameterTypes() As Type, _   ByVal nativeCallConv As CallingConvention, _   ByVal nativeCharSet As CharSet _) As MethodBuilder
[C#]
public MethodBuilder DefinePInvokeMethod(stringname,stringdllName,MethodAttributesattributes,CallingConventionscallingConvention,TypereturnType,Type[] parameterTypes,CallingConventionnativeCallConv,CharSetnativeCharSet);
[C++]
public: MethodBuilder* DefinePInvokeMethod(String* name,String* dllName,MethodAttributesattributes,CallingConventionscallingConvention,Type* returnType,Type* parameterTypes[],CallingConventionnativeCallConv,CharSetnativeCharSet);
[JScript]
public function DefinePInvokeMethod(
   name : String,dllName : String,attributes : MethodAttributes,callingConvention : CallingConventions,returnType : Type,parameterTypes : Type[],nativeCallConv : CallingConvention,nativeCharSet : CharSet) : MethodBuilder;

パラメータ

  • name
    PInvoke メソッドの名前。name に null を埋め込むことはできません。
  • dllName
    PInvoke メソッドが定義されている DLL の名前。
  • attributes
    メソッドの属性。
  • callingConvention
    メソッドの呼び出し規約。
  • returnType
    メソッドの戻り値の型。
  • parameterTypes
    メソッドのパラメータの型。
  • nativeCallConv
    ネイティブな呼び出し規約。
  • nativeCharSet
    メソッドのネイティブな文字セット。

戻り値

定義された PInvoke メソッド。

例外

例外の種類 条件
ArgumentException このメソッドは静的ではありません。

または

親の型がインターフェイスです。

または

このメソッドは抽象メソッドです。

または

メソッドは既に定義されています。

または

name または dllName の長さが 0 です。

ArgumentNullException name または dllName が null 参照 (Visual Basic では Nothing) です。
InvalidOperationException 格納している型が、 CreateType を使用して既に作成されています。

解説

DLL インポート属性 (System.Runtime.InteropServices.DllImportAttribute の説明を参照) の中には、このメソッドの引数として指定できないものがあります。このような属性は、メソッドに対してカスタム属性を生成することによって設定する必要があります。たとえば、DLL インポート属性 PreserveSig は、カスタム属性を生成することで設定します。

使用例

[Visual Basic, C#, C++] 次のコード例は、 DefinePInvokeMethod を使用して、動的な型の中に、指定した外部メソッド シグネチャへの PInvoke 呼び出しを作成する方法を示しています。

 
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName
Dim myMethodBuilder As MethodBuilder = Nothing

' 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")
Dim myType As Type() = _ 
          {GetType(Integer), GetType(String), GetType(String), GetType(Integer)}

' Define a PInvoke method.
myMethodBuilder = _ 
                myModuleBuilder.DefinePInvokeMethod("MessageBoxA", "user32.dll", _ 
                MethodAttributes.Public Or MethodAttributes.Static Or _
                MethodAttributes.PinvokeImpl, CallingConventions.Standard, Nothing, _ 
                myType, CallingConvention.Winapi, CharSet.Ansi)

myModuleBuilder.CreateGlobalFunctions()

[C#] 
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();

[C++] 
AppDomain* currentDomain;
AssemblyName* myAssemblyName;
MethodBuilder* myMethodBuilder=0;

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

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Run);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule(S"TempModule");
Type* myType[] = {__typeof(int),__typeof(String),__typeof(String),__typeof(int)};

// Define a PInvoke method.
myMethodBuilder = myModuleBuilder->DefinePInvokeMethod(
   S"MessageBoxA",
   S"user32.dll",
   static_cast<MethodAttributes>(MethodAttributes::Public
                                |MethodAttributes::Static
                                |MethodAttributes::PinvokeImpl),
   CallingConventions::Standard,
   0,
   myType,CallingConvention::Winapi,
   CharSet::Ansi);

myModuleBuilder->CreateGlobalFunctions();

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

TypeBuilder クラス | TypeBuilder メンバ | System.Reflection.Emit 名前空間 | TypeBuilder.DefinePInvokeMethod オーバーロードの一覧