如何添加对非托管 C++ 的引用由 C# 项目调用的项目?

发布于 2024-10-19 11:22:41 字数 715 浏览 4 评论 0原文

一种解决方案 (the.sln)

一个 C++ 项目(2010 年的 mycppproject.vcxproj 或 2008 年的 mycppproject.vcproj),编译导出一些函数的本机 DLL。在调试中,此构建 c:\output\Debug\mycppproject_d.dll,在发布中,此构建 c:\output\Release\mycppproject.dll。

一个 C# 控制台应用程序 (mycsharpconsole.csproj),包含对 DLL 的 PInvoke 调用。

一切编译都很好。

当我构建时,我希望能够将 csharp 项目的引用添加到 cpp DLL 项目,以便它可以将适当的文件从适当的目录复制到 csharp 项目构建到的 \bin\Debug 目录中。

这应该是可能的,因为 IDE 知道有关 DLL 构建位置以及 C# 应用程序构建位置的所有信息。

在 Visual Studio 2010 中:

我尝试在 csharp 项目上使用“依赖项...”并添加对 mycppproject 的依赖项,但这没有效果。

我已在 csharp 项目上尝试“添加引用...”并添加对 cpp 项目的引用,但收到一条警告消息“项目“mycppproject”的目标框架版本高于当前项目目标框架版本” 。您是否愿意将此引用添加到您的项目中? (是/否/取消)。

单击“是”会产生错误消息“无法添加对 mycppproject 的引用”。

One solution (the.sln)

One C++ project (mycppproject.vcxproj in 2010or mycppproject.vcproj in 2008) which compiles a native DLL exporting some function(s). In debug this builds c:\output\Debug\mycppproject_d.dll and in release this builds c:\output\Release\mycppproject.dll.

One C# console application (mycsharpconsole.csproj) containing PInvoke calls into the DLL.

All compiles fine.

When I build, I would like to be able to add a reference from the csharp project to the cpp DLL project so that it can copy the appropriate file from the appropriate directory into the \bin\Debug directory the csharp project is built into.

This should be possible, since the IDE knows everything there is to know about where the DLL gets built, and where the C# application gets built.

In Visual Studio 2010:

I've tried "Dependencies..." on the csharp project and adding a dependency on mycppproject, but that has no effect.

I've tried "Add Reference..." on the csharp project and adding a reference to the cpp project, but I get a warning message 'The Target Framework version for the project "mycppproject" is higher than the current project Target Framework version. Would you like to add this reference to your project anyway?' (Yes/No/Cancel).

Clicking "Yes" produces the error message "A reference to mycppproject" could not be added."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-10-26 11:22:41

Visual Studio 不支持从托管 C# 项目引用非托管 C++ 项目,但 MSBuild 支持从任何其他项目引用任何项目。

您可以通过手动编辑 .csproj 文件来手动添加对项目的引用。在文件中,找到现有的一组 ProjectReference 元素(如果没有,则添加一个新的 ItemGroup)并添加以下引用:

<ProjectReference Include="..\mycpproject.csproj">
  <Project>{b402782f-de0a-41fa-b364-60612a786fb2}</Project>
  <Name>mycppproject</Name>
  <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
  <OutputItemType>Content</OutputItemType>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</ProjectReference>

当您执行构建时,引用将导致 MSBuild 首先构建引用的项目。 ReferenceOutputAssembly 值告诉 MSBuild 不要复制构建的输出程序集(因为 C++ 项目不生成输出程序集),而是复制 OutputItemTypeCopyToOutputDirectory 值指示它将输出内容复制到引用项目的输出文件夹。

您将能够在 Visual Studio 中看到参考,但您不能在那里做太多事情。

此答案基于 Kirill Osenkov 在他的 MSDN 博客上解决的类似问题:https://blogs.msdn.microsoft.com/kirillosenkov/2015/04/04/how-to-have-a-project -引用不引用实际二进制文件/

Visual Studio doesn't support referencing an unmanaged C++ project from a managed C# one, but MSBuild supports referencing any project from any other project.

You can manually add a reference to your project by editing the .csproj file by hand. In the file, find your existing set of ProjectReference elements (or add a new ItemGroup if you don't have one) and add the following reference:

<ProjectReference Include="..\mycpproject.csproj">
  <Project>{b402782f-de0a-41fa-b364-60612a786fb2}</Project>
  <Name>mycppproject</Name>
  <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
  <OutputItemType>Content</OutputItemType>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</ProjectReference>

When you perform the build, the reference will cause MSBuild to build the referenced project first. The ReferenceOutputAssembly value tells MSBuild not to copy the output assembly of the build (since the C++ project does not produce one), but the OutputItemType and CopyToOutputDirectory values instruct it to copy the output content to the referencing project's output folder.

You will be able to see the reference in Visual Studio, but you can't do much with it there.

This answer is based on a similar problem solved by Kirill Osenkov on his MSDN blog: https://blogs.msdn.microsoft.com/kirillosenkov/2015/04/04/how-to-have-a-project-reference-without-referencing-the-actual-binary/

妄断弥空 2024-10-26 11:22:41

您无法添加对非托管 DLL 的引用。

相反,您应该执行构建后任务来复制文件。

或者,您可以将非托管 DLL 的链接添加为 C# 项目中的文件,并将 Build Action 设置为 NoneCopy to Output Directory > 到如果较新则复制

You cannot add a reference to an unmanaged DLL.

Instead, you should make a post-build task to copy the file.

Alternatively, you can add a link to the unmanaged DLL as a file in the C# project, and set Build Action to None and Copy to Output Directory to Copy If Newer.

那些过往 2024-10-26 11:22:41

我会遵循Slaks的第二个答案...

[...]您可以添加指向非托管的链接DLL 作为 C# 项目中的文件,并将“构建操作”设置为“无”,并将“复制到输出目录”设置为“如果较新则复制”。

...后跟 我的评论,以区分调试和发布版本(即使有点“hackish”,因为它需要您手动编辑 C# 项目文件)

使用文本编辑器打开 C# 项目的 csproj 文件,然后搜索所有出现的“YourNativeCppProject.dll”(不包含“.dll”后缀,因此如果您也将 pdb 文件添加为链接,您会发现不止一个出现),并使用宏更改包含路径,例如: Include="$(SolutionDir)$(ConfigurationName) \YourNativeCppProject.dll

PS:如果你查看属性(F4),即使你切换到Release配置,VS也会显示Debug的路径,但是如果你编译,你会看到dll被复制到输出是发布版本*

I would follow Slaks' second answer...

[...] you can add a link to the unmanaged DLL as a file in the C# project, and set Build Action to None and Copy to Output Directory to Copy If Newer.

... followed by my comment, to differentiate between Debug and Release builds (even if is a little bit "hackish", since it requires you to manually edit the C# project file)

open your C# project's csproj file with a text editor and search for all "YourNativeCppProject.dll" occurrences (without the ".dll" subfix, so if you added pdb files as a link too, you'll find more than one occurrence), and change the Include path using macros, for example: Include="$(SolutionDir)$(ConfigurationName)\YourNativeCppProject.dll

PS: if you look at the properties (F4), VS shows you the Debug's path even if you switch to the Release configuration, but if you compile, you'll see that the dll copied to output is the release version*

歌入人心 2024-10-26 11:22:41

添加引用仅适用于同一解决方案中的 .NET 程序集、.net 项目或 COM 组件(无论如何都会为其创建管理器包装器...)。

事实上,在 C# 代码中使用 DLLImport 访问 C++ dll 并不意味着 Visual Studio 会知道应该将外部 dll 从何处复制到何处。

想象一下 DLLImport 是一个运行时选项,您放入其中的路径在运行时用于查找 dll,因此您必须自己部署 c++ dll 并使其可用于 .net 应用程序,通常位于同一个 bin 文件夹中。

Add Rederences only works for .NET assemblies, .net projects in the same solution or for COM components ( for which a manager wrapper will be created anyway... ).

the fact that in your C# code you access the C++ dll with DLLImport does not mean that Visual Studio will know from where to where the external dll should be copied.

Imagine that DLLImport is a runtime option, the path you puth in there is used at runtime to look for the dll, so you have to deploy the c++ dll yourself and make it available for the .net application, usually in the same bin folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文