Consume C# dlls in C++ for Windows Phone 8
I have a windows phone 8 app created with Visual Studio 2012 - "Windows Phone Direct3D App (Native only)" project under Visual C++, and a library created with "Windows Phone Class Library" project under Visual C# group having target platform "Windows Phone OS 8.0". All I need is to consume the C# dll in the C++ code. The restriction is that I cannot change the architecture of the application, so the main entry-point must be in the C++ project, and from here to call somehow the code written in C#.
If I try to reference the C# dll in C++ app I get this error
a reference to [dllname] cannot be added because the two projects target different runtimes.
Based on http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714080(v=vs.105).aspx , I tried also to reference the C# dll indirectly through a Visual C++ "Windows Phone Runtime Component" but again I get the same error when I try to add reference to the runtime component. I tried to change some project settings and nothing worked. In all samples and demos which I found on the web, for WP8, the main entry-point is always in C#. Only some hints regarding windows store apps seem to indicate that could be possible to consume C# from C++, but I don't find any reference saying explicitly that my scenario on wp8 is possible or impossible.
Thanks in advance to anybody which shares the solution or any suggestion.
Late answer, but the short story is that you can't. In Windows 8 C# WinRT dlls are full WinRT dlls that can then be consumed by any framework (WinJS, Native etc.) but not so for the phone.
On the phone C# can consume C++ WinRT objects (from C++ dlls), but not vice versa. You can create trickery by having your C# dll call into your C++ dll and pass around function pointers, but that's about it.
I've liked this: http://www.developer.nokia.com/Community/Wiki/C%2B%2B_support_from_Windows_Phone_8 . You may inject an interface into the .winmd C++ side metadata, that could be referenced and implemented in C#. Much cleaner than using delegates ... Still have to be initiated from C# ...
链接地址: http://www.djcxy.com/p/67518.html