2014-02-27 39 views
4

以下是來自msdn的教程。 _flushall的輸出是教程中的「測試」,但是我通過使用console.write()顯示輸出來獲得「2」。請有人解釋一下嗎?平臺pinvoke教程msdn

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(); 
    } 
} 
+0

從[MSDN documentatation](http://msdn.microsoft.com/en-us/library /s9xk9ehd.aspx)爲_flushAll,該方法返回打開流的數量。另外,_flushAll的返回類型是int。所以,它不能是「測試」。但是,上述程序的輸出是「測試」,因爲這是通過調用puts方法打印的。 – sthotakura

+0

確實返回類型是int。我如何獲得「測試」的價值呢? – arjun

+0

如果我正確理解您的評論,則通過調用put函數將「Test」打印到控制檯。 – sthotakura

回答

6

該代碼在現代Windows版本上無法使用。你得到的「msvcrt.dll」版本是Windows可執行文件的私人CRT實現,它以其他不可知的方式進行修改,可能與安全性有關。

你需要找到另一個仍然友好的。如果安裝了Visual Studio 2010或更高版本,您的計算機上將顯示一個。在c:\ windows \ syswow64目錄中查看並查找其中xxx爲100,110或120的msvcrxxx.dll。相應地更改聲明。在我的機器,用VS2013安裝:

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

輸出:

測試