-1

At a broader level, I'm converting a MFC application (MFC 6.0) into Windows Forms application (Visual Studio 2013). I read certain blogs that describes that this conversion is possible. By this conversion I can re-use code written in MFC, only I will need to create UI. However I will need to understand the previous code and may need to re-write it a bit.

I got the motivation from here and here.

I have performed following steps so far.

  1. Opened Visual C++ 6.0 project in Visual Studio 2013.
  2. Build it successfully.
  3. Then added CLR support to it, and fixed errors.
  4. Added a Windows form, and added controls to it. As mentioned here.
  5. Added functionality and build it successfully.
  6. Now when I run this application, then it still point to old MFC window.

I'm missing certain settings which will change the rendering window from MFC to WindowsForm. What am I missing here?

Addition to that, I see problem with this approach as described by @Roger in comments, and I understand that. So, I wanted to know for any tool/utility which may convert legacy code into C#. Does such utility available?

TIA.

Community
  • 1
  • 1
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
  • You know, it's not some magic setting, that miraculously makes your window appear on screen. It's code. Readable. Debuggable. Apply both, not necessarily in that order. – IInspectable Aug 24 '15 at 11:52
  • 1
    It sounds to me like it would be much easier to create a Windows Forms application, add the controls to it, then port the logic. Trying to get a quick win always ends up with a slow painful loss. – Roger Rowland Aug 24 '15 at 12:07
  • @RogerRowland that is another way to do it... I have a very huge project where I want to do this.. So instead of creating a new WinApp, I just want to do modifications in current MFC app. I have successfully created first screen, only the matter is, I'm not able to link it with CWinApp object.. – Amnesh Goel Aug 24 '15 at 12:09
  • *"huge project"* .... *"I have successfully..."* ..... *"I'm not able to ..."* .... yep, this is only the first of your many problems ;-) I strongly suggest you try the way I mentioned, it will also give you an opportunity to refactor and take advantage of the .NET Framework where appropriate too. – Roger Rowland Aug 24 '15 at 12:13
  • @RogerRowland I don't mind to go in that approach too..Then clear my one doubt.. the code that was written in old MFC application was written using Visual C++ (all windows control/handlers etc), and when I start working with WindowsForm then it uses C#. So how these two different languages will work together.. or how can I port every bit of C++ code in C#? – Amnesh Goel Aug 24 '15 at 12:16
  • @RogerRowland two more things.. First, do you know a way to set the start page in my current appraoch? Second, if I go with your approach then what code I can port to WindowsForm and how? Because my concern is different languages. I can compile one DLL of existing code, but how that C++ code will help, I'm figuring out that yet. – Amnesh Goel Aug 24 '15 at 12:19
  • C# and C++ are quite similar. However, the point I'm trying to make is that you are porting *functionality*, not porting *code*. You need to read and understand what the MFC application does, then reimplement that same functionality using C# and WinForms. There's no quick win. – Roger Rowland Aug 24 '15 at 12:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87779/discussion-between-amnesh-goel-and-roger-rowland). – Amnesh Goel Aug 24 '15 at 12:32
  • C++/CLI is the tool to port functionality **and** code. Since you don't know MFC, nor C#, nor C++, nor the tools involved, this isn't going to be easy. Or even possible. – IInspectable Aug 24 '15 at 13:21
  • @IInspectable I know flow of MFC application, I have worked on that.. I have worked on C#.. And c++ I did in school days.. However it would have been good to give some real help instead taunting whatever who u r.. If u hv knowledge then share it else stay away.. Thanks – Amnesh Goel Aug 24 '15 at 13:56

1 Answers1

1

The code you are referring to seems suitable for amending a MFC application with a few forms as child windows to make use of .NET features. However, the main window is another story. You wrote the application is huge, so I suppose you don't want a simple form as your main window and rather have some kind of MDI interface in mind. If you replace the CMainFrame in the legacy MFC application, it just doesn't make sense to maintain an old CWinApp class. Anyway, if you are hell-bent on going down that path, you may want to have a look at an old CodeProject articel (.NET1.x, .NET2.x) to get a better grasp at the whole concept.

But as Roger already suggested, it would be a wise choice to find a nice GUI framework, perhaps even WPF instead of WindowsForms, and do a GUI rewrite -- especially if one part of the motivation for the conversion is to move to newer UI concepts. If your C++ program logic is well separated in your MFC project, put it in a COM server and make use of interoperability.

thomiel
  • 2,467
  • 22
  • 37
  • 1
    C++/CLI is **a lot** better suited to implement an interop layer, than creating a COM server. Unless your application is already implemented using COM, go with C++/CLI (see [.NET Programming with C++/CLI (Visual C++)](https://msdn.microsoft.com/en-us/library/68td296t.aspx)). – IInspectable Aug 25 '15 at 08:43