How can I handle Ctrl + C in a Windows CE console application?

I need to do some cleaning before closing my application, but SetConsoleCtrlHandler does not seem to be available for Windows CE console applications.

Is there an alternative processing method for Ctrl+C in Windows CE 6?

+3
source share
2 answers

According to Microsoft documentation, in Windows CE 3.0 and above, the DeviceIoControl function, called with the IOCTL_CONSOLE_SETCONTROLCHANDLER control code, will install the Ctrl + C handler in Windows CE. I have not tried myself yet, but something like this "should" work:

 DWORD ignore; DeviceIoControl( _fileno(stdout), // handle to the console IOCTL_CONSOLE_SETCONTROLCHANDLER, // Tell Win CE to set the console Ctrl+C handler (LPVOID)consoleHandler, // pointer to the signal handler sizeof(consoleHandler), // size of the pointer NULL, // output buffer not needed 0, // zero output buffer size &ignore, // no data will be put into the output buffer so we don't need its size NULL); // not an asynchronous operation - don't need to provide async info 

where consoleHandler is of course your handler Ctrl + C.

Docs:

Required Headers:

  • Console.h
  • winbase.h (usually enabled through windows.h).
+5
source

I got this work on Windows Embedded Compact 7. Final events Ctrl + C and "windows closed."

  • Create a Win32 event.
  • Pass this DeviceIoControl () event with IOCTL_CONSOLE_SETCONTROLCEVENT and pass a console handle (e.g. _fileno (stdout)). This event will be signaled when you enter Ctrl + C or close the console window.
  • Create a thread that waits for the Win32 event to signal, and when it becomes such, it calls your handler Ctrl + C or performs your cleanup and probably exits the program.

Note that IOCTL_CONSOLE_SETCONTROLCHANDLER is deprecated, and DeviceIoControl () fails if it is given this IOCTL code.

0
source

Source: https://habr.com/ru/post/1387593/


All Articles