www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to catch a signal

reply W.Boeke <w.boeke upcmail.nl> writes:
I am trying to catch a signal from the OS, as follows:

int winch;

void set_winch (int sig) {
   enum SIGWINCH = 28;
   signal.signal(SIGWINCH,cast(void function(int))set_winch);
   winch = sig;
}

The SIGWINCH signal notifies a window resize. In C this works 
(without the cast), but in D I get a compilation error:

Error: function signal.set_winch(int sig) is not callable using 
argument types ()


What should be the right way to accomplish this?
Thanks in advance for your answer.

Wouter
Nov 09 2019
parent reply Dennis <dkorpel gmail.com> writes:
On Saturday, 9 November 2019 at 12:44:20 UTC, W.Boeke wrote:
 What should be the right way to accomplish this?
Put an ampersand before the function to get its address:
   signal.signal(SIGWINCH,cast(void function(int)) &set_winch);
In C you can omit the & when taking a function address, but when you do that in D it tries to call the function and cast the return value of the function instead.
Nov 09 2019
parent W.Boeke <w.boeke upcmail.nl> writes:
On Saturday, 9 November 2019 at 12:56:52 UTC, Dennis wrote:
 Put an ampersand before the function to get its address:
   signal.signal(SIGWINCH,cast(void function(int)) &set_winch);
In C you can omit the & when taking a function address, but when you do that in D it tries to call the function and cast the return value of the function instead.
Bingo! That worked. The modified code is: int winch; extern(C) void set_winch(int sig) nothrow nogc system { enum SIGWINCH = 28; signal.signal(SIGWINCH,&set_winch); winch = sig; } Wouter
Nov 09 2019