home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_01 / SetProc / SetProc.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  2.2 KB  |  63 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : setproc.h                                                             //
  10. //  Description: API hooking through import/export table, Chapter 1                  //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include "..\..\include\pehack.h"
  19.  
  20. int WINAPI MyMessageBoxA(HWND hWnd, LPCSTR pText, LPCSTR pCaption, 
  21.                          UINT uType)
  22. {
  23.     WCHAR wText[MAX_PATH];
  24.     WCHAR wCaption[MAX_PATH];
  25.  
  26.     MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pText, 
  27.         -1, wText,    MAX_PATH);
  28.     wcscat(wText, L" - intercepted");
  29.  
  30.     MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pCaption,
  31.         -1, wCaption, MAX_PATH);
  32.     wcscat(wCaption, L" - intercepted");
  33.  
  34.     return MessageBoxW(hWnd, wText, wCaption, uType);
  35. }
  36.  
  37. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
  38. {
  39.     {
  40.         KPEFile pe(hInstance);
  41.     
  42.         pe.SetImportAddress("user32.dll", "MessageBoxA", (FARPROC) MyMessageBoxA);
  43.  
  44.         MessageBoxA(NULL, "Test", "SetImportAddress", MB_OK);
  45.     }
  46.  
  47.     HMODULE hUser = GetModuleHandle("user32.dll");
  48.  
  49.     KPEFile user32(hUser);
  50.  
  51.     FARPROC oldproc = GetProcAddress(hUser, "MessageBoxA");
  52.  
  53.     user32.SetExportAddress("MessageBoxA", (FARPROC) MyMessageBoxA);
  54.  
  55.     FARPROC newproc = GetProcAddress(hUser, "MessageBoxA");
  56.  
  57.     char temp[64];
  58.     wsprintf(temp, "GetProcAddress(MessageBoxA)\n"
  59.         "changes from %x to %x", oldproc, newproc);
  60.     MessageBoxA(NULL, temp, "SetExportAddress", MB_OK);
  61.  
  62.     return 0;
  63. }