home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / walker / probe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  2.7 KB  |  123 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. #include "pwalk.h"
  13.  
  14.  
  15. /* process  globals */
  16. HANDLE            hMMFile = 0;
  17. LPPROBE         lpProbe = NULL;
  18. CONTEXT         gContext;
  19. DWORD            gReturn;
  20. LPVOID            gLocation;
  21. CRITICAL_SECTION    gCS;
  22.  
  23.  
  24. BOOL   WINAPI ProbeProcess (HANDLE);
  25. void   WINAPI FreeProbe ();
  26. void   WINAPI GetFuncAddrInChildProc (char *, char *, DBGDLL *, LPVOID *);
  27.  
  28.  
  29. /* entry point for DLL loading and unloading */
  30. BOOL WINAPI DllMain (
  31.     HANDLE    hModule,
  32.     DWORD     dwFunction,
  33.     LPVOID    lpNot)
  34. {
  35.     switch (dwFunction)
  36.     {
  37.     case DLL_PROCESS_ATTACH:
  38.         ProbeProcess (hModule);
  39.         break;
  40.  
  41.     case DLL_PROCESS_DETACH:
  42.         FreeProbe ();
  43.         break;
  44.  
  45.     default:
  46.         break;
  47.     }
  48.  
  49.     return TRUE;
  50. }
  51.  
  52.  
  53.  
  54. /* function initializes port structures */
  55. BOOL WINAPI ProbeProcess (
  56.     HANDLE    hDLL)
  57. {
  58.     char    szMapFileName[MAX_PATH];
  59.  
  60.     /* load name for global file mapping */
  61.     LoadString (hDLL, IDS_MAPFILENAME, szMapFileName, MAX_PATH);
  62.  
  63.     /* ProcessWalker creates file mapping then child process attaches */
  64.     if ((hMMFile = OpenFileMapping (FILE_MAP_WRITE, FALSE, szMapFileName)))
  65.     {
  66.     /* map global view of file for hook function access */
  67.     lpProbe = (LPPROBE)MapViewOfFile (hMMFile, FILE_MAP_WRITE, 0, 0, 0);
  68.  
  69.     /* initialize known process specific information in probe */
  70.     lpProbe->hProcess = GetCurrentProcess ();
  71.     lpProbe->hDefHeap = GetProcessHeap ();
  72.     }
  73.  
  74.     else
  75.     if (!(hMMFile = CreateFileMapping ((HANDLE)0xffffffff,
  76.                        NULL,
  77.                        PAGE_READWRITE,
  78.                        0,
  79.                        0x100000,
  80.                        szMapFileName)))
  81.         return FALSE;
  82.     else
  83.         {
  84.         lpProbe = (LPPROBE)MapViewOfFile (hMMFile, FILE_MAP_WRITE, 0, 0, 0);
  85.         ResetProbe ();
  86.         }
  87.  
  88.     /* success */
  89.     return TRUE;
  90. }
  91.  
  92.  
  93.  
  94.  
  95. /* release process objects */
  96. void WINAPI FreeProbe ()
  97. {
  98.     if (lpProbe != NULL)
  99.     /* remove child process objects */
  100.     UnmapViewOfFile ((char *)lpProbe);
  101.  
  102.     CloseHandle (hMMFile);
  103. }
  104.  
  105.  
  106.  
  107.  
  108. /* function passes pointer to probe info back to ProcessWalker */
  109. LPPROBE  WINAPI RetrieveProbeData ()
  110. {
  111.     return lpProbe;
  112. }
  113.  
  114.  
  115.  
  116.  
  117. /* reset probe data to null */
  118. void  WINAPI ResetProbe ()
  119. {
  120.     lpProbe->hProcess = NULL;
  121.     lpProbe->hDefHeap = NULL;
  122. }
  123.