home *** CD-ROM | disk | FTP | other *** search
- /*
- BREAK.C -- Tests a Windows Ctrl-Brk handler
-
- From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Build using: WINIOBC BREAK (for Borland C++ v3.00)
- WINIOMS BREAK (for Microsoft C/SDK)
- */
-
- #include <windows.h>
- #include "winio.h"
-
- WORD (FAR PASCAL *SetSigHandler)(FARPROC newSignalHandler,
- DWORD FAR * lpOldSignalHandlerAddress, WORD FAR *lpOldSignalType,
- WORD signalType, WORD mustBeOne);
-
- void (FAR PASCAL *DoSignal)(void);
-
- int volatile do_abort = 0;
-
- WORD FAR PASCAL _export ctrl_break_handler(WORD a, WORD b)
- {
- MessageBeep(0); // seems ok to call, though most APIs aren't
- return do_abort++;
- }
-
- main(int argc, char *argv[])
- {
- FARPROC ctrl_break;
- DWORD dwOldProc;
- WORD wOldType;
- int do_dosignal = (argc < 2) ;
- HANDLE hKernel = GetModuleHandle("KERNEL");
-
- winio_about("BREAK"
- "\nTests a Windows Ctrl-Brk handler"
- "\n\nFrom Chapter 5 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek"
- );
-
- if (do_dosignal)
- if (! (DoSignal = GetProcAddress(hKernel, "DOSIGNAL")))
- fail("Can't find DoSignal");
- if (! (SetSigHandler = GetProcAddress(hKernel, "SETSIGHANDLER")))
- fail("Can't find SetSigHandler");
- ctrl_break = MakeProcInstance((FARPROC) ctrl_break_handler, __hInst);
- SetSigHandler((FARPROC) ctrl_break,
- &dwOldProc, &wOldType, 2, 1); // install handler
- for (;;)
- {
- if (do_dosignal)
- {
- puts("This is a test of DoSignal!");
- DoSignal();
- }
- else
- puts("Hit Ctrl-Break!");
-
- if (do_abort) // signal handler was called
- {
- if (MessageBox(NULL, "Ctrl-Break Pressed",
- "Test of SetSigHandler",
- MB_ABORTRETRYIGNORE) == IDABORT)
- break; // abort
- else
- do_abort = 0; // retry, ignore
- }
- }
-
- FreeProcInstance(ctrl_break);
- puts("Program aborted");
- return 0;
- }
-
-