如何在.NET应用程序中使用C ++项目? [英] How to use a C++ project from within a .NET application?

查看:182
本文介绍了如何在.NET应用程序中使用C ++项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个常规的.NET开发人员,旨在将C ++库集成到.NET项目中。我有几个想法,但由于我是新的C ++在一般情况下,我不知道的技术的限制,我想努力工作。 C ++项目本质上是一个快速声音渲染器,可以播放多轨音频文件与一堆不同的后处理技巧。所有这一切都很酷,但考虑到我有一个简单的.NET WinForms应用程序,我想整合,事情开始看起来很乱。

I'm a regular .NET developer aiming to integrate a C++ library into a .NET project. I have a few ideas, but since I'm new to C++ in general I don't know the limits of the technology I'm trying to work with. The C++ project is essentially a fast sound renderer that can playback multi-track audio files with a bunch of different postprocessing tricks. All of this is cool, but considering I have a simple .NET WinForms app I want to integrate with, things start to look messy.


  1. 首先,C ++项目没有.NET绑定或ActiveX / COM集成。它是一个简单的MS VC ++ 9项目。如果我要在我的.NET应用程序中工作,我必须以某种方式接口与它,即。

其次,它被构建为作为一个独立的Windows应用程序运行,并管理其自己的窗口使用MS Windows API。这是好的,但不知何故,我需要.NET VM 在后台运行,管理显示和运行所有我的C#代码。我不写C ++,所以我需要坚持用C#构建一个应用程序围绕这个库。

Secondly, its built to run as an independent Windows app and manages its own windows using the MS Windows API. This is fine, but somehow I need the .NET VM running in the background, managing the show and running all my C# code. I don't write C++ so I need to stick with C# to build an app around this lib.

第三,我是否在同一个进程作为C ++ lib或不,我需要一个方法来构建/接口/调试这两个单独的应用程序,如果他们是一个。我没有C ++编程的背景,除了我已经编写了一对夫妇C ++ DLL的高性能数据操作。

Thirdly, whether I'm running in the same process as the C++ lib or not, I need a way to build/interface/debug these 2 separate apps as if they were one. I have no background in C++ programming except for the fact that I have written a couple C++ DLLs for high-performance data manipulation.

所以,很多问题,不知道如何开始!

So, many questions and no idea how to start!


  • 我可以完全编译这个lib并把它放到一个VC EXE中,但是如何 / strong>我的.NET代码进去了吗?或者,如何将C ++代码编译成.NET EXE,以便在受管环境中运行?注意,它不是为此设计的,如果我试图改变太多,可能会错误。

  • I can fully compile this lib and get it into a VC EXE, but how do I co-compile my .NET code into it? Or alternatively, how do I compile the C++ code into the .NET EXE so it runs within a managed environment? Note that it wasn't designed for this and may misbehave if I attempt to change too much.

主要问题是接口。如何公开一些C ++类从.NET访问?我知道我需要什么类,我只需要几十个类,以及他们的相关方法/属性可以从.NET。我不介意.NET中的手写包装类,以帮助.NET VM理解来回传输的字节的类结构。我希望我可以直接使用C ++对象从托管环境,所以我的大部分代码可以保留在.NET。

The main problem is interfacing with it. How do I expose some C++ classes to be accessed from .NET? I know exactly which classes I need and I'll need only a couple dozen classes along with their relevant methods/properties to be available from .NET. I don't mind handwriting wrapper classes in .NET to help the .NET VM understand the class structure of the bytes transferred back and forth. I'm hoping I can directly work with C++ objects from a managed environment so most of my code can remain in .NET.

即使我将它作为独立的应用程序运行,我必须使用套接字或其他东西进行通信吗?

Even if I run it as an independant app, would I have to resort to sockets or something to communicate with it? This is the absolute worse case and I'll do anything to avoid this.

任何帮助或指针都值得赞赏,我希望我自己和我的任务在手边清楚,我的问题是足够具体和负责任的。非常感谢您的帮助!

Any help or pointers are appreciated, I hope I made myself and my task at hand clear and that my questions are specific enough and answerable. Thanks for any help!

编辑:如果我编写包装类或生成它们,我可以使用P/Invoke 在其上创建类实例和调用方法,并让它们运行本机C ++代码在后台?这些C ++对象的内存将在哪里存储和管理?

If I write wrapper classes or generate them, can I use P/Invoke to create class instances and call methods on them and have them run native C++ code in the background? Where will the memory for such C++ objects be stored and managed? Within the .NET heap or outside it?

推荐答案

您可以将托管C ++(C ++ / CLI)包装器写入非托管C ++代码。这将是最灵活的解决方案。基本上,你在C ++中编写一个.NET程序集,该程序集公开了调用非托管代码的托管类。然后你可以从你的C#项目中引用这个程序集。

You can write managed C++ (C++/CLI) wrapper to unmanaged C++ code. It would be the most flexible solution. Basically, you write a .NET assembly in C++, the assembly exposes managed classes which call your unmanaged code. Then you can reference this assembly from your C# projects.

有一个非常基本的文章如何做: http://www.windowsdevcenter.com/pub/a/dotnet/2004/03/29/mcpp_part3.html

There is a very basic article how to do that: http://www.windowsdevcenter.com/pub/a/dotnet/2004/03/29/mcpp_part3.html

这可能也有帮助:
http://www.multicoreconsulting.co.uk/blog/c-snippets/how-to-call-unmanaged-cplusplus -from-csharp /

和MSDN文章 .NET Visual C ++中的编程

这篇关于如何在.NET应用程序中使用C ++项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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