如何在单一解决方案下为多个C ++项目编写CLI包装器? [英] How do I write CLI wrapper for multiple C++ project under single solution?

查看:63
本文介绍了如何在单一解决方案下为多个C ++项目编写CLI包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个解决方案文件下有三个C ++项目,每个项目都有多个C ++文件。我需要编写C ++ CLI包装器,因此C#应用程序可以访问C ++类的功能。



我创建了一个带有一个类的托管dll项目,它模拟了一个C ++的所有功能我的问题是,我是否需要创建三个托管dll项目,参考3个不同的C ++项目?另外,我是否需要在C ++类和托管类之间进行一对一的映射,例如一个本地C ++的托管类?



问候,

Maddie

解决方案

< blockquote>首先,请看我对这个问题的评论。



如果那些C ++项目是你的并且你有源代码,那么创建CLI的最佳方法包装器将您的项目从C ++迁移到C ++ / CLI。这样,您可以创建具有双重用途的混合模式

模块(托管+非托管):它们可以用作常规非托管库模块,以及常规.NET程序集,因此您可以使用它们通过引用程序集,以通常的方式在C#或VB.NET项目中。组织此类C ++ / CLI项目的最佳方法是将它们分离为非托管代码和托管包装器。您可以在托管CLIref类/结构中包装非托管类,并通过公开来公开引用程序集。



请参阅:https://msdn.microsoft.com/en-us/library/ms235282.aspx [ ^ ]。



另见我过去的答案:

如何在ASP.NET中调用C ++ DLL? [ ^ ],

处理Windows窗体应用程序 [ ^ ]。



如果你仍然需要使用显式P / Invoke,由一个或另一个原因,请参阅: https://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx [ ^ ]。



您将看到所有内容都基于 DLLImportAttribute ,这是基于库模块名称(DLL)使用的和每个功能的入口点名称: https ://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute%28v=vs.110%29.aspx [ ^ ]。



这意味着y你甚至可以在一个项目中包装任意数量的模块,而不仅仅是一个解决方案。



-SA


I have three C++ projects under one solution file and each project has multiple C++ files. I need to write C++ CLI wrapper so C# application can access functionalities of C++ classes.

I have created managed dll project with one class which is mocking all functionalities of one C++ class.

My question is, do I need to create three managed dll project referring to 3 different C++ project? Also, do I need to have one to one mapping between C++ class and managed class, e.g. one managed class for one native C++?

Regards,
Maddie

解决方案

First of all, please see my comment to the question.

If those C++ projects are yours and you have the source code, the best way to create a CLI wrapper would be migrating your projects from C++ to C++/CLI. This way, you can create mixed-mode
modules (managed+unmanaged) of dual purpose: they can be uses as regular unmanaged library modules, as well as regular .NET assemblies, so you could use them in your C# or VB.NET projects in a usual way, by referencing the assemblies. And the best way to organize such C++/CLI projects is to separate them into unmanaged code and managed wrappers for it. You can wrap unmanaged classes in your managed CLI "ref" classes/structures and expose the the referencing assemblies them by making public.

Please see: https://msdn.microsoft.com/en-us/library/ms235282.aspx[^].

See also my past answers:
How to invoke C++ DLL in ASP.NET?[^],
Dealing with windows form application[^].

If you still need to use explicit P/Invoke, by one or another reason, please see: https://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx[^].

You will see that everything is based on DLLImportAttribute, which is uses based on the library module name (DLL) and an entry point name, per function: https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute%28v=vs.110%29.aspx[^].

It means that you can wrap any number of module even in a single project, not only a single solution.

—SA


这篇关于如何在单一解决方案下为多个C ++项目编写CLI包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆