平台pinvoke教程msdn

以下是msdn的教程。 _flushall的输出在教程中是“Test”,但是通过使用console.write()显示输出我得到了“2”。 有人可以解释一下吗?

using System; using System.Runtime.InteropServices; 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(); } } 

该代码在现代Windows版本上不再起作用。 您获得的“msvcrt.dll”版本是针对Windows可执行文件的私有CRT实现,它以其他方式不可识别的方式进行修改,可能与安全性有关。

你需要找到另一个仍然友好的。 如果安装了Visual Studio 2010或更高版本,则计算机上将有一个存在。 看看c:\ windows \ syswow64目录并查找msvcrxxx.dll,其中xxx是100,110或120.相应地更改声明。 在我的机器上,安装了VS2013:

 [DllImport("msvcr120.dll")] public static extern int puts(string c); [DllImport("msvcr120.dll")] internal static extern int _flushall(); 

输出:

测试