[fpc-pascal] How to use the `signals` unit?

silvioprog silvioprog at gmail.com
Wed Oct 21 20:12:14 CEST 2015


Hello,

What is the secret to use this it on Windows?

I have the following example in C:

==== begin code ===

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

void sigproc() {
    printf("You have pressed Ctrl-C\n");
    exit(0);
}

main()  {
    signal(SIGINT, sigproc);
    printf("Use Ctrl-C to quit\n");
    for(;;);
}

==== end code ===

So when I use the Ctrl+C command I get the "You have pressed Ctrl-C" in the
terminal.

However, using this code (compiled in FPC from trunk):

==== begin code ===

program project1;

{$mode objfpc}{$H+}

uses
  signals;

  function sigproc(v: LongInt): longint; cdecl;
  begin
    WriteLn('You have pressed Ctrl-C');
    Result := v;
    Halt(0);
  end;

begin
  signal(SIGINT, @sigproc);
  WriteLn('Use Ctrl-C to quit');
  while true do ;
end.

==== end code ===

It doesn't work. :-(

The program quits, but I can't get the "You have pressed Ctrl-C".

I can use the `Windows.SetConsoleCtrlHandler()`, but I would be happy to
understand how to use the `signals` unit. =)

-- 
Silvio Clécio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151021/57b40e71/attachment.html>


More information about the fpc-pascal mailing list