MFC Dll with COM interface
I am new to managed / unmanaged compatibility and COM concepts.
I was prompted to use COM Interop to use my existing C # MFC code. But the problem for me is that I have an MFC Dll which is not a valid COM component. How can I get these MFC libraries to have COM available interfaces ready for use in .NET?
a source to share
From Thread Loading MFC DLL to Windows C # Application
To access native code from C #, you have several options.
In most cases, you can use DllImportAttribute to describe DLL entry points in C # terms so that they can be called via P / Invoke. They will look like static methods to your C # program.
Less directly, you can create a Managed C ++ assembly that wraps your DLL in one or more managed objects. The managed C ++ DLL can be accessed from C # using the Add Reference (because it is a managed assembly with a .dll extension), and you can also access your MFC DLL using #include to include the MFC dll header file.
The third option is to turn your dll into a COM object so that your C # program can access it that way.
a source to share
There is no easy way to make MFC COM Dll available. You have to write a lot of COM code by hand by creating a COM wrapper. If you have no previous experience with COM, this can be difficult. The second option from the message of Yakov Seleznev looks less painful. A C ++ / CLI wrapper internally linked to an existing MFC dll and providing a clean .NET interface to the C # client looks like the optimal solution.
If the MFC Dll is exporting a C-style interface (API) and not classes, use PInvoke.
a source to share
In my experience, I agree with @Jacob Seleznev and add that if your MFC DLL interface contains mostly "simple" parameters and return types, using DLLImportAttribute is most likely your path of least resistance.
A great reference for marshaling types is to see how it is done against the unmanaged Win32 API. It can be found here: pinvoke.net
What I did was find an API call with similar types and see how that call is configured on pinvoke.net. I don't use it more often, but it was very useful a few years ago.
Hope it helps.
a source to share
Of the three ways to call native code from managed code (COM Interop, P / Invoke, and IJW, or C ++ / CLI interop), COM Interop is the slowest. And if your existing native code is not in the form of a COM component, then this is also the hardest part because it will be step 1.
To use P / Invoke, you need some C-style (extern C) functions that go on and call your existing code. To use IJW or C ++ / CLI interop, you implement a class public ref
(in the compiled / clr file) with methods that continue and call existing code. It is up to you which will be easier for you. Once you have the wrapper, from C #, you can make PInvoke with the attribute DllImport
when you declare functions, and then call them as usual. To make an IJW, you add an assembly reference with a public ref class in it and call the methods in that class as usual.
My recommendation is to ask if you want some kind of facade pattern where you put some kind of logic before the interaction - if so, then go IJW. Also, if you want to control marshaling, go to IJW. If not, go to P / Invoke. But it works anyway.
a source to share