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就是重命名的函数名。
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!
浙公网安备 33010602011771号