本文整理汇总了C#中System.Reflection.Emit.ModuleBuilder.DefinePInvokeMethod方法的典型用法代码示例。如果您正苦于以下问题:C# ModuleBuilder.DefinePInvokeMethod方法的具体用法?C# ModuleBuilder.DefinePInvokeMethod怎么用?C# ModuleBuilder.DefinePInvokeMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.ModuleBuilder
的用法示例。
在下文中一共展示了ModuleBuilder.DefinePInvokeMethod方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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
*/