question

JiShirley-1989 avatar image
0 Votes"
JiShirley-1989 asked ·

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

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++
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Does the warning at the beginning of the function description in
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2019
apply to your project?

0 Votes 0 ·

Hi,

Could you please provide the screenshot to show the result you want?

According to the issue, if you want to achieve display "*" for input in mfc application? If so, as far as I'm concerned, you could try to change the "Password" property of the edit box to "True".

If you want to use "*" as the display of command line parameters, as far as I'm concerned, it couldn't be achieved. You could try to store them encrypted in a file and then load that file as a parameter.

Thank you!

0 Votes 0 ·

1 Answer

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered ·

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







·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.