简体   繁体   English

使用Visual Studio 2013将C / C ++ DLL生成并交付到C#应用程序中

[英]Build and deliver a C/C++ DLL into a C# app using Visual Studio 2013

I am a Visual Studio 2013 Newbie who is trying to figure out how to build a C DLL in Visual Studio and then invoke the functions exported by the C lib from within a C# app. 我是一个Visual Studio 2013新手,他试图弄清楚如何在Visual Studio中构建C DLL,然后从C#应用程序中调用C库导出的函数。 I've got everything building, but I cannot figure out the proper way of getting the DLL included with the app build so that it will actually load properly when using [DLLImport()] . 我已经建立了所有东西,但是我无法弄清楚将DLL包含在应用程序构建中的正确方法,以便在使用[DLLImport()]时可以正确地加载它。 I currently get a System.DllNotFoundException when attempting to invoke a function from the library from within my C# class. 当前,当尝试从我的C#类中的库中调用函数时,我得到了System.DllNotFoundException

I have setup a VS 2013 solution which contains the following projects: 我已经安装了VS 2013解决方案,其中包含以下项目:

HelloWorldLib.Shared : Shared project that contains the .cpp and .h files for the DLL HelloWorldLib.Shared:包含DLL的.cpp和.h文件的共享项目

HelloWorldLib.Windows : Win 8.1 C++ project that references the shared project and builds the DLL for Win 8.1 HelloWorldLib.Windows:引用共享项目并为Win 8.1构建DLL的Win 8.1 C ++项目

MyApp-Win.Windows : C# project for Win 8.1 that wants to make use of the HelloWorldLib.Windows.dll produced by the HelloWorldLib.windows build MyApp-Win.Windows:Win 8.1的C#项目,要使用HelloWorldLib.windows构建生成的HelloWorldLib.Windows.dll

I have been looking over many SO questions and answers. 我一直在寻找许多SO问题和答案。 For instance, I think my question is similar to this one, which doesn't appear to have been answered in any useful way: Interop, Native DLL and Visual Studio build process 例如,我认为我的问题与此问题类似,似乎没有以任何有用的方式回答: Interop,Native DLL和Visual Studio构建过程

Some of the other answers suggested adding a file reference to the DLL, but how would I do that and keep the proper Debug/Release versions? 其他一些答案建议添加对DLL的文件引用,但是我该怎么做并保留正确的Debug / Release版本? Given that all of these projects are being built within the same solution, and there are build dependencies between them, it feels like there should be a straightforward way to set this up. 鉴于所有这些项目都是在同一个解决方案中构建的,并且它们之间存在构建依赖关系,因此感觉应该有一种直接的方法来进行设置。

BTW, I am using the shared project setups, as my ultimate goal is to figure out how to get this all working with Xamarin to target iOS, Win, Win Phone, Mac and Android. 顺便说一句,我正在使用共享项目设置,因为我的最终目标是弄清楚如何与Xamarin一起使用以针对iOS,Win,Win Phone,Mac和Android。 So that we can use a common C/C++ layer for our high performance requirement code, and the reference it from the UI layers on the various platforms. 这样我们就可以将通用的C / C ++层用于高性能需求代码,并从各种平台上的UI层进行引用。

This is a pretty straight-forward problem, easy to identify if you look where the built files end up. 这是一个非常简单的问题,如果您看一下生成文件的最终位置,则很容易识别。 The C++ build system has a different strategy than the managed build system. C ++构建系统与托管构建系统的策略不同。 It by default writes the build output to the solution directory's Debug directory. 默认情况下,它将生成输出写入解决方案目录的Debug目录。 Your C# project however builds to its bin\\Debug directory and requires all dependent DLLs to be present there. 但是,您的C#项目将构建到其bin \\ Debug目录,并且需要所有相关的DLL都存在于此。

So, by default, your C# program can never find the DLL, it is in the wrong directory and Windows doesn't look there. 因此,默认情况下,您的C#程序永远找不到DLL,它位于错误的目录中,而Windows不在该目录中。 Nor is MSBuild smart enough to discover this by itself, like it can for managed DLLs, it cannot see that you program depends on the C++ DLL. MSBuild也不够聪明,无法单独发现它,就像托管DLL一样,无法看到您的程序依赖于C ++ DLL。

Two basic ways to work around this: 解决此问题的两种基本方法:

  • Change the C++ project's Output Directory setting 更改C ++项目的输出目录设置
  • Write a post-build event for your C# project that uses xcopy.exe to copy the DLL. 为您的C#项目编写一个使用xcopy.exe复制DLL的生成后事件。

Unfortunately things aren't that easy, at least not yet. 不幸的是,事情并不是那么容易,至少现在还不是。 I was going to have a meeting with Microsoft C++ team but that got postponed, similar functionality you are after was one of the things on my wishlist (building managed C++/CLI wrappers for Xamarin.iOS and Android). 我原本打算与Microsoft C ++团队开会,但是被推迟了,您追求的类似功能只是我的愿望清单之一(为Xamarin.iOS和Android构建托管的C ++ / CLI包装器)。

Currently, if you can, the easiest option is to wrap C++ code inside a DLL that exposes C interface. 当前,如果可以的话,最简单的选择是将C ++代码包装在公开C接口的DLL中。 Then build it with XCode and Android NDK. 然后使用XCode和Android NDK进行构建。 If you can't then the next option is to create Objective-C binding project that wraps the C++ code inside Objective C classes for iOS. 如果不能,那么下一个选择是创建Objective-C绑定项目,该项目将C ++代码包装在iOS的Objective C类中。 For Android either create Java wrapper or use SWIG . 对于Android,要么创建Java包装器,要么使用SWIG Both of these options are cumbersome which is why C API should be the first choice you investigate. 这两个选项都很麻烦,这就是为什么C API应该成为您研究的首选。

I know it's not the answer you were hoping for but reusing C++ code is way more complicated than reusing C# or even C. 我知道这不是您想要的答案,但是重用C ++代码比重用C#甚至C复杂得多。

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

相关问题 在Visual Studio C#2013中导入matlab .dll导致问题 - importing matlab .dll in visual studio c# 2013 causing issue 在Visual Studio C ++中使用DLL - Using a DLL in Visual Studio C++ Visual Studio 2013挂在WinCE Deployment for C ++ App而非C#上 - Visual Studio 2013 Hangs on WinCE Deployment for C++ App not C# Visual Studio 2017 上 c# 中的 C++.dll - C++ .dll in c# on Visual Studio 2017 如何使用Visual Studio Express 2013将C#接口实现/合并到简单的C ++程序中 - How to implement/merge C# interface into a simple C++ program using Visual Studio Express 2013 使用Visual Studio 2013进行编译时将日期和时间添加到C#DLL - Adding Date and Time to C# DLL when compiling using Visual Studio 2013 Visual Studio 2013 C#构建输出到文件 - Visual Studio 2013 C# Build Output to File 在Visual Studio 2012 C#中使用由Eclipse制作的C ++ DLL? - Using c++ dll made from eclipse in visual studio 2012 c#? 查找在其上构建exe / dll的Visual Studio版本(C#) - Find visual studio release on which an exe/dll was build (C#) 链接到TFS-Git的Visual Studio 2013中的C#DLL项目的团队资源管理器中未出现构建节点 - Build Node doesn't appear in Team Explorer for a C# DLL project in Visual Studio 2013 linked to TFS-Git
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM