none
how to prevent a console created with AllocConsole() RRS feed

  • Question

  • how to prevent a console created with AllocConsole()from the accidental closure of the program and reduce it to an icon only?(or better tray ico)

    AllocConsole();
    ShowWindow(GetConsoleWindow(), SW_HIDE);
    BindStdHandlesToConsole();

    there is no way to intercept the console messages

    Thanks


    Sunday, February 10, 2019 10:41 PM

All replies

  • There should be, I know the console has SetConsoleCtrlHandler and it will notify on console close using CTRL_CLOSE_EVENT.

    Maybe you could use that to detach from the console using FreeConsole.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Monday, February 11, 2019 1:05 AM
  • Unfortunately Windows will call ExitProcess after the handler function has processed the CTRL_CLOSE_EVENT regardless of whether the handler returns TRUE or FALSE.

    I played around with this on Windows 8.1 and Windows terminated the process when the handler function attempted to call FreeConsole.

    It also terminated the process while the handler function was was waiting for input from the console.

    It doesn't seem like there is a way to prevent the process from being terminated if the user closes the console.

    Of course, there is nothing to prevent a user from terminating a process in any number of ways.

    Perhaps setting the console's caption to a warning string would be helpful.

    Monday, February 11, 2019 12:13 PM
  • Yes i think you are right but like now i use this solution to prevent to close the main program using:

    // Disable X to windows console
    //...
    hMenu = GetSystemMenu(GetConsoleWindow(), FALSE);
    	RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
    	DrawMenuBar(hwnd);
    //...
    

    like now i have one main program showing only one tray icon and the menu associated to him...

    when i select from menu show console it show the console, but when i click the X to the console windows it kill the app, using that code i disable the X button

    and i add one hide function to tray menu..

    if some one have a better solution ...

    Monday, February 11, 2019 12:42 PM
  • The only other option that I have would be to use a separate process for the console application and communicate using IPC.

    While this would be more complex to write, it would be able to get what you want this way.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Monday, February 11, 2019 1:04 PM
  • Even though you are disabling the X you should still use SetConsoleCtrlHandler to install a handler function in order to catch Ctrl-C and Ctrl-Break.

    Otherwise, a user can still terminate the application by sending either of those to the open console.

    Monday, February 11, 2019 1:15 PM
  • Hi,

    Thank you for posting here.

    If the HandlerRoutine parameter is NULL, a TRUE value causes the calling process to ignore CTRL+C input, and a FALSE value restores normal processing of CTRL+C input. A console process adds or removes additional handler functions by calling the SetConsoleCtrlHandler function, which does not affect the list of handler functions for other processes. When a console process receives any of the control signals, its handler functions are called on a last-registered, first-called basis until one of the handlers returns TRUE. If none of the handlers returns TRUE, the default handler is called . The handler list for each process contains only a default handler function that calls the ExitProcess function.

    For more details , you could refer to the link:https://docs.microsoft.com/en-us/windows/console/setconsolectrlhandler

    Best wishes,

    Jeanine Zhang

    Tuesday, February 12, 2019 5:59 AM
    Moderator