2

I have an winmain application that is executed from cmd.exe and prints output to it. I atach to the cmd.exe using AttachConsole(ATTACH_PARENT_PROCESS). After application is executed and output is printed to cmd.exe command line prompt is not displayed and it looks like application is stil running(while it is already closed). Before closing my application I release the console using FreeConsole().

#include <iostream>
#include <fstream>
#include <windows.h>

int wWinMain
(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine,
    int nCmdShow
) 
    {
    AttachConsole(ATTACH_PARENT_PROCESS);

    std::wofstream console_out("CONOUT$");
    std::wcout.rdbuf(console_out.rdbuf());

    std::wcout << L"\nSome Output" << std::endl;

    FreeConsole();

    return 0;
    }

Current result: enter image description here

My goal:enter image description here

How should I make prompt C:New folder> appear after myapp.exe has printed its output and is closed.

LosBlancoo
  • 113
  • 10
  • Showing a [mcve] would certainly help. It doesn't sound like that should take more than 3 lines of code. – IInspectable Aug 16 '16 at 12:10
  • My bad. Added code example. – LosBlancoo Aug 16 '16 at 12:34
  • I am not convinced that this is the right solution to your problem: http://stackoverflow.com/questions/38788851/is-it-possible-to-launch-command-line-application-without-displaying-its-window – David Heffernan Aug 16 '16 at 12:39
  • Could you mention other ways to solve it, because as far as my research has gone(and I did try my hardest) I've come to conclusion that it is the only way if you want to have application that: 1. Requires elevation 2. In some cases needs to be launched from command line and print output to it. 3. In other cases needs to be launched from other application and make no appearance. – LosBlancoo Aug 16 '16 at 12:45
  • You are calling `FreeConsole` while your *console_out* object still references it. I don't know if this errors out, but neither do you, since you aren't interested in the return value from [FreeConsole](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683150.aspx). – IInspectable Aug 16 '16 at 12:50
  • I've used debugger to check if it returns success value and it does. – LosBlancoo Aug 16 '16 at 12:53
  • The use of `wWinMain()` shows that the system thinks the program is a windowed application, and thus `cmd.exe` will begin prompting for more commands right away. Your extra message doesn't change anything; it will just shove the user input down a few lines. The console will still be waiting for a command from the earlier prompt. What are you really trying to accomplish by doing this, in the long term? – andlabs Aug 16 '16 at 13:04
  • Can't you make it a console app rather than a GUI app so this is unnecessary? – jcoder Aug 16 '16 at 13:04
  • @jcoder If I make it as console app, it is impossible to completely hide it when launching with `ShellExecuteEx` from other application. – LosBlancoo Aug 16 '16 at 13:05
  • Why don't you launch it using `CreateProcess` then? – IInspectable Aug 16 '16 at 13:12
  • @IInspectable You cannot launch an executable that requires elevation using `CreateProcess`. :( – LosBlancoo Aug 16 '16 at 13:15
  • Place the code that requires elevation into its own module, manifested with `requireAdministrator`, and keep using `CreateProcess`. Or use the [COM Elevation Moniker](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679687.aspx). – IInspectable Aug 16 '16 at 13:19
  • 1
    If I were you I would ask about your problem rather than your solution. – David Heffernan Aug 16 '16 at 13:24
  • @IInspectable Is it possible to launch an application that requires elevation from non elevated process using `CreateProcess`? Because if I set requestedExecutionLevel level="requireAdministrator" and try to launch that `.exe` using `CreateProcess` I get error code 740 - The requested operation requires elevation. – LosBlancoo Aug 16 '16 at 15:37
  • @LosBlancoo: Not with `CreateProcess()`, no. See [How to run application which requires admin rights from one that doesn't have them](http://stackoverflow.com/questions/11586139/). Also see [Vista UAC: The Definitive Guide](http://www.codeproject.com/Articles/19165/Vista-UAC-The-Definitive-Guide) and its `CreateProcessElevated()` helper function. – Remy Lebeau Aug 16 '16 at 17:27
  • I'm fairly convinced that the implementation for `ShellExecuteEx` doesn't use any secret OS internals, but calls `CreateProcess` at some point. It may not be a simple matter of calling `CreateProcess`, but it should be possible. – IInspectable Aug 16 '16 at 17:32

1 Answers1

2

In case the question has not been answered yet (after such a long time), it is required to simulate the actual pressing of the 'Enter' key in the console window by sending (or, preferably, posting) the corresponding WM_KEYDOWN message to the console window, i.e.

after std::wcout << L"\nSome Output" << std::endl;

and before calling FreeConsole(), insert the following:

HWND hWndCon_ = ::GetConsoleWindow();
 if( hWndCon_ ) {
    ::PostMessage( hWndCon_, WM_KEYDOWN, VK_RETURN, 0 );
 }

or simply

::PostMessage( ::GetConsoleWindow(), WM_KEYDOWN, VK_RETURN, 0 );

sanyassh
  • 8,100
  • 13
  • 36
  • 70
PaulM
  • 21
  • 2