Board index » delphi » problem: SetConsoleCtrlHandler

problem: SetConsoleCtrlHandler

hiho

the following code shows how I want to close an console application ..
but the "HandlerRoutine" is not called if I close the console by using
the system-menue of the console-window or press CTRL+C etc.
(how it the W32-API-Help says)

after the initialization phase the maintread of the process runs the
function MsgHandler and is waiting for any windows-messages..

what is wrong or what can can I do

thx

Steffen Kloessel
germany
************************************************************************

unit ...

interface

implementation

.
.
.

function MsgHandler : Integer;
var Msg  : TMSG;
    hWnd : THandle;
    Finish : Boolean;
begin
  repeat
    GetMessage(Msg, GetCurrentProcess, 0,0 );
    finish := Msg.message = WM_QUIT;
  until Finish;
end;

function HandlerRoutine(
    dwCtrlType : DWORD  //  control signal type
   ) : Boolean; cdecl;
begin
  result := TRUE;
  case dwCtrlType of
    CTRL_CLOSE_EVENT,
    CTRL_C_EVENT,
    CTRL_BREAK_EVENT : begin
      PostThreadMessage(GetCurrentThreadID,WM_QUIT,0,0);
    end;
    else result := FALSE;
  end;
end;

initialization

  SetConsoleCtrlHandler(
    @HandlerRoutine,    // address of handler function
    TRUE                // handler to add or remove
   );
finalization
  SetConsoleCtrlHandler(
    @HandlerRoutine,    // address of handler function
    FALSE               // handler to add or remove
   );
end.

************************************************************************

 

Re:problem: SetConsoleCtrlHandler


Just a thought: Shouldn't your handler routine be tagged as stdcall, not
cdecl ??

Good luck,
Stephane

Re:problem: SetConsoleCtrlHandler


I tried all (std/safe/...)... but no calling convention runs :-(

*looks sad*

but thx

Steffen again

Re:problem: SetConsoleCtrlHandler


On Fri, 07 Apr 2000 14:01:09 +0200, Steffen Kloessel

Quote
<skloe...@htwm.de> wrote:
>the following code shows how I want to close an console application ..
>but the "HandlerRoutine" is not called if I close the console by using
>the system-menue of the console-window or press CTRL+C etc.

This code works for me:

 function Handler(dwCtrlType: DWORD): BOOL; stdcall;
   begin
   Result := True;
   case dwCtrlType of
     CTRL_C_EVENT:
       ShowMessage('Ctrl-C');
     CTRL_BREAK_EVENT:
       ShowMessage('Break');
     CTRL_CLOSE_EVENT:
       ShowMessage('Close');
     CTRL_LOGOFF_EVENT:
       ShowMessage('Logoff');
     CTRL_SHUTDOWN_EVENT:
       ShowMessage('Shutdown');
     else
       MessageBeep(0) end end;

 procedure TForm1.Button1Click(Sender: TObject);
   begin
   SetConsoleCtrlHandler(@Handler, True) end;

-Steve

Re:problem: SetConsoleCtrlHandler


Re:problem: SetConsoleCtrlHandler


I retried it but it does not work .. I can see my brain-death not far
away from here ..
I insall the handler like you but in the initialization part of the unit
but the handler will not called by the system ..

*smile & cu*

Steffen

Quote
"Steve Schafer (TeamB)" wrote:

> On Fri, 07 Apr 2000 14:01:09 +0200, Steffen Kloessel
> <skloe...@htwm.de> wrote:

> >the following code shows how I want to close an console application ..
> >but the "HandlerRoutine" is not called if I close the console by using
> >the system-menue of the console-window or press CTRL+C etc.

> This code works for me:

>  function Handler(dwCtrlType: DWORD): BOOL; stdcall;
>    begin
>    Result := True;
>    case dwCtrlType of
>      CTRL_C_EVENT:
>        ShowMessage('Ctrl-C');
>      CTRL_BREAK_EVENT:
>        ShowMessage('Break');
>      CTRL_CLOSE_EVENT:
>        ShowMessage('Close');
>      CTRL_LOGOFF_EVENT:
>        ShowMessage('Logoff');
>      CTRL_SHUTDOWN_EVENT:
>        ShowMessage('Shutdown');
>      else
>        MessageBeep(0) end end;

>  procedure TForm1.Button1Click(Sender: TObject);
>    begin
>    SetConsoleCtrlHandler(@Handler, True) end;

> -Steve

Re:problem: SetConsoleCtrlHandler


On Sat, 08 Apr 2000 14:48:33 +0200, Steffen Kloessel

Quote
<skloe...@htwm.de> wrote:
>I insall the handler like you but in the initialization part of the unit
>but the handler will not called by the system ..

Have you tried the code EXACTLY as I showed it (that is, by installing
the handler from a button click)?

-Steve

Re:problem: SetConsoleCtrlHandler


Re:problem: SetConsoleCtrlHandler


hi Steve Schafer (TeamB)

I've found the problem .. I tried to install the handler in the
initialization-part of my unit .. but there the console was'nt still
allocated by the process.

your button-click was the cause (food) for thought

now it works .. :-) *feels lucky*
thanks
Steffen
germany

Other Threads