Hook.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "stdafx.h"
  2. #include "Hook.h"
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include "Detours/detours.h"
  6. #include "Detours/detver.h"
  7. #include <string>
  8. extern HWND g_hWnd;
  9. static HANDLE (WINAPI * Real_CreateFileW)(
  10. __in LPCWSTR lpFileName,
  11. __in DWORD dwDesiredAccess,
  12. __in DWORD dwShareMode,
  13. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  14. __in DWORD dwCreationDisposition,
  15. __in DWORD dwFlagsAndAttributes,
  16. __in_opt HANDLE hTemplateFile
  17. ) = CreateFileW;
  18. static HANDLE(WINAPI * Real_CreateFileA)(
  19. __in LPCSTR lpFileName,
  20. __in DWORD dwDesiredAccess,
  21. __in DWORD dwShareMode,
  22. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  23. __in DWORD dwCreationDisposition,
  24. __in DWORD dwFlagsAndAttributes,
  25. __in_opt HANDLE hTemplateFile
  26. ) = CreateFileA;
  27. HANDLE WINAPI Mine_CreateFileW(
  28. __in LPCWSTR lpFileName,
  29. __in DWORD dwDesiredAccess,
  30. __in DWORD dwShareMode,
  31. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  32. __in DWORD dwCreationDisposition,
  33. __in DWORD dwFlagsAndAttributes,
  34. __in_opt HANDLE hTemplateFile
  35. )
  36. {
  37. std::wstring wsFileName = lpFileName;
  38. if (wsFileName == L"COM1" || wsFileName == L"com1")
  39. {
  40. MessageBox(NULL, L"1111111111", L"111111111111111111111", MB_OK);
  41. }
  42. return Real_CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  43. }
  44. HANDLE WINAPI Mine_CreateFileA(
  45. __in LPCSTR lpFileName,
  46. __in DWORD dwDesiredAccess,
  47. __in DWORD dwShareMode,
  48. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  49. __in DWORD dwCreationDisposition,
  50. __in DWORD dwFlagsAndAttributes,
  51. __in_opt HANDLE hTemplateFile
  52. )
  53. {
  54. std::string FileName = lpFileName;
  55. if (FileName == "COM1" || FileName == "com1")
  56. {
  57. MessageBox(NULL, L"1111111111", L"2222", MB_OK);
  58. }
  59. return Real_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  60. }
  61. void Hook()
  62. {
  63. LONG error;
  64. DetourRestoreAfterWith();
  65. DetourTransactionBegin();
  66. DetourUpdateThread(GetCurrentThread());
  67. DetourAttach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW);
  68. DetourAttach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA);
  69. error = DetourTransactionCommit();
  70. if (error == NO_ERROR) {
  71. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  72. " Detoured Echo().\n");
  73. }
  74. else {
  75. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  76. " Error detouring Echo(): %d\n", error);
  77. }
  78. }
  79. void UnHook()
  80. {
  81. LONG error;
  82. DetourTransactionBegin();
  83. DetourUpdateThread(GetCurrentThread());
  84. DetourDetach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW);
  85. DetourDetach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA);
  86. error = DetourTransactionCommit();
  87. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  88. " Removed Echo() (result=%d)\n", error);
  89. fflush(stdout);
  90. }