home *** CD-ROM | disk | FTP | other *** search
- /*
- WALKSEGS.C -- Walks segments belonging to module
-
- From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Build using: WINIOBC WALKSEGS (for Borland C++ v3.00)
- WINIOMS WALKSEGS (for Microsoft C/SDK)
- */
-
- #include <windows.h>
- #include <dos.h>
- #include "winio.h"
-
- // Ignore the 'parameter 1 different' error from MS - the following
- // declaration splits the original GetCodeHandle lpProc param into
- // seg/ofs params.
- DWORD (FAR PASCAL *GetCodeHandleD)(WORD hMod, WORD wSeg);
-
- /* GetCodeInfo is documented, but not very well */
- typedef struct {
- WORD wOfs, wLen, wFlags, wAlloc, wHandle, wShift, wReserved[2];
- } CODEINFO;
-
- main(int argc, char *argv[])
- {
- char *modname;
- HANDLE hMod;
- WORD wSeg;
- WORD cSeg;
- DWORD dwCode;
-
- winio_about("WALKSEGS"
- "\nWalks segments belonging to module"
- "\nUsage: WALKSEGS [module name]"
- "\n\nFrom Chapter 5 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek"
- );
-
- GetCodeHandleD = GetCodeHandle;
-
- modname = (argc < 2) ? "KERNEL" : argv[1];
- if (! (hMod = GetModuleHandle(modname)))
- fail("Cannot locate module");
-
- /* Get number of segments from offset 1Ch in Module Table */
- cSeg = *((WORD far *) MK_FP(hMod, 0x1c));
-
- printf("%s %04x\n", modname, hMod);
- for (wSeg=1; wSeg<=cSeg; wSeg++)
- if ((dwCode = GetCodeHandleD(hMod, wSeg)) < 32)
- printf(" Seg #%d\tError!!\n", wSeg);
- else
- {
- CODEINFO codeinfo;
- WORD wSel = HIWORD(dwCode); // code selector
- /* this alternate form of GetCodeInfo is documented */
- GetCodeInfo((FARPROC) MK_FP(hMod, wSeg), &codeinfo);
- printf(" Seg #%d\t%04x\tLen=%04xh\t%s\n",
- wSeg, wSel,
- codeinfo.wLen,
- (codeinfo.wFlags & 1) ? "DATA" : "CODE");
- }
- return 0;
- }
-
-