Writing C# managed code in native C++
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开发者_StackOverflow中文版 error .
Any suggestion is welcome.
Thank you in advance.
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.
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.
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.
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!
精彩评论