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

不羁的心 提交于 2019-12-10 20:13:49

问题


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.

  1. Create a Win32 event.
  2. 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.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!