混合托管/非托管C ++? [英] Mixing managed/unmanaged C++?

查看:126
本文介绍了混合托管/非托管C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用标准C ++编写的库。我也有一个.Net窗体应用程序编写的C#利用非托管库。

I have a library written in standard C++. I also have a .Net windows form app written in C# that utilizes the unmanaged library.

我理解我可以使用pinvoke,但我的C ++完全是OO,

I understand I can just use pinvoke, but my C++ is completely OO and I really wouldn't want to mess around with marshaling and such.

有没有办法,我可以创建一个新的托管C ++ dll项目,复制和粘贴我的标题和代码文件,并编译它并访问所有的类?

Is there a way that I can just create a new managed C++ dll project, copy and paste my header and code files, and compile it and have access to all the classes?

或者我需要在托管c ++中创建一些ref类,并将其挂接到我的非托管类?

Or would I have to create some ref class in managed c++ and hook it up to my unmanaged class? I just want this to be as simple as possible.

推荐答案

即使在C ++ / CLI中,仍然必须编写特定的.NET包装,它们看起来不一样。所以答案,你能把它放在一个C ++ / CLI项目中,并重新编译并使用它是,是没有。你仍然需要编组数据(在这种情况下,你可能还是使用p / Invoke可能)或者你可以创建句柄和使用固定内存,使您的结构可用于托管代码。

You would still have to write specific .NET wrappers even in C++/CLI, they just look different. So the answer to, can you put it in a C++/CLI project and recompile and use it as is, is no. You still have to either marshal the data over (in which case you may as well use p/Invoke probably) or you can create handles and use pinned memory to make your structures available to managed code.

我这样做了两个方面:我写了一个GPU库在CUDA C和调用它从F#使用p / Invoke和我写了一个视频处理库,并编写一个简单的C ++ / CLI包装,然后在.NET应用程序中使用它。我认为CLI包装更痛苦,但对于实时视频,我需要更少的延迟(更少的复制内存)。对于GPU项目,我更关心吞吐量。

I've done this both ways: I've written a GPU library in CUDA C and called it from F# using p/Invoke and I've written a video processing library in C++ and written a thin C++/CLI wrapper then used it in a .NET app. I think the CLI wrapper was more painful, but for live video I needed less latency (less copying memory). For the GPU project I was more concerned with throughput.

这篇关于混合托管/非托管C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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