c# – 通过PInvoke“Hello World”

c# – 通过PInvoke“Hello World”,第1张

概述我正在尝试在C#中创建一些需要调用一些非托管DLL的东西,这个过程对我一无所知!我发现 “Hello World” tutorial应该像从底部复制和粘贴几行代码一样简单: using System;using System.Runtime.InteropServices;namespace PInvokeTest{ class Program { [Dll 我正在尝试在C#中创建一些需要调用一些非托管DLL的东西,这个过程对我一无所知!我发现 “Hello World” tutorial应该像从底部复制和粘贴几行代码一样简单:
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版本控制很重要).

@H_403_8@解决方法 你正在使用的msvcrt *版本可能就是问题所在.如果我使用未修改的代码创建一个控制台应用程序,我会得到相同的结果 – 没有输出.

如果我将引用的dll从msvcrt40.dll更改为msvcr120.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中发生了变化,这是由于这造成了混乱和脆弱的依赖链.有关这些更改的更多信息,请访问:

The Great CRT Refactoring

Introducing the Universal CRT

@H_403_8@ @H_403_8@ 总结

以上是内存溢出为你收集整理的c# – 通过PInvoke“Hello World”全部内容,希望文章能够帮你解决c# – 通过PInvoke“Hello World”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1249509.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存