TypeBuilder.DefinePInvokeMethod メソッド
PInvoke メソッドを定義します。
オーバーロードの一覧
メソッド名、メソッドが定義される DLL の名前、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメータの型、および PInvoke フラグを指定して、 PInvoke メソッドを定義します。
メソッド名、メソッドが定義される DLL の名前、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメータの型、および PInvoke フラグを指定して、 PInvoke メソッドを定義します。
使用例
[Visual Basic, C#, C++] 次のコード例は、 DefinePInvokeMethod を使用して、動的な型の中に、指定した外部メソッド シグネチャへの PInvoke 呼び出しを作成する方法を示しています。
[Visual Basic, C#, C++] メモ ここでは、DefinePInvokeMethod のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
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 giving the name of the entrypoint in the dll.
myMethodBuilder = _
myModuleBuilder.DefinePInvokeMethod("MsgBox", "user32.dll", "MessageBox", _
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 giving the name of the entrypoint in the dll.
myMethodBuilder = myModuleBuilder.DefinePInvokeMethod("MsgBox",
"user32.dll", "MessageBox",
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 giving the name of the entrypoint in the dll.
myMethodBuilder = myModuleBuilder->DefinePInvokeMethod(
S"MsgBox",
S"user32.dll",
S"MessageBox",
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
参照
TypeBuilder クラス | TypeBuilder メンバ | System.Reflection.Emit 名前空間