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

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

问题描述

我是个普通.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应用程序与项目的工作,我会以某种方式的接口有了它,即。创建类的实例,设置属性,调用方法,在元帅数据进出等。

  1. Firstly, the C++ project has no .NET bindings or ActiveX/COM integration. Its a plain 'ol MS VC++ 9 project. If I am to work with the project in my .NET app I'll have to somehow interface with it, ie. create class instances, set properties, call methods, marshal data in and out, etc.

其次,它的建成为一个独立的Windows应用程序运行和管理其自己的Windows使用微软Windows API。这是好的,但不知何故,我需要的.NET VM的运行的背景下,管理展示和运行我的C#代码。我不写C ++,所以我需要用C#,坚持围绕此lib中的应用程序。

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或不是,我需要一种方法为建立/接口/调试这2个独立的应用程序,如果他们之一。我在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,但我怎么共同编译我的.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.

主要的问题就是接口它。我如何暴露以从.NET访问一些C ++类?我知道我需要到底是哪班,我就只需要与他们相关的方法/属性沿着几十类可从.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 ++对象内存中存储和管理?内.NET堆或外面?

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 - 从-CSHARP /

This might be helpful as well: http://www.multicoreconsulting.co.uk/blog/c-snippets/how-to-call-unmanaged-cplusplus-from-csharp/

和MSDN文章的。在Visual C NET编程++

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

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