平台pinvoke教程msdn [英] platform pinvoke tutorial msdn

查看:147
本文介绍了平台pinvoke教程msdn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是从 MSDN <教程/ A>。 _flushall的输出中是在教程测试,但我得到了2用console.write显示输出()。 ?有人能解释请

 使用系统;使用System.Runtime.InteropServices 
;

类PlatformInvokeTest
{
函数[DllImport(MSVCRT.DLL)]
公共静态外部INT看跌期权(串c);
函数[DllImport(MSVCRT.DLL)]
内部静态外部INT _flushall();

公共静态无效的主要()
{
看跌期权(测试);
_flushall();
}
}


这代码不能在现代版本的Windows不再工作。在MSVCRT.DLL版本,你得到的是对于已经另有undiagnosable方面进行了修修补补用,有可能是与安全的Windows可执行文件的私人CRT实现。



你需要找到另外一个是仍然友好。你有你的机器上的一个礼物,如果你的Visual Studio 2010或更高版本。在c一看,见:\windows\syswow64目录下,寻找msvcrxxx.dll其中xxx为100,110或120更改相应的声明。在我的机器上安装了VS2013:

 函数[DllImport(msvcr120.dll)] 
公共静态外部INT看跌期权(串c);
函数[DllImport(msvcr120.dll)]
内部静态外部INT _flushall();



输出:




测试



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

解决方案

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

这篇关于平台pinvoke教程msdn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆