summaryrefslogtreecommitdiffstats
path: root/src/runtime/testdata/testwinlibsignal/main.c
blob: 37f24823e66a7c8d88fbd604dc2c28f8c1d62bca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <windows.h>
#include <stdio.h>

HANDLE waitForCtrlBreakEvent;

BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
    switch (fdwCtrlType)
    {
    case CTRL_BREAK_EVENT:
        SetEvent(waitForCtrlBreakEvent);
        return TRUE;
    default:
        return FALSE;
    }
}

int main(void)
{
    waitForCtrlBreakEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!waitForCtrlBreakEvent) {
        fprintf(stderr, "ERROR: Could not create event\n");
        return 1;
    }

    if (!SetConsoleCtrlHandler(CtrlHandler, TRUE))
    {
        fprintf(stderr, "ERROR: Could not set control handler\n");
        return 1;
    }

    // The library must be loaded after the SetConsoleCtrlHandler call
    // so that the library handler registers after the main program.
    // This way the library handler gets called first.
    HMODULE dummyDll = LoadLibrary("dummy.dll");
    if (!dummyDll) {
        fprintf(stderr, "ERROR: Could not load dummy.dll\n");
        return 1;
    }

    // Call the Dummy function so that Go initialization completes, since
    // all cgo entry points call out to _cgo_wait_runtime_init_done.
    if (((int(*)(void))GetProcAddress(dummyDll, "Dummy"))() != 42) {
        fprintf(stderr, "ERROR: Dummy function did not return 42\n");
        return 1;
    }

    printf("ready\n");
    fflush(stdout);

    if (WaitForSingleObject(waitForCtrlBreakEvent, 5000) != WAIT_OBJECT_0) {
        fprintf(stderr, "FAILURE: No signal received\n");
        return 1;
    }

    return 0;
}