Writing C# managed code in native C++

狂风中的少年 提交于 2019-12-11 07:31:02

问题


I am developing a managed lib (using Microsoft Web Services) and I am including it into a c++ project. The project doesn't use /clr option, so when I include my library's header file VS2005 show me an error saying I have to use /clr option. Doing this I have a incompatibility with /EHs command line option (error D8016), but changing from EHs to no exception handling not solving problem and keep showing me same error .

Any suggestion is welcome.

Thank you in advance.


回答1:


If you have unmanaged C++ code and want to use managed code, you have a few options:

  • Change your unmanaged code to C++/CLI, by use of the /clr switch.
  • Write a C++/CLI wrapper library. It could DLL-export unmanaged functions which you call in your unmanaged code.
  • Skip the wrapper library and directly DLL-export unmanaged functions via this library.



回答2:


You can't use a managed lib from an unmanaged c++ application. Since you add the /clr option, your c++ application becomes managed too (just for the record :) )

Here's what might help you: http://msdn.microsoft.com/en-us/library/ffkc918h.aspx - the restrictions of the /clr option.




回答3:


It is possible to write managed c++ adapter, that will call the C# library, and call this adapter from unmanaged c++ program as you would usually call a normal c++ library. You will compile your adapter library with /clr and your main c++ program without /clr if for whatever reason you want to keep it unmanaged.




回答4:


You can embed a mono environment and start an AppDomain. mono's runtime API will allow you to instantiate classes and call members on them. It will be clumsy, but is will work

http://www.mono-project.com/Embedding_Mono

Note that Mono is a full .Net 4.0 compliant CLR and it can work with the Microsoft core libraries on Windows.

On windows and Unix it can work with the Mono corlib/class libraries. There are areas not covered in Mono, but they seem to get sparse. You can use the MoMa tool to spot whether your application uses incompatible/incomplete APIs.

Or you can just use the Microsoft .NET framework, assuming you're on windows anyway!



来源:https://stackoverflow.com/questions/6095982/writing-c-sharp-managed-code-in-native-c

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