home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l430 / 1.ddi / CHAP6.ZIP / STSYSTMR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.9 KB  |  99 lines

  1. /*
  2.     STSYSTMR.C -- Illustrates access to the reserved 'System' timers
  3.  
  4.     From Chapter 6 of "Undocumented Windows" (Addison-Wesley 1992)
  5.     by Andrew Schulman, Dave Maxey and Matt Pietrek
  6.  
  7.     Build using: WINIOBC STSYSTMR (for Borland C++ v3.00)
  8.                  WINIOMS STSYSTMR (for Microsoft C/SDK)
  9. */
  10.  
  11. #include <windows.h> 
  12. #include "wmhandlr.h" 
  13. #include "winio.h" 
  14.  
  15. /* undocumented function */ 
  16. WORD (FAR PASCAL *lpfnSetSystemTimer)(HWND hwnd, int nIDEvent,
  17.                             WORD wElapse, FARPROC lpTimerFunction);
  18. BOOL (FAR PASCAL *lpfnKillSystemTimer)(HWND hwnd, WORD wTimer);
  19.  
  20. #define WM_SYSTIMER         0x0118
  21.  
  22. #define MAXTABLE 64         // Overgenerous..!
  23.  
  24. WORD hSysTimer;
  25. WORD hRegTimer[MAXTABLE] = {0};
  26.  
  27. long do_systimer(HWND hWnd, WORD wMsg,
  28.                             WORD nIDEvent, DWORD lParam)
  29.     {
  30.     printf("hWnd=0x%04X, wMsg=0x%04X, nIDEvent=0x%04X, lParam=0x%08lX\n",
  31.                 hWnd, wMsg, nIDEvent, lParam);
  32.     return 0;
  33.     }
  34.  
  35. BOOL ResetTimer(HWND hwnd)
  36.     {
  37.     if (hSysTimer)
  38.         (*lpfnKillSystemTimer)(hwnd, hSysTimer);
  39.     return TRUE;
  40.     }
  41.  
  42.  
  43. int main()
  44.     {
  45.     int i;
  46.     
  47.     winio_about("STSYSTMR"
  48.         "\nIllustrates access to the reserved 'System' timers"
  49.         "\n\nFrom Chapter 6 of"
  50.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  51.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  52.         );
  53.  
  54.     (FARPROC) lpfnSetSystemTimer =
  55.         GetProcAddress(GetModuleHandle("USER"), (LPSTR) (DWORD) 11);
  56.     (FARPROC) lpfnKillSystemTimer =
  57.         GetProcAddress(GetModuleHandle("USER"), (LPSTR) (DWORD) 182);
  58.  
  59.     if ((! lpfnSetSystemTimer) || (! lpfnKillSystemTimer))
  60.         {
  61.         printf("Could not locate Set/KillSystemTimer functions.\n");
  62.         return 0;
  63.         }
  64.  
  65.     puts("Allocating timers...");
  66.     
  67.     for (i = 0; i < MAXTABLE; i++)
  68.         {
  69.         if (! (hRegTimer[i] = SetTimer(__hMainWnd, i+1, 10000,
  70.                     (FARPROC) 0L)))
  71.             break;
  72.         }
  73.     printf("\nUsed SetTimer() to allocate all timers\n"
  74.         "(there were %d available).\n\n"
  75.         "We can still allocate a timer, however, using\n"
  76.         "SetSystemTimer(). An event will be generated\n"
  77.         "every second.\n"
  78.         "Close the window to terminate.\n\n", i);
  79.  
  80.     wmhandler_set(__hMainWnd, WM_SYSTIMER, (WMHANDLER) do_systimer);
  81.     
  82.     if ((hSysTimer = (*lpfnSetSystemTimer)(__hMainWnd, 0xDADA,
  83.                                 1000, NULL)) == NULL)
  84.         printf("Could not create system timer.\n");
  85.     else
  86.         printf("Timer handle is %04X\n\n", hSysTimer);
  87.  
  88.     printf("Freeing up %d regular timers.\n", i);
  89.     for (i = 0; i < MAXTABLE; i++)
  90.         if (hRegTimer[i])
  91.             KillTimer(__hMainWnd, hRegTimer[i]);
  92.         else
  93.             break;
  94.  
  95.     winio_onclose(__hMainWnd, (DESTROY_FUNC) ResetTimer);
  96.  
  97.     return 0;
  98.     }
  99.