0

I have an mfc dll that I like to load into an C sharp program!

My source tutorial: http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx

The problem is that he use primitive data types (e.g. int) in the function to load, but I need own types (the class object for getInstance() )!

Is there an easy way to do that?

Thank you, greets leon22

leon22
  • 5,280
  • 19
  • 62
  • 100

1 Answers1

1

You can't consume C++ classes in a C# project. In fact you can't consume C++ classes in any project compiled with a different compiler, e.g. a different version of MSVC.

Your best approach here is to use COM which is a binary interface standard designed to solve exactly this problem.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Yeah, this. I'm not sure why you'd want to use an MFC DLL anyway. The big benefit of MFC is that it wraps the Windows API into classes. The .NET Framework provides either WinForms or WPF for this purpose. Using `GetInstance` in particular makes no sense. – Cody Gray - on strike May 09 '11 at 11:18
  • I have an old mfc dll we need in the new C sharp program! (getInstance() is an own method to return the class object) However, the best way is to use a COM, isn't it? – leon22 May 09 '11 at 11:29
  • @leon22 Other than COM you could try to recompile using the .net version of C++ which is named either managed C++ or C++/CLI, I can never remember which is the old name, and which is the new name! My guess is that COM will get you where you need to be more quickly. – David Heffernan May 09 '11 at 11:31
  • COM is not the best way if you've never written a COM server before, it has a steep learning curve. First consider simple C functions if you can flatten the MFC code to a non-OOP interface. If that's too much hardship then write a C++/CLI wrapper. See http://stackoverflow.com/questions/2691325/c-cli-mixed-mode-dll-creation/2691448#2691448 – Hans Passant May 09 '11 at 12:48
  • Thanks. But it's really necessary to write a wrapper since VS support mixed code (managed/unmanaged)?! – leon22 May 11 '11 at 05:47
  • 1) Sure you can... the /clr switch on the C++ is the key. 2) We wnt to because there is 20+ years of C++ that needs to be incrementally migrated over the next 5 years. – David V. Corbin Jan 17 '21 at 13:23
  • @David It's an MFC DLL. It's not a C++/CLR DLL. – David Heffernan Jan 17 '21 at 14:30
  • @DavidHeffernan - test, it is, and there is nothing to prevent adding a "ref class" then compiling with /clr. No "wrapper" needed. FWIW I responded because I am currently working on a project this relates to, and found this thread during my research.... I have thousands of MFC components over 100s of DLLs that make up a large commercial app. Now having aModule State in both the App and the Dll is straightforward and known... – David V. Corbin Jan 17 '21 at 18:06
  • But the twist is NOT having a Module State relate to th App at all...rather redirect the State from all the Dlls that would normally resolve to the EXE to the custom (native C++) DLL. If you have info, feel free to reach out david(dot)corbin(at)dynconcepts(dot)com. – David V. Corbin Jan 17 '21 at 18:06