且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Delphi控制台应用程序中处理Ctrl + C?

更新时间:2023-02-17 21:25:55

从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;