Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every body

How can I use .NET4 library from MFC/C++ applications ? Actually I want use .NET4 library from my MFC project and I need pretty compatibility ... :)
Posted

I almost forgot: there is another approach, more realistic in your case.

You can develop your code as a mixed-mode C++/CLI application, managed + unmanaged. In such project, you can reference any .NET assembly and use it as it is usually do with .NET. At the same time, the unmanaged part of the code can implement an MFC application. The language provides wide opportunities for collaboration between managed (CLI) and unmanaged code.

However, I would advice you to strictly segregate your files in three groups: 1) purely unmanaged code; 2) the code with managed "ref" classes; 3) the code with unmanaged wrappers of the CLI code; these wrappers can be used in group #1.

Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA
 
Share this answer
 
Comments
Aydin Homay 28-Mar-13 4:58am    
Very good answer so tnx Sergey Alexandrovich kryukov ;)
Sergey Alexandrovich Kryukov 28-Mar-13 10:10am    
You are welcome.
Will you accept if formally then (green button)? — thanks.
—SA
H.Brydon 28-Mar-13 16:17pm    
I like your solution #2 better than #1 (+5 accordingly).
Sergey Alexandrovich Kryukov 28-Mar-13 16:23pm    
Well yes, if C++ is used anyway, so C++/CLI can be used, it's better. I just failed to think about it at first.
Solution #1 is more interesting (and still perfectly legitimate), just because it breaks the common misconception that export to unmanaged is not allowed for .NET assemblies. Actually, this is allowed by the standard for IL directly, and for C++/CLI, indirectly, but not allowed in most .NET languages.
Thank you.
—SA
Aydin Homay 29-Mar-13 5:36am    
Dear guys

Actually when we have legacy project from (MFC or ...) we have more problems with develop and extend that. if we develop new sub system from .NET and integrated with our legacy system we have COM technology problem or if we develop new sub system from MFC again we lost .NET feasibility... :/
Maybe if we develop new sub system from .NET and we use .exe from our legacy system with shell and ... is a better than other solutions !
Many would say it's impossible (not counting creation of a COM component).

This is in principle possible, but in practice, just ridiculous. Too difficult to bother, and too easy to port to .NET from majorly obsolete and not-so-easy-to-use MFC.

However, if you really want to bite a bullet, there is one of the method explained in some CodeProject articles with overview and discussion in my past answer:

loading C# DLL in MFC[^],
How can I use a dll created in Visual Basic 2008 in Visual Basic 6.0[^],
Call Managed DLL written in C# from Unmanged Code VC++[^],
API's in .Net: Managed or UnManaged Code[^].

Note that some members considered this approach as highly questionable, but I value it much more than COM which contaminates the system. It works, even though many would say it's impossible.

Again, I don't recommend any. Get rid of MFC and don't torture yourself. Anyway, you decide.

[EDIT]

I almost forgot: in your special case, there is another solution. Maybe it will work for you better. Please see Solution 2.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900