Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

How to handle closing console window?

jirina
New Contributor I
973 Views
I am trying to handle various control signals when my application is running calculations. For historical reasons, I am not calling SetConsoleCtrlHandler directly from the Fortran code, but I have a DLL written in C++ and I call a function from this DLL.

This is what I have in Fortran:
[fxfortran]         integer*4 function InitSignalProcessing ( )
         !dec$ attributes stdcall, dllimport, decorate, alias: "InitSignalProcessing" :: InitSignalProcessing
         end function
         ...
         status = InitSignalProcessing ( )[/fxfortran]
C++ code is:
[cpp]__declspec(dllexport) int __stdcall InitSignalProcessing( void ) {
	return (int) SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE );
}

BOOL CtrlHandler( DWORD fdwCtrlType )
{
	BOOL ret = TRUE;

	switch ( fdwCtrlType ) {
		case CTRL_C_EVENT:
			printf( "\n\n Stopping due to Ctrl-C.\n" );
			ret = TRUE;

		case CTRL_CLOSE_EVENT:
			printf( "\n\n Stopping due to closing the window.\n" );
			ret = TRUE;
		
		case CTRL_BREAK_EVENT:
			printf( "\n\n Stopping due to Ctrl-Break.\n" );
			ret = TRUE;

		// Pass other signals to the next handler - ret = FALSE

		case CTRL_LOGOFF_EVENT:
			printf( "\n\n Stopping due to logoff.\n" );
			ret = FALSE;

		case CTRL_SHUTDOWN_EVENT:
			printf( "\n\n Stopping due to system shutdown.\n" );
			ret = FALSE;
		
		default:
			printf( "\n\n Stopping due to unknown event.\n" );
			ret = FALSE;
	}

	return ret;
}[/cpp]
I am particularly interested in CTRL_CLOSE_EVENT. After I click the close button of my application's console window, I get the message "Stopping due to closing the window", see the code above, and I am offered to end the task; however, this works only in Windows XP. In Windows 7, the console window is immediately closed, so it loooks like the corresponding signal is not handled.

To confirm this unwanted functionality, I wrote a small application based on IanH's code from this thread, and the behavior is the same - the console window shows the message, but is closed immediately.

Could it be that my Windows 7 are 64-bit? Or is there any other reason why the console window does not wait for user's input after the user closes it?
0 Kudos
1 Reply
Steven_L_Intel1
Employee
973 Views
I don't have an answer for you, but you might read through this thread which is trying to do something similar.
0 Kudos
Reply