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

  1. /*
  2.     TASKWALK.C -- Generates a list of Windows tasks
  3.  
  4.     From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
  5.     by Andrew Schulman, Dave Maxey and Matt Pietrek
  6.  
  7.     Build using: WINIOBC TASKWALK (for Borland C++ v3.00)
  8.                  WINIOMS TASKWALK (for Microsoft C/SDK)
  9. */
  10.  
  11. #include <windows.h>
  12. #include <dos.h>
  13. #include "winio.h"
  14.  
  15. #ifndef __BORLANDC__ 
  16. #define MK_FP(a,b)  ((void far *)(((unsigned long)(a) << 16) | (b))) 
  17. #endif
  18.  
  19. /* GetCurrentTask is documented, but only with WORD retval */
  20. DWORD (FAR PASCAL *GetCurrentTaskD)(void);
  21.  
  22. main()
  23. {
  24.     HWND hwnd = winio_current();
  25.     DWORD dwTask;
  26.     WORD wFirstTask, wNextTask;
  27.     WORD wNumTasks;
  28.     
  29.     winio_about("TASKWALK"
  30.         "\nGenerates a list of Windows tasks"
  31.         "\n\nFrom Chapter 5 of"
  32.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  33.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  34.         );
  35.     
  36.     GetCurrentTaskD = GetCurrentTask;
  37.  
  38.     dwTask = GetCurrentTaskD();
  39.     wFirstTask = HIWORD(dwTask);  // get base of linked list
  40.     wNextTask = wFirstTask;
  41.     wNumTasks = 0;
  42.     printf("%lx\n", dwTask);
  43.     winio_setpaint(hwnd,FALSE);  // so we don't yield while walking task list!
  44.     for (;;)
  45.     {
  46.         /*
  47.             See the description of the Task Database data structure
  48.             for an explanation of offsets 0 and 0F2h.
  49.         */
  50.         char far *fp;
  51.         char modname[9], *p=modname;
  52.         int i;
  53.         /* Copy the modname name at offset 0F2h in the Task Database */
  54.         for (i=0, fp= (char far *) MK_FP(wNextTask,0xf2); i<8; i++, p++, fp++)
  55.             *p = *fp;
  56.         *p = '\0';
  57.         printf("%04x %s", wNextTask, modname);
  58.         if (wNextTask == GetCurrentTask())
  59.             printf(" <== current task");
  60.         printf("\n");
  61.         wNumTasks++;
  62.         /* Get the handle of the next task from offset 0 in the Task DB */
  63.         wNextTask = *((WORD far *) MK_FP(wNextTask, 0));
  64.         if (wNextTask == 0)
  65.             break;
  66.     }
  67.     if (wNumTasks != GetNumTasks())
  68.         fail("Wrong number of tasks!");
  69.     winio_setpaint(hwnd,TRUE);
  70.     return 0;
  71. }
  72.  
  73.