问题描述
是否有显示如何在 Delphi 控制台应用程序中处理 Ctrl+C 的最佳实践和代码片段?
Are there best practices and code snippets available which show how I can handle Ctrl+C in a Delphi console application?
我发现一些文章提供了一些关于调试器可能存在的问题的信息,包括异常处理、DLL 的卸载、标准输入的关闭和终结例如这个 CodeGear 论坛主题.
I have found some articles which give some information about possible problems with the debugger, with exception handling, unloading of DLLs, closing of stdin, and finalization for example this CodeGear forums thread.
推荐答案
来自 Windows API (MSDN):
From Windows API (MSDN):
BOOL WINAPI SetConsoleCtrlHandler(
PHANDLER_ROUTINE HandlerRoutine, // address of handler function
BOOL Add // handler to add or remove
);
HandlerRoutine 函数是控制台进程指定用于处理进程接收到的控制信号的函数.该函数可以有任何名称.
A HandlerRoutine function is a function that a console process specifies to handle control signals received by the process. The function can have any name.
BOOL WINAPI HandlerRoutine(
DWORD dwCtrlType // control signal type
);
在 Delphi 中,处理程序例程应该是这样的:
In the Delphi the handler routine should be like:
function console_handler( dwCtrlType: DWORD ): BOOL; stdcall;
begin
// Avoid terminating with Ctrl+C
if ( CTRL_C_EVENT = dwCtrlType ) then
result := TRUE
else
result := FALSE;
end;
这篇关于如何在 Delphi 控制台应用程序中处理 Ctrl+C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!