How handle exit event in a console application.

  • Thread starter Thread starter Jean-Francois Hamelin
  • Start date Start date
J

Jean-Francois Hamelin

Hi,

I have an console application, in the main when all my initializations are
done I wait for a user input to terminate the application. Since the
application is a console the user and click on the X to close the console,
when this occurs I have not able to gracefully shutdown my applications,
this part is important for my application.

Is there some event I can trap to handle this situation?

JF
 
Hello,

Thanks for your post. You can handle exit event with a function registered
in SetConsoleCtrlHandler(). Please refer to the following MSDN article for
detailed information:

SetConsoleCtrlHandler
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas
e/setconsolectrlhandler.asp

Console Event Handling
http://www.codeproject.com/win32/console_event_handling.asp

If it's a VB .NET or C# console application, you should call this native
API via P/Invoke.

Consuming Unmanaged DLL Functions
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconConsumingUnmanagedDLLFunctions.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top