Thread: Run MFC exe from Command Line

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    35

    Run MFC exe from Command Line

    Hi,

    I would like to create an MFC program using the wizard (MS VC++), but I would also like the facility to run the program from the command line using various switches and parameters.

    When I run from command line, I dont want the window to be display, just for it to do its business and return info back to command line.

    Does this sound fesible and easy to do?

    Regards

    Mike

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seems reasonable to me - when you detect a command line, you skip all the window generation and message loop business, and just do what the command line tells you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    35
    Any clues how I would detect a command line?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by magic.mike
    Any clues how I would detect a command line?
    CWinApp has a datamember (m_lpCmdLine) that contains the command line

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    35
    Many Thanks - I can trap the command line and not show the dialog box using follwoing code,

    Code:
    	CString cmdLine = this->m_lpCmdLine;
    if (cmdLine.Compare("-t") == -1)
    {
    
    	int nResponse = dlg.DoModal();
    	if (nResponse == IDOK)
    	{
    		// TODO: Place code here to handle when the dialog is
    		//  dismissed with OK
    	}
    	else if (nResponse == IDCANCEL)
    	{
    		// TODO: Place code here to handle when the dialog is
    		//  dismissed with Cancel
    	}
    }
    else
    {
    	dlg.commandLineSwitch(cmdLine);
    }
    If from command line, I call the commandLineSwitch function.

    I want to be able to send output to the console, but when using cout, nothing is displayed.

    Do I hae to change the output stream, and if so how do I do it?

    Many Thanks

    Magic

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    There's a switch in the PE file format (Which most exe files use) to decide whether the program will have a console or not. MFC apps are commonly created without consoles. Although I havent tried, I'm sure you could attach a console to your app using the AllocConsole API. This should also setup the std input and output handles allowing cout etc to work. If it doesnt, come back and we'll have another look

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Just pass /subsystem:console to the linker in order to get a console window.

    Code:
    #pragma comment(linker, "/subsystem:console")

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and Empty MFC project in Visual Studio?
    By Swerve in forum Windows Programming
    Replies: 7
    Last Post: 11-01-2008, 04:43 PM
  2. Having only one function run at higher priority
    By ulillillia in forum C Programming
    Replies: 35
    Last Post: 02-24-2008, 06:56 PM
  3. compiler does run .exe not
    By hoppy in forum C++ Programming
    Replies: 10
    Last Post: 01-06-2008, 02:59 PM
  4. run windows exe
    By munna_dude in forum Linux Programming
    Replies: 3
    Last Post: 10-10-2007, 01:12 AM