How to port C++ code to C++/CLI in Visual Studio?

▼魔方 西西 提交于 2019-11-30 08:36:11

问题


I have an application written in native C++ which I'd like to get running on the .NET virtual machine. I was thinking of recompiling the C++ code as C++/CLI, using the Visual Studio 2008 compiler. Regrettably, I don't find any documentation on how to do this, so hence my questions:

  • Does this actually make sense? Am I trying the impossible?
  • Where can information on the topic be found?

回答1:


A lot of native C++ code will actually just compile and run on C++/CLI. This is really a kind of hybrid compiler that can call native Win32 functions and use standard C libraries like OpenGL. You can even call COM interfaces directly (all the stuff you can do with a native C++ compiler).

The .Net library is also available but for these you create managed classes (using the ref class keyword). You will use gcnew to allocate memory for these classes (from a garbage collected heap). Memory for your normal classes is still allocated using new and delete (from a standard, non garbage-collected heap).

In short, you can migrate to .Net in bits and pieces, though there is still some friction when switching between managed and unmanaged classes.

I found this book useful: Pro Visual C++/CLI.




回答2:


Go to project properties -> General -> Common Language Runtime support -> change to /clr

It's called CLR now. Read about it here and here.




回答3:


In C++ you can simply recompile your codebase with /clr. This technique called IJW (It Just Works) so you can easily use your existing classes with CLR.



来源:https://stackoverflow.com/questions/2116132/how-to-port-c-code-to-c-cli-in-visual-studio

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