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

  1. /*
  2.     MODWALK.C -- Lists Windows modules
  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 MODWALK (for Borland C++ v3.00)
  8.                  WINIOMS MODWALK (for Microsoft C/SDK)
  9. */
  10.  
  11. #include <windows.h>
  12. #include <dos.h>
  13. #include "winio.h"
  14.  
  15. /* GetModuleHandle is documented, but only with WORD retval */
  16. DWORD (FAR PASCAL *GetModuleHandleD)(LPSTR lpModName);
  17.  
  18. main()
  19. {
  20.     extern WORD __hInst;
  21.     DWORD dwMod;
  22.     HWND hwnd;
  23.     WORD wFirstMod, wNextMod;
  24.     WORD wNumMods;
  25.     WORD wThisMod;
  26.  
  27.     winio_about("MODWALK"
  28.         "\nLists Windows modules"
  29.         "\n\nFrom Chapter 5 of"
  30.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  31.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  32.         );
  33.     
  34.     GetModuleHandleD = GetModuleHandle;
  35.  
  36.     dwMod = GetModuleHandleD("KERNEL");
  37.     wFirstMod = HIWORD(dwMod);  // get base of module linked list
  38.     wNextMod = wFirstMod;
  39.     wNumMods = 0;
  40.     wThisMod = GetModuleHandle(MK_FP(0, __hInst));   // same as GetExePtr
  41.     hwnd = winio_current();
  42.     winio_setpaint(hwnd,FALSE);  // so we don't yield while walking mod list!
  43.     for (;;)                // walk linked list
  44.     {
  45.         /*
  46.             For an explanation of offsets 0 and 6, see the description
  47.             of the Module Table data structure
  48.         */
  49.         char filename[128];
  50.         BYTE far *fpMod = MK_FP(wNextMod, 0);
  51.         if (fpMod[0] != 'N' || fpMod[1] != 'E') // check 'NE' signature
  52.             fail("Not a module!");
  53.         GetModuleFileName(wNextMod, filename, 128);
  54.         printf("%04x %s", wNextMod, filename);
  55.         if (wNextMod == wThisMod)
  56.             printf(" <== this module");
  57.         printf("\n");
  58.         wNumMods++;
  59.         /* Get the handle of the next Mod from offset 6 in the ModTbl */
  60.         wNextMod = *((WORD far *) MK_FP(wNextMod, 6));
  61.         if (wNextMod == 0)
  62.             break;
  63.     }
  64.     winio_setpaint(hwnd,TRUE);
  65.     printf("%d modules\n", wNumMods);
  66.     return 0;
  67. }
  68.  
  69.