SetConsoleCtrlHandler issue

The following Swift code compile ok but when running on Windows the consoleHandler is not installed.
What's wrong ?

import Foundation

#if os(Windows)
import WinSDK
#endif

// Windows Ctrl-C handler

let consoleHandler: @convention(c) (UInt32) -> WindowsBool = { dwType in
    print("In consoleHandler")

    switch(dwType) {
    case UInt32(CTRL_C_EVENT):
        print("ctrl-c")
        Beep(750, 300)
        return true
    default:
        print("Some other event")
        return false
    }
}

if SetConsoleCtrlHandler(consoleHandler, true) == true {
    print("Unable to install handler!")
    exit(1) 
}

let input = readLine()
print(input)

Microsoft Reference: Registering a Control Handler Function - Windows Console | Microsoft Learn

Unless I am mistaken, SetConsoleCtrlHandler() returns false on failure, therefore it should be

if !SetConsoleCtrlHandler(consoleHandler, true)  {
    print("Unable to install handler!")
    exit(1) 
}

Your right. I mis interpreted the SetConsoleCtrlHandler ref doc.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

So of course, zero value is Swift false.

Note for the Swift Team and Dispatch Source on Windows:
My first attempt was to use Swift Dispatch Source functions that worked fine on macOS and Linux.

// macOS and Linux Ctrl-C handler
let queue = DispatchQueue(label: "myQueue")
signal(SIGINT, SIG_IGN)
let sourceSIGINT  = DispatchSource.makeSignalSource(signal: SIGINT, queue: queue)
sourceSIGINT.setEventHandler {
    print("In Ctrl-C handler")
    exit(0)
}
sourceSIGINT.activate()

While this code compile and run ok on macOS and Linux, when running on Windows, the CTRL-C is ignored as expected, but not handle by sourceSIGINT.setEventHandler.

Is there a bug in the Windows implementation of Dispatch Source or this is expected ?

The Windows SetConsoleCtrlHandler method is my workaround now but it will be nice to have Dispatch Source also working on Windows so the same Swift code works on all platforms.

Edit: I added the sourceSIGINT.activate() call, thanks to ibex10. Still not working on Windows however.

In order for it to work, on macOS, the above code needs a call to DispatchQueue.activate.

Run the following code from the command line to test.

@main
enum Ctrl_C_Handler {
    static func main () async {
        
        let queue = DispatchQueue(label: "myQueue")
        
        signal (SIGINT, SIG_IGN)
        let sourceSIGINT = DispatchSource.makeSignalSource (signal: SIGINT, queue: queue)
        
        sourceSIGINT.setEventHandler {
            print ("In Ctrl-C handler")
        }
        sourceSIGINT.activate()
        
        let minutes = 1.5
        print ("sleep for \(minutes) minutes...")
        do {
            try await Task.sleep (nanoseconds: UInt64 (60 * minutes * 1_000_000_000))
        }
        catch {
            print (error)
        }
    }
}

Your right. I forget the sourceSIGINT.activate() in the sample code. As I mentioned I used DispatchSource and work ok on macOS and Linux but not on Windows.