blob: 37f24823e66a7c8d88fbd604dc2c28f8c1d62bca [file] [log] [blame]
martin57568082019-12-12 16:03:04 -08001#include <windows.h>
2#include <stdio.h>
3
4HANDLE waitForCtrlBreakEvent;
5
6BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
7{
8 switch (fdwCtrlType)
9 {
10 case CTRL_BREAK_EVENT:
11 SetEvent(waitForCtrlBreakEvent);
12 return TRUE;
13 default:
14 return FALSE;
15 }
16}
17
18int main(void)
19{
20 waitForCtrlBreakEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
21 if (!waitForCtrlBreakEvent) {
Jason A. Donenfeld7e63c8b2021-05-21 12:01:39 +020022 fprintf(stderr, "ERROR: Could not create event\n");
martin57568082019-12-12 16:03:04 -080023 return 1;
24 }
25
26 if (!SetConsoleCtrlHandler(CtrlHandler, TRUE))
27 {
Jason A. Donenfeld7e63c8b2021-05-21 12:01:39 +020028 fprintf(stderr, "ERROR: Could not set control handler\n");
martin57568082019-12-12 16:03:04 -080029 return 1;
30 }
31
32 // The library must be loaded after the SetConsoleCtrlHandler call
33 // so that the library handler registers after the main program.
34 // This way the library handler gets called first.
35 HMODULE dummyDll = LoadLibrary("dummy.dll");
36 if (!dummyDll) {
Jason A. Donenfeld7e63c8b2021-05-21 12:01:39 +020037 fprintf(stderr, "ERROR: Could not load dummy.dll\n");
38 return 1;
39 }
40
41 // Call the Dummy function so that Go initialization completes, since
42 // all cgo entry points call out to _cgo_wait_runtime_init_done.
43 if (((int(*)(void))GetProcAddress(dummyDll, "Dummy"))() != 42) {
44 fprintf(stderr, "ERROR: Dummy function did not return 42\n");
martin57568082019-12-12 16:03:04 -080045 return 1;
46 }
47
48 printf("ready\n");
49 fflush(stdout);
50
51 if (WaitForSingleObject(waitForCtrlBreakEvent, 5000) != WAIT_OBJECT_0) {
Jason A. Donenfeld7e63c8b2021-05-21 12:01:39 +020052 fprintf(stderr, "FAILURE: No signal received\n");
martin57568082019-12-12 16:03:04 -080053 return 1;
54 }
55
56 return 0;
57}