Mixing managed/unmanaged C++?

北战南征 提交于 2019-12-12 13:34:03

问题


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

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.

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?

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.

Thanks!


回答1:


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.

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.




回答2:


You are probably going to want to write the wrapper classes in C++/CLI. It's fairly trivial to do, as long as you return handles instead of pointers, enumerators instead of collections, etc.



来源:https://stackoverflow.com/questions/10660214/mixing-managed-unmanaged-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!