1
votes

We have a command line application that could benefit from a GUI. We want to add some plotting functionality and have identified a plotting library that uses MFC. Initially we developed a separate app, but we'd rather have the GUI in the same process space.

I was thinking of possibly a GUI in an MFC DLL that could be hosted in the production app AND in a testing app.

The questions are:

  • What are the steps necessary to add an MFC GUI to a win32 command line app
  • Is it possible to make a GUI in an MFC DLL and how can it be done? (so that different apps can reuse the same GUI)

EDIT

I should add that this is an unmanaged app (and needs to stay that way - it needs to be highly performant, makes extensive use of templates, boost, custom allocators, internally developed thread serialization, etc)

RESULTS:

Nick D's answer worked great - especially the follow-up link in his comment with the details about a regular MFC DLL.

Note that we will be using Qt for the next iteration. Modifying our build environment and getting used to a a new framework was just too much this time around.

3
I don't have VS 2008 but the first step would be to create a "MFC AppWizard (dll)" project, or something similar.Nick Dandoulakis
Nick, Right, but then what? The devil is in the details - like how to add MFC support in the existing exe? Is that necessary at all?Tim

3 Answers

3
votes

You can call/reuse GUI code in a dll. (I even use Delphi forms in my C++ projects)

A very simple dll example:

// The DLL exports foo() function
void foo()
{
    AFX_MANAGE_STATE( AfxGetStaticModuleState() );

    CDlgFoo dlg;
    dlg.DoModal();
}

In the console program you'll have code like this:

h = ::LoadLibrary( "my.dll" );
::DisableThreadLibraryCalls( h ); 
pfoo = (foo_type*)::GetProcAddress( h, (const char*)1 );
if ( pfoo ) pfoo();
2
votes

First, you will have to surrender WinMain().

If you still want to retain the command-line arguments functionality, process command arguments in InitInstance() of your App class.

1
votes

The straight forward approach would be to add a switch to your program and given a certain value it will launch the gui, otherwise use the command line options. Something like "app.exe -mode=gui". If you don't see this command arg on program launch, fall back to the old command line behavior.

Regarding the DLL, you could write all the UI functionality in a DLL and use it from your "production app" where you have a message loop running and a WinMain. But what's the point? If it's for testing purposes why not just separate the presentation from the logic and test the logic alone. How do you intend to test the UI in your test app anyway? Simulate button clicks?