(root)/
gcc-13.2.0/
libgo/
go/
runtime/
testdata/
testwinlibsignal/
main.c
       1  #include <windows.h>
       2  #include <stdio.h>
       3  
       4  HANDLE waitForCtrlBreakEvent;
       5  
       6  BOOL 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  
      18  int main(void)
      19  {
      20      waitForCtrlBreakEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
      21      if (!waitForCtrlBreakEvent) {
      22          fprintf(stderr, "ERROR: Could not create event\n");
      23          return 1;
      24      }
      25  
      26      if (!SetConsoleCtrlHandler(CtrlHandler, TRUE))
      27      {
      28          fprintf(stderr, "ERROR: Could not set control handler\n");
      29          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) {
      37          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");
      45          return 1;
      46      }
      47  
      48      printf("ready\n");
      49      fflush(stdout);
      50  
      51      if (WaitForSingleObject(waitForCtrlBreakEvent, 5000) != WAIT_OBJECT_0) {
      52          fprintf(stderr, "FAILURE: No signal received\n");
      53          return 1;
      54      }
      55  
      56      return 0;
      57  }