问题
I need to make some clean up before closing my application, but SetConsoleCtrlHandler
doesn't seem to be available for Windows CE console applications.
Is there any alternative method for handling Ctrl+C
in Windows CE 6?
回答1:
According to Microsoft's documentation, on Windows CE 3.0 and up, the DeviceIoControl
function called with the IOCTL_CONSOLE_SETCONTROLCHANDLER
control code will install a Ctrl+C handler on Windows CE. I haven't tried it out 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 Ctrl+C handler.
Docs:
- DeviceIoControl (CE 7)
- IOCTL_CONSOLE_SETCONTROLCHANDLER (CE .NET)
Headers needed:
Console.h
winbase.h
(usually included through windows.h).
回答2:
I got this working on Windows Embedded Compact 7. The Ctrl+C and the "window closed" events are both catched.
- Create a Win32 event.
- Pass that event to DeviceIoControl() using the IOCTL_CONSOLE_SETCONTROLCEVENT, and given the console handle (e.g., _fileno(stdout)). That event will be signaled when Ctrl+C is typed, or the console window is closed.
- Create a thread that waits on the Win32 event becoming signaled, and when it becomes so, calls your Ctrl+C handler or performs your cleanup, and probably exits the program.
Notice that IOCTL_CONSOLE_SETCONTROLCHANDLER has been deprecated and DeviceIoControl() fails when it is given that IOCTL code.
来源:https://stackoverflow.com/questions/1366879/how-can-i-handle-ctrlc-in-a-windows-ce-console-application