home *** CD-ROM | disk | FTP | other *** search
- /*
- STSYSTMR.C -- Illustrates access to the reserved 'System' timers
-
- From Chapter 6 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Build using: WINIOBC STSYSTMR (for Borland C++ v3.00)
- WINIOMS STSYSTMR (for Microsoft C/SDK)
- */
-
- #include <windows.h>
- #include "wmhandlr.h"
- #include "winio.h"
-
- /* undocumented function */
- WORD (FAR PASCAL *lpfnSetSystemTimer)(HWND hwnd, int nIDEvent,
- WORD wElapse, FARPROC lpTimerFunction);
- BOOL (FAR PASCAL *lpfnKillSystemTimer)(HWND hwnd, WORD wTimer);
-
- #define WM_SYSTIMER 0x0118
-
- #define MAXTABLE 64 // Overgenerous..!
-
- WORD hSysTimer;
- WORD hRegTimer[MAXTABLE] = {0};
-
- long do_systimer(HWND hWnd, WORD wMsg,
- WORD nIDEvent, DWORD lParam)
- {
- printf("hWnd=0x%04X, wMsg=0x%04X, nIDEvent=0x%04X, lParam=0x%08lX\n",
- hWnd, wMsg, nIDEvent, lParam);
- return 0;
- }
-
- BOOL ResetTimer(HWND hwnd)
- {
- if (hSysTimer)
- (*lpfnKillSystemTimer)(hwnd, hSysTimer);
- return TRUE;
- }
-
-
- int main()
- {
- int i;
-
- winio_about("STSYSTMR"
- "\nIllustrates access to the reserved 'System' timers"
- "\n\nFrom Chapter 6 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek"
- );
-
- (FARPROC) lpfnSetSystemTimer =
- GetProcAddress(GetModuleHandle("USER"), (LPSTR) (DWORD) 11);
- (FARPROC) lpfnKillSystemTimer =
- GetProcAddress(GetModuleHandle("USER"), (LPSTR) (DWORD) 182);
-
- if ((! lpfnSetSystemTimer) || (! lpfnKillSystemTimer))
- {
- printf("Could not locate Set/KillSystemTimer functions.\n");
- return 0;
- }
-
- puts("Allocating timers...");
-
- for (i = 0; i < MAXTABLE; i++)
- {
- if (! (hRegTimer[i] = SetTimer(__hMainWnd, i+1, 10000,
- (FARPROC) 0L)))
- break;
- }
- printf("\nUsed SetTimer() to allocate all timers\n"
- "(there were %d available).\n\n"
- "We can still allocate a timer, however, using\n"
- "SetSystemTimer(). An event will be generated\n"
- "every second.\n"
- "Close the window to terminate.\n\n", i);
-
- wmhandler_set(__hMainWnd, WM_SYSTIMER, (WMHANDLER) do_systimer);
-
- if ((hSysTimer = (*lpfnSetSystemTimer)(__hMainWnd, 0xDADA,
- 1000, NULL)) == NULL)
- printf("Could not create system timer.\n");
- else
- printf("Timer handle is %04X\n\n", hSysTimer);
-
- printf("Freeing up %d regular timers.\n", i);
- for (i = 0; i < MAXTABLE; i++)
- if (hRegTimer[i])
- KillTimer(__hMainWnd, hRegTimer[i]);
- else
- break;
-
- winio_onclose(__hMainWnd, (DESTROY_FUNC) ResetTimer);
-
- return 0;
- }
-