Win32 Console - How to recognize Close?

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
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
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.
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
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.
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.