How to change the input parameters of Command Line in MFC C++ application

Ji Shirley 181 Reputation points
2020-08-05T07:31:54.157+00:00

I have a program developed by MFC C++ x86. This MFC application supports Command Line startup and obtains execution parameters through the command line. Now I want to obtain input parameters in the following way:

C:\MFCCommandLine.exe -a
C:\Input parameters:
C:\*** *** ***

Then, I want to use _getch() to get input and use putchar to replace it.

std::cout << "Input parameters: " << endl;
char str[15] = { 0 };
char ch;
for (int i = 0; (i < 14 ) && (( ch = _getch()) != EOF) && (ch != '\n'); i++)
{
    str[i] = ch;
    putchar('*');
}

But after testing, problems were found: After "Input Parameters: " displaying, if you don’t press Enter key, the entered character will not be changed to *; If you press Enter key and enter multiple characters, only the first character becomes *, and the rest remain in plain text; The same code does not have the problem in the Windows Console application.

Maybe my writing above is wrong, I hope to obtain the input characters of the command line in MFC, and use * as a replacement to display on the command line.
Any suggestions?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 40,286 Reputation points
    2020-08-09T17:58:43.613+00:00

    I assume that you are attaching the MFC application to the parent process console in order to obtain input from the console window.

    However, when starting the MFC application from the command line, cmd.exe is also processing console input and that is the interference that you are experiencing.

    You can build your MFC application and then use the editbin utility to change the application's subsystem from Windows To Console.

    This will cause the system treat your MFC application as if it was a console application and the application handles for stdin, stdout and stderr will be initialized automatically at startup.

    By doing this your MFC application will be able to interact with the user for console input without interference. However, it also means that closing the console will also terminate the MFC application. You can manage the visibility of the console window to avoid accidental closure by the user.

    For example, I created a new MFC SDI application using the new application wizard. In the application class I added a bool variable and some code to InitInstance and ExitInstance as follows:

    Added to InitInstance -

    // Added by RLWA32
    DWORD dwId = 0, dwNum = 0;
    
    dwNum = GetConsoleProcessList(&dwId, 1);
    _ASSERTE(dwNum != 0);
    
    if (dwNum > 1)  // more than one process is sharing the console
    m_bShowConsole = true;
    
    _tprintf(_T("Enter Input:"));
    
    TCHAR str[5]{};
    int ch{};
    int ndx = 0, count = ARRAYSIZE(str) - 1;
    
    while (ndx < count)
    {
    ch = _gettch();
    if (_istalnum(ch))
    {
    str[ndx] = ch;
    _puttch(_T('*'));
    ++ndx;
    }
    };
    
    // Hide console window so we cannot be terminated by mistake if the user closes the console
    ShowWindow(GetConsoleWindow(), SW_HIDE);
    
    MessageBox(NULL, str, _T("Entered info"), MB_TOPMOST | MB_OK);
    
    // End of RLWA32 additions
    

    Added to ExitInstance -

    // Added by RLWA32 - Unhide the console window
    if (m_bShowConsole)
    ShowWindow(GetConsoleWindow(), SW_SHOW);
    

    After building the application this was the editbin.exe command -
    editbin /subsystem:console MFCSample.exe

    0 comments No comments

  2. kamryncer 1 Reputation point
    2022-10-31T03:49:43.923+00:00

    I wish to have a small dialog based application which is passed command line parameters, so, using VC++6 I ran the application wizard and chose an MFC dialog application.

    This is not automatically equipped with command-line parameters. So I went to MSDN to refresh my memory on these. MSDN states that all C++ programs have either a main() or a wmain() function and that the argc, etc. arguments go here. The application I just created does not have these.

    As there is obviously a function which is the entry point to the application, can I stick the arguments here? I did try this, but I am not convinced that I was actually editing the correct function. (Can I find the function which is acting as the main() function from the project settings or similar?)

    Basically, how do I get my program to read command line parameters.

    Also as a sideline. For a simple program, which this is, I really do not want to make it an MFC application, and thereby over a MB in size. Are there application wizard template libraries that will allow me to make a non-MFC dialog application?