简体   繁体   English

在 C# 中使用 C++ DLL

[英]Using a c++ dll in c#

If I have some c# code like this如果我有这样的 C# 代码

   public const string CppFunctionsDLL = @"..\..\..\Release\CplusplusCode.dll";

    [DllImport(CppFunctionsDLL, CallingConvention = CallingConvention.Cdecl)]
    static extern int GetData(out IntPtr data, byte[] dataFromCard);

After building the solution..then running the executable on a different machine.. when GetData gets called does the actual c++ dll have to be present on the machine the executable is running on?构建解决方案后..然后在另一台机器上运行可执行文件.. 当调用GetData时,实际的 c++ dll 是否必须存在于运行可执行文件的机器上? or was everything loaded in when the build of the solution was run..?还是在运行解决方案构建时加载了所有内容..?

Essentially.. how do I ensure that when the executable is run on a users machine.. that the c++ dll is present and that I can reference it in code?本质上.. 我如何确保当可执行文件在用户机器上运行时.. c++ dll 存在并且我可以在代码中引用它?

when GetData gets called does the actual c++ dll have to be present on the machine the executable is running on?当调用 GetData 时,实际的 c++ dll 是否必须存在于运行可执行文件的机器上?

Yes.是的。 DllImport works internally by using LoadLibrary() and GetProcAddress() at runtime. DllImport通过在运行时使用LoadLibrary()GetProcAddress()在内部工作。 When the C# function is called, the referenced DLL is loaded, and then the referenced DLL function is called, marshaling parameter data back and forth as needed.当调用 C# 函数时,加载引用的 DLL,然后调用引用的 DLL 函数,根据需要来回封送参数数据。

or was everything loaded in when the build of the solution was run..?还是在运行解决方案构建时加载了所有内容..?

No.不。

Essentially.. how do I ensure that when the executable is run on a users machine.. that the c++ dll is present and that I can reference it in code?本质上.. 我如何确保当可执行文件在用户机器上运行时.. c++ dll 存在并且我可以在代码中引用它?

You must distribute and install the DLL alongside the built EXE.您必须将 DLL 与构建的 EXE 一起分发和安装。 How you do that exactly is up to you.你如何做到这一点完全取决于你。 There are many ways to bundle up and distribute multi-file applications.有许多方法可以捆绑和分发多文件应用程序。 For example, via a Windows Installer MSI package.例如,通过Windows Installer MSI 包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM