如何直接调用从DLL导出的本机函数?
答
这里是在行动DllImport
属性的一个简单的例子:
using System.Runtime.InteropServices;
class C
{
[DllImport("user32.dll")]
public static extern int MessageBoxA(int h, string m, string c, int type);
public static int Main()
{
return MessageBoxA(0, "Hello World!", "Caption", 0);
}
}
此示例显示声明在本机中实现的C#方法的最低要求e DLL。方法C.MessageBoxA()
使用静态和外部修饰符声明,并具有DllImport
属性,该属性告知编译器该实现来自user32.dll,使用默认名称MessageBoxA。
答
class PlatformInvokeTest
{
[DllImport("msvcrt.dll")]
public static extern int puts(string c);
[DllImport("msvcrt.dll")]
internal static extern int _flushall();
public static void Main()
{
puts("Test");
_flushall();
}
}
如果您需要从本地DLL生成C#的DllImport声明,看这个帖子:Generate C# DLLImport declarations from a native dll
答
取决于你想要什么......我在我的代码是这样的,但这使用Win32 API的DLL
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
然后就叫
GetForegroundWindow()
好像在类里定义的一样
你试过谷歌吗? – 2012-04-12 11:09:58