HOWTO: invoking a C # managed interface from unmanaged C ++ on a WindowsCE Compact platform

I have an extensive unmanaged Windows CE 5 C ++ code that provides a user interface that I want to use in a new product, combining it with a lot of new business logic and communications written in managed C # in Windows CE 6 and Compact Framework .

The user interface may know about business logic, but I want the business logic to not know the user interface, so I can later replace it with a managed version or any other user interface that I choose as the front-end.

I found an article that describes how to use COM as a bridge in the Windows world, but I can hardly use it in .NET CF under WinCE. I used to import type libraries and use COM calls (CoInitialize (), CoCreateInstance ()) to get pointers to interfaces on other Windows platforms and what is the strategy I'm currently pursuing: using COM directly in an unmanaged C ++ library to access C # interfaces in my managed library, assuming WinCE provides the same facility.

Here is my problem: typelib. It is not available from my C # managed library since I used it in the past with the "#import" SomeCPPLibrary.dll instruction. I believe that he is buried in the .dll assembly, stored differently than it was in the past and, therefore, is not directly accessible through the #import of the library itself. I think I can #import typelib, but I can’t find a way to extract typelib from my managed .dll, and although I could crack the interface definition file (.idl) and use the midl.exe platform to generate .tlb from it the guarantees that my .idl and therefore the result of .tlb will really match what is in my C # .dll. I don’t even know if the midl.exe platform works this way, but suppose it does.

  • Am I barking the wrong tree? Is it possible to use the C # managed interface in unmanaged C ++ through the corresponding COM interface?

  • Does the [assembly: ComVisible (true)] attribute in the AssemblyInfo.cs file set all the interfaces in the managed assembly that are accessible through COM in an unmanaged world using the GUID that defines AssemblyInfo.cs, or do I need to do something else?

  • How to get typelib from a managed .dll so that my unmanaged C ++ library can #import it?

  • I tried adding my managed C # library project as a reference in an unmanaged C ++ library project, but that didn't seem to help. Is such a link relevant in general in this situation?

  • Is there a better approach to solving the main problem of invoking managed C # code from the unmanaged C ++ world? Something I just read about is libarary mixed mode with a controlled translation layer to bridge the unmanaged / managed gap. I’m not sure if this is a good strategy, because the speed of answering calls is an important factor, but maybe it will be better in the long run, as I plan to rewrite the user interface to managed C # at some point and, thus, makes all efforts to drop the UI instead of dropping with a more consistent business logic? Regardless of the answer to this question, I would still like to solve the problem of using COM, if only curiosity.

+6
windows-ce compact-framework com managed unmanaged
source share
2 answers

I tried calling C # from C ++ to WinCE. I do not believe that there is COM support provided by the Compact Framework, so you cannot use ComVisible (true).

I also could not find a way to host .NET in C ++, because again the functionality was not found in the Compact Framework.

My solution was to create a C # stub application and contact the C ++ host through Msg Queues. It also solves the problem of data marshaling. The performance for my use is fine. The biggest cost is the startup time for the stub, which you will have to pay in any case if you have finished the C # application.

+3
source share

You bark the wrong tree. For native code to invoke managed code, the native code must rotate the CLR execution engine. This is called CLR Hosting , and it is not supported in CF. This means that you simply cannot do this - even with a creative hack (believe me, I tried every attempt).

+2
source share

All Articles