通过PInvoke的“ Hello World”

Benjin

我正在尝试使用C#进行某些操作,这些操作要求调用一些非托管DLL,而我对此一无所知!我找到了一个“ Hello World”教程,它应该就像从底部复制并粘贴几行代码一样简单:

using System;
using System.Runtime.InteropServices;

namespace PInvokeTest
{
    class Program
    {
        [DllImport("msvcrt40.dll")]
        public static extern int printf(string format, __arglist);

        public static void Main()
        {
            printf("Hello %s!\n", __arglist("World"));
            Console.ReadKey();
        }
    }
}

它将编译并运行完成,没有任何错误,但是到达时将不打印任何内容ReadKey()

我错过了一些重要的设置步骤吗?该项目是为.NET 4.6.1构建的(如果对DLL版本控制很重要)。

大卫·坦西

msvcrt*您使用的版本可能是问题所在。如果我使用未经修改的代码创建控制台应用程序,则会得到相同的结果-无输出。

如果将引用的dll从更改为msvcrt40.dllmsvcr120.dll则可以看到预期的输出。

[DllImport("msvcr120.dll")]
public static extern int printf(string format, __arglist);

public static void Main()
{
    printf("Hello %s!\n", __arglist("World"));
    Console.ReadKey();
}

附加信息

msvcrt*跟踪Visual Studio版本的各种编号版本

  • MSVCRT70.DLL Visual Studio .NET
  • MSVCRT71.DLL Visual Studio 2003
  • MSVCRT80.DLL Visual Studio 2005
  • MSVCRT90.DLL Visual Studio 2008年
  • MSVCRT100.DLL Visual Studio 2010
  • MSVCRT110.DLL Visual Studio 2012
  • MSVCRT120.DLL Visual Studio 2013

由于VS2015中创建的混乱和脆弱的依赖链,此版本编号方法已在VS2015中进行了更改。有关这些更改的更多信息,可以在这里找到:

出色的CRT重构

通用CRT简介

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章