I added a small bit of code to the InitInstance function of an MFC application created by the New Application template.
The code looks for /CONSOLE on the command line and, if it is found, it runs a console interface on a secondary thread while the main thread waits for the secondary thread to exit. After the console interface is closed the program terminates.
BOOL CMFCToggleApp::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinAppEx::InitInstance();
int nArgs = 0;
bool bAlloc = false;
LPWSTR *psz = CommandLineToArgvW(GetCommandLineW(), &nArgs);
for (int i = 0; i < nArgs; i++)
{
if (_wcsicmp(psz[i], L"/CONSOLE") == 0)
bAlloc = true;
}
if (bAlloc)
{
std::thread tconsole([]{
FILE *fpstdin = stdin, *fpstdout = stdout, *fpstderr = stderr;
if (AllocConsole())
{
// Initialize for console i/o
freopen_s(&fpstdin, "CONIN$", "r", stdin);
freopen_s(&fpstdout, "CONOUT$", "w", stdout);
freopen_s(&fpstderr, "CONOUT$", "w", stderr);
puts("This is the console interface on a secondary thread");
puts("The MFC main window has not been created");
puts("Hit a key to exit the program");
_getch();
fclose(stderr);
fclose(stdout);
fclose(stdin);
FreeConsole();
}
else
{
CString strError;
strError.Format(_T("AllocConsole failed, error code was %d"), GetLastError());
AfxMessageBox(strError);
}
});
tconsole.join(); // wait for console interface thread to exit
return FALSE; // exit the program
}
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
EnableTaskbarInteraction(FALSE);
// AfxInitRichEdit2() is required to use RichEdit control
// AfxInitRichEdit2();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(4); // Load standard INI file options (including MRU)
InitContextMenuManager();
InitKeyboardManager();
InitTooltipManager();
CMFCToolTipInfo ttParams;
ttParams.m_bVislManagerTheme = TRUE;
theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMFCToggleDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMFCToggleView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line. Will return FALSE if
// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}