Win32 Console - How to recognize Close?

Jan 10, 2013 at 9:11pm
Hello, I'm writing a TCP console server using boost::asio ( http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio.html)
Since I tried to follow the given examples/tutorials on the boost website I ended up having a similar (if not the same) main function.

However some calls like io_service.run(); are blocking the program until "all work is done" which in my case means that it runs infinitely until the program/console is being closed.

The problem I having here is that the program doesn't properly when I close it. I would expect him to return from the blocking function and finish the main function to get to the return 0; statement.
But he never gets there, the console window gets closed and the mentioned code part is never accessed. Instead he quits with this code:
has exited with code -1073741510 (0xc000013a).

Now besides the fact I want him to exit properly I also need him to access some code after the blocking function since I need to cleanup some objects (as well as closing files).

I was hoping someone could give me a hint how to solve this problem or tell me if there is some way to know when the console window gets closed so I can signal the blocking call that he has to stop blocking.
Last edited on Jan 10, 2013 at 9:14pm
Jan 10, 2013 at 10:32pm
I don't know the full answer to your question, but the GetConsoleWindow() API function returns the HWND of the console window.
Last edited on Jan 10, 2013 at 10:33pm
Jan 10, 2013 at 11:00pm
Now besides the fact I want him to exit properly I also need him to access some code after the blocking function since I need to cleanup some objects (as well as closing files).

This will happen by itself when the program closes, under any circumstance. I recommend having a separate thread deal with io_service.run().

When you close the program via the little x, it just exits immediately. The OS then deals with cleanup.
Jan 10, 2013 at 11:58pm
You mention you're followed the example code. Does this include using the console ctrl handler (set using SetConsoleCtrlHandler) as shown in this partilcular example?

http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/server/win_main.cpp

The handler should call the stop() method before the app is told to exit.

Andy

PS SetConsoleCtrlHandler function (Windows)
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms686016%28v=vs.85%29.aspx
Last edited on Jan 11, 2013 at 12:08am
Jan 11, 2013 at 10:31am
Odd, the SetConsoleCtrlHandler example seems to be disappeared in the newer boost version pages. But it seems to be shown there in a different way, I'll try out both and see what suits me better ;)

Anyway, thanks for pointing me there, I guess I must have overlooked that specific example.
Jan 11, 2013 at 10:26pm
I guess the approach in example I pointed you at has been superseded by the boost::asio::signal_set mechanism (Boost 1.47 and later)

Andy
Topic archived. No new replies allowed.