1、使用微软的C运行库msvcrt.dll提供的方法和Win32 API提供的函数进行平台调用
代码如下:
class Program { //微软的C运行库msvcrt.dll提供的puts方法 [DllImport("msvcrt.dll")] static extern int puts(string msg); [DllImport("msvcrt.dll")] static extern int _flushall(); //Win32 API提供的函数进行平台调用 [DllImport("user32.dll", EntryPoint = "MessageBox")] public static extern int MessageBox(int hwnd, string lpText, string lpCaption, int wType); static void Main(string[] args) { puts("hello world test!"); _flushall(); MessageBox(0, "Hello world Test!", "Title is here", 0); } }
2、工具的使用。要查看DLL中包含的函数信息,可以用的工具有:Depends.exe、dumpbin.exe、PE Explorer等
使用dumpbin的形式如下:
3、使用EntryPoint字段对函数进行重命名
[DllImport("NativeLib.dll",EntryPoint="PrintMsg")] public static extern void PrintMsgRename(string msg);
PrintMsg必须与非托管的函数名一致,PrintMsgRename就是重命名的函数名。