platform pinvoke tutorial msdn

痴心易碎 提交于 2019-12-23 12:47:18

问题


The following is a tutorial from msdn. The ouput of _flushall is "Test" in the tutorial but I got "2" by displaying output using console.write(). Can somebody explain please?

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

回答1:


That code doesn't work anymore on modern Windows versions. The "msvcrt.dll" version you get is a private CRT implementation for Windows executables that has been tinkered with in otherwise undiagnosable ways, probably having something to do with security.

You'll need to find another one that is still friendly. You'll have one present on your machine if you have Visual Studio 2010 or later installed. Have a look-see in the c:\windows\syswow64 directory and look for msvcrxxx.dll where xxx is 100, 110 or 120. Change the declaration accordingly. On my machine, with VS2013 installed:

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

Output:

Test



来源:https://stackoverflow.com/questions/22071964/platform-pinvoke-tutorial-msdn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!