Prevent application from closing when closing console












0















I have a WinAPI/Win32 application that also opens a console window (with debugging purposes) before the main window is opened). I added a safe check for when the main window gets its X button pressed it asks the "Are you sure?" thing. However, if I click X on the console, it kills the application right away without asking anything. Is there any way to prevent that?
Here is a snippet of my code:



int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
EnableDebug();
WNDCLASSA MainWindow = { 0 };
MainWindow.hbrBackground = (HBRUSH) COLOR_WINDOW;
MainWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
MainWindow.hInstance = hInst;
MainWindow.lpszClassName = "WindowClass";
MainWindow.lpfnWndProc = WndProc;
ATOM result = RegisterClassA(&MainWindow);
if (!result)
{
MessageBoxA(NULL, "Failed to register window class", "Error", MB_OK);
return -1;
}
MSG msg = { 0 };
//here the app goes on


//here is the start of the debug function
void EnableDebug(){
if (AllocConsole() == 0)
{
MessageBoxA(NULL, "Unable to create a debug window!", "Error", MB_OK);
return;
}
freopen("CONOUT$", "w", stderr);
SetConsoleTitleA("Debug Window");
clog.clear();









share|improve this question























  • Start your program in the background in 1st place?

    – πάντα ῥεῖ
    Nov 24 '18 at 21:45











  • What value is that incomplete, useless snippet of code supposed to provide?

    – Ken White
    Nov 24 '18 at 21:53






  • 1





    What problem are you trying to solve? This question is merely asking, how to fix your proposed solution.

    – IInspectable
    Nov 24 '18 at 22:00











  • it seems you didn't understand the question. The snippet of code shows how I start my application and the console window. What I am asking is how to make the application not close when I close the console.

    – DarkAtom
    Nov 24 '18 at 22:24






  • 1





    if you create console only for debugging purposes - you easy can create usual window in your process (say create separate thread, WC_EDIT windows) and printf to it for debugging

    – RbMm
    Nov 25 '18 at 9:04
















0















I have a WinAPI/Win32 application that also opens a console window (with debugging purposes) before the main window is opened). I added a safe check for when the main window gets its X button pressed it asks the "Are you sure?" thing. However, if I click X on the console, it kills the application right away without asking anything. Is there any way to prevent that?
Here is a snippet of my code:



int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
EnableDebug();
WNDCLASSA MainWindow = { 0 };
MainWindow.hbrBackground = (HBRUSH) COLOR_WINDOW;
MainWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
MainWindow.hInstance = hInst;
MainWindow.lpszClassName = "WindowClass";
MainWindow.lpfnWndProc = WndProc;
ATOM result = RegisterClassA(&MainWindow);
if (!result)
{
MessageBoxA(NULL, "Failed to register window class", "Error", MB_OK);
return -1;
}
MSG msg = { 0 };
//here the app goes on


//here is the start of the debug function
void EnableDebug(){
if (AllocConsole() == 0)
{
MessageBoxA(NULL, "Unable to create a debug window!", "Error", MB_OK);
return;
}
freopen("CONOUT$", "w", stderr);
SetConsoleTitleA("Debug Window");
clog.clear();









share|improve this question























  • Start your program in the background in 1st place?

    – πάντα ῥεῖ
    Nov 24 '18 at 21:45











  • What value is that incomplete, useless snippet of code supposed to provide?

    – Ken White
    Nov 24 '18 at 21:53






  • 1





    What problem are you trying to solve? This question is merely asking, how to fix your proposed solution.

    – IInspectable
    Nov 24 '18 at 22:00











  • it seems you didn't understand the question. The snippet of code shows how I start my application and the console window. What I am asking is how to make the application not close when I close the console.

    – DarkAtom
    Nov 24 '18 at 22:24






  • 1





    if you create console only for debugging purposes - you easy can create usual window in your process (say create separate thread, WC_EDIT windows) and printf to it for debugging

    – RbMm
    Nov 25 '18 at 9:04














0












0








0








I have a WinAPI/Win32 application that also opens a console window (with debugging purposes) before the main window is opened). I added a safe check for when the main window gets its X button pressed it asks the "Are you sure?" thing. However, if I click X on the console, it kills the application right away without asking anything. Is there any way to prevent that?
Here is a snippet of my code:



int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
EnableDebug();
WNDCLASSA MainWindow = { 0 };
MainWindow.hbrBackground = (HBRUSH) COLOR_WINDOW;
MainWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
MainWindow.hInstance = hInst;
MainWindow.lpszClassName = "WindowClass";
MainWindow.lpfnWndProc = WndProc;
ATOM result = RegisterClassA(&MainWindow);
if (!result)
{
MessageBoxA(NULL, "Failed to register window class", "Error", MB_OK);
return -1;
}
MSG msg = { 0 };
//here the app goes on


//here is the start of the debug function
void EnableDebug(){
if (AllocConsole() == 0)
{
MessageBoxA(NULL, "Unable to create a debug window!", "Error", MB_OK);
return;
}
freopen("CONOUT$", "w", stderr);
SetConsoleTitleA("Debug Window");
clog.clear();









share|improve this question














I have a WinAPI/Win32 application that also opens a console window (with debugging purposes) before the main window is opened). I added a safe check for when the main window gets its X button pressed it asks the "Are you sure?" thing. However, if I click X on the console, it kills the application right away without asking anything. Is there any way to prevent that?
Here is a snippet of my code:



int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
EnableDebug();
WNDCLASSA MainWindow = { 0 };
MainWindow.hbrBackground = (HBRUSH) COLOR_WINDOW;
MainWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
MainWindow.hInstance = hInst;
MainWindow.lpszClassName = "WindowClass";
MainWindow.lpfnWndProc = WndProc;
ATOM result = RegisterClassA(&MainWindow);
if (!result)
{
MessageBoxA(NULL, "Failed to register window class", "Error", MB_OK);
return -1;
}
MSG msg = { 0 };
//here the app goes on


//here is the start of the debug function
void EnableDebug(){
if (AllocConsole() == 0)
{
MessageBoxA(NULL, "Unable to create a debug window!", "Error", MB_OK);
return;
}
freopen("CONOUT$", "w", stderr);
SetConsoleTitleA("Debug Window");
clog.clear();






c++ winapi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 21:38









DarkAtomDarkAtom

214




214













  • Start your program in the background in 1st place?

    – πάντα ῥεῖ
    Nov 24 '18 at 21:45











  • What value is that incomplete, useless snippet of code supposed to provide?

    – Ken White
    Nov 24 '18 at 21:53






  • 1





    What problem are you trying to solve? This question is merely asking, how to fix your proposed solution.

    – IInspectable
    Nov 24 '18 at 22:00











  • it seems you didn't understand the question. The snippet of code shows how I start my application and the console window. What I am asking is how to make the application not close when I close the console.

    – DarkAtom
    Nov 24 '18 at 22:24






  • 1





    if you create console only for debugging purposes - you easy can create usual window in your process (say create separate thread, WC_EDIT windows) and printf to it for debugging

    – RbMm
    Nov 25 '18 at 9:04



















  • Start your program in the background in 1st place?

    – πάντα ῥεῖ
    Nov 24 '18 at 21:45











  • What value is that incomplete, useless snippet of code supposed to provide?

    – Ken White
    Nov 24 '18 at 21:53






  • 1





    What problem are you trying to solve? This question is merely asking, how to fix your proposed solution.

    – IInspectable
    Nov 24 '18 at 22:00











  • it seems you didn't understand the question. The snippet of code shows how I start my application and the console window. What I am asking is how to make the application not close when I close the console.

    – DarkAtom
    Nov 24 '18 at 22:24






  • 1





    if you create console only for debugging purposes - you easy can create usual window in your process (say create separate thread, WC_EDIT windows) and printf to it for debugging

    – RbMm
    Nov 25 '18 at 9:04

















Start your program in the background in 1st place?

– πάντα ῥεῖ
Nov 24 '18 at 21:45





Start your program in the background in 1st place?

– πάντα ῥεῖ
Nov 24 '18 at 21:45













What value is that incomplete, useless snippet of code supposed to provide?

– Ken White
Nov 24 '18 at 21:53





What value is that incomplete, useless snippet of code supposed to provide?

– Ken White
Nov 24 '18 at 21:53




1




1





What problem are you trying to solve? This question is merely asking, how to fix your proposed solution.

– IInspectable
Nov 24 '18 at 22:00





What problem are you trying to solve? This question is merely asking, how to fix your proposed solution.

– IInspectable
Nov 24 '18 at 22:00













it seems you didn't understand the question. The snippet of code shows how I start my application and the console window. What I am asking is how to make the application not close when I close the console.

– DarkAtom
Nov 24 '18 at 22:24





it seems you didn't understand the question. The snippet of code shows how I start my application and the console window. What I am asking is how to make the application not close when I close the console.

– DarkAtom
Nov 24 '18 at 22:24




1




1





if you create console only for debugging purposes - you easy can create usual window in your process (say create separate thread, WC_EDIT windows) and printf to it for debugging

– RbMm
Nov 25 '18 at 9:04





if you create console only for debugging purposes - you easy can create usual window in your process (say create separate thread, WC_EDIT windows) and printf to it for debugging

– RbMm
Nov 25 '18 at 9:04












2 Answers
2






active

oldest

votes


















0














I believe you need to call SetConsoleCtrlHandler to provide a handler routine to handle the close event. Something like this:



BOOL WINAPI MyCtrlHandler (DWORD dwCtrlType)
{
if (dwCtrlType == CTRL_CLOSE_EVENT)
...
}

SetConsoleCtrlHandler (MyCtrlHandler, TRUE);


You probably want to handle various values of dwCtrlType in various ways. Consult the documentation for details.






share|improve this answer
























  • unfortunately this is not work. event if you not exit from app on close console event, after several seconds process will be force terminated external

    – RbMm
    Nov 24 '18 at 22:26











  • @RbMm OK, thanks. That's sad.

    – Paul Sanders
    Nov 24 '18 at 22:27











  • csrss.exe external call TerminateProcess with STATUS_CONTROL_C_EXIT - stackoverflow.com/questions/26404907/…

    – RbMm
    Nov 24 '18 at 22:39













  • disabling the X button on the console could also be an option, but I think that is not possible.

    – DarkAtom
    Nov 24 '18 at 23:09











  • @DarkAtom It is possible, see my answer.

    – zett42
    Nov 25 '18 at 16:39



















0














You can disable the close button of the console window to prevent accidentally terminating your application:



if( HWND hwnd = GetConsoleWindow() )
{
if( HMENU hMenu = GetSystemMenu( hwnd, FALSE ) )
{
EnableMenuItem( hMenu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED );
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462590%2fprevent-application-from-closing-when-closing-console%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I believe you need to call SetConsoleCtrlHandler to provide a handler routine to handle the close event. Something like this:



    BOOL WINAPI MyCtrlHandler (DWORD dwCtrlType)
    {
    if (dwCtrlType == CTRL_CLOSE_EVENT)
    ...
    }

    SetConsoleCtrlHandler (MyCtrlHandler, TRUE);


    You probably want to handle various values of dwCtrlType in various ways. Consult the documentation for details.






    share|improve this answer
























    • unfortunately this is not work. event if you not exit from app on close console event, after several seconds process will be force terminated external

      – RbMm
      Nov 24 '18 at 22:26











    • @RbMm OK, thanks. That's sad.

      – Paul Sanders
      Nov 24 '18 at 22:27











    • csrss.exe external call TerminateProcess with STATUS_CONTROL_C_EXIT - stackoverflow.com/questions/26404907/…

      – RbMm
      Nov 24 '18 at 22:39













    • disabling the X button on the console could also be an option, but I think that is not possible.

      – DarkAtom
      Nov 24 '18 at 23:09











    • @DarkAtom It is possible, see my answer.

      – zett42
      Nov 25 '18 at 16:39
















    0














    I believe you need to call SetConsoleCtrlHandler to provide a handler routine to handle the close event. Something like this:



    BOOL WINAPI MyCtrlHandler (DWORD dwCtrlType)
    {
    if (dwCtrlType == CTRL_CLOSE_EVENT)
    ...
    }

    SetConsoleCtrlHandler (MyCtrlHandler, TRUE);


    You probably want to handle various values of dwCtrlType in various ways. Consult the documentation for details.






    share|improve this answer
























    • unfortunately this is not work. event if you not exit from app on close console event, after several seconds process will be force terminated external

      – RbMm
      Nov 24 '18 at 22:26











    • @RbMm OK, thanks. That's sad.

      – Paul Sanders
      Nov 24 '18 at 22:27











    • csrss.exe external call TerminateProcess with STATUS_CONTROL_C_EXIT - stackoverflow.com/questions/26404907/…

      – RbMm
      Nov 24 '18 at 22:39













    • disabling the X button on the console could also be an option, but I think that is not possible.

      – DarkAtom
      Nov 24 '18 at 23:09











    • @DarkAtom It is possible, see my answer.

      – zett42
      Nov 25 '18 at 16:39














    0












    0








    0







    I believe you need to call SetConsoleCtrlHandler to provide a handler routine to handle the close event. Something like this:



    BOOL WINAPI MyCtrlHandler (DWORD dwCtrlType)
    {
    if (dwCtrlType == CTRL_CLOSE_EVENT)
    ...
    }

    SetConsoleCtrlHandler (MyCtrlHandler, TRUE);


    You probably want to handle various values of dwCtrlType in various ways. Consult the documentation for details.






    share|improve this answer













    I believe you need to call SetConsoleCtrlHandler to provide a handler routine to handle the close event. Something like this:



    BOOL WINAPI MyCtrlHandler (DWORD dwCtrlType)
    {
    if (dwCtrlType == CTRL_CLOSE_EVENT)
    ...
    }

    SetConsoleCtrlHandler (MyCtrlHandler, TRUE);


    You probably want to handle various values of dwCtrlType in various ways. Consult the documentation for details.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 22:01









    Paul SandersPaul Sanders

    5,2512621




    5,2512621













    • unfortunately this is not work. event if you not exit from app on close console event, after several seconds process will be force terminated external

      – RbMm
      Nov 24 '18 at 22:26











    • @RbMm OK, thanks. That's sad.

      – Paul Sanders
      Nov 24 '18 at 22:27











    • csrss.exe external call TerminateProcess with STATUS_CONTROL_C_EXIT - stackoverflow.com/questions/26404907/…

      – RbMm
      Nov 24 '18 at 22:39













    • disabling the X button on the console could also be an option, but I think that is not possible.

      – DarkAtom
      Nov 24 '18 at 23:09











    • @DarkAtom It is possible, see my answer.

      – zett42
      Nov 25 '18 at 16:39



















    • unfortunately this is not work. event if you not exit from app on close console event, after several seconds process will be force terminated external

      – RbMm
      Nov 24 '18 at 22:26











    • @RbMm OK, thanks. That's sad.

      – Paul Sanders
      Nov 24 '18 at 22:27











    • csrss.exe external call TerminateProcess with STATUS_CONTROL_C_EXIT - stackoverflow.com/questions/26404907/…

      – RbMm
      Nov 24 '18 at 22:39













    • disabling the X button on the console could also be an option, but I think that is not possible.

      – DarkAtom
      Nov 24 '18 at 23:09











    • @DarkAtom It is possible, see my answer.

      – zett42
      Nov 25 '18 at 16:39

















    unfortunately this is not work. event if you not exit from app on close console event, after several seconds process will be force terminated external

    – RbMm
    Nov 24 '18 at 22:26





    unfortunately this is not work. event if you not exit from app on close console event, after several seconds process will be force terminated external

    – RbMm
    Nov 24 '18 at 22:26













    @RbMm OK, thanks. That's sad.

    – Paul Sanders
    Nov 24 '18 at 22:27





    @RbMm OK, thanks. That's sad.

    – Paul Sanders
    Nov 24 '18 at 22:27













    csrss.exe external call TerminateProcess with STATUS_CONTROL_C_EXIT - stackoverflow.com/questions/26404907/…

    – RbMm
    Nov 24 '18 at 22:39







    csrss.exe external call TerminateProcess with STATUS_CONTROL_C_EXIT - stackoverflow.com/questions/26404907/…

    – RbMm
    Nov 24 '18 at 22:39















    disabling the X button on the console could also be an option, but I think that is not possible.

    – DarkAtom
    Nov 24 '18 at 23:09





    disabling the X button on the console could also be an option, but I think that is not possible.

    – DarkAtom
    Nov 24 '18 at 23:09













    @DarkAtom It is possible, see my answer.

    – zett42
    Nov 25 '18 at 16:39





    @DarkAtom It is possible, see my answer.

    – zett42
    Nov 25 '18 at 16:39













    0














    You can disable the close button of the console window to prevent accidentally terminating your application:



    if( HWND hwnd = GetConsoleWindow() )
    {
    if( HMENU hMenu = GetSystemMenu( hwnd, FALSE ) )
    {
    EnableMenuItem( hMenu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED );
    }
    }





    share|improve this answer




























      0














      You can disable the close button of the console window to prevent accidentally terminating your application:



      if( HWND hwnd = GetConsoleWindow() )
      {
      if( HMENU hMenu = GetSystemMenu( hwnd, FALSE ) )
      {
      EnableMenuItem( hMenu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED );
      }
      }





      share|improve this answer


























        0












        0








        0







        You can disable the close button of the console window to prevent accidentally terminating your application:



        if( HWND hwnd = GetConsoleWindow() )
        {
        if( HMENU hMenu = GetSystemMenu( hwnd, FALSE ) )
        {
        EnableMenuItem( hMenu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED );
        }
        }





        share|improve this answer













        You can disable the close button of the console window to prevent accidentally terminating your application:



        if( HWND hwnd = GetConsoleWindow() )
        {
        if( HMENU hMenu = GetSystemMenu( hwnd, FALSE ) )
        {
        EnableMenuItem( hMenu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED );
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 16:38









        zett42zett42

        7,3733740




        7,3733740






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462590%2fprevent-application-from-closing-when-closing-console%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python