| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #include "stdafx.h"
- #include "Hook.h"
- #include <windows.h>
- #include <stdio.h>
- #include "Detours/detours.h"
- #include "Detours/detver.h"
- #include <string>
- extern HWND g_hWnd;
- static HANDLE (WINAPI * Real_CreateFileW)(
- __in LPCWSTR lpFileName,
- __in DWORD dwDesiredAccess,
- __in DWORD dwShareMode,
- __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
- __in DWORD dwCreationDisposition,
- __in DWORD dwFlagsAndAttributes,
- __in_opt HANDLE hTemplateFile
- ) = CreateFileW;
- static HANDLE(WINAPI * Real_CreateFileA)(
- __in LPCSTR lpFileName,
- __in DWORD dwDesiredAccess,
- __in DWORD dwShareMode,
- __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
- __in DWORD dwCreationDisposition,
- __in DWORD dwFlagsAndAttributes,
- __in_opt HANDLE hTemplateFile
- ) = CreateFileA;
- HANDLE WINAPI Mine_CreateFileW(
- __in LPCWSTR lpFileName,
- __in DWORD dwDesiredAccess,
- __in DWORD dwShareMode,
- __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
- __in DWORD dwCreationDisposition,
- __in DWORD dwFlagsAndAttributes,
- __in_opt HANDLE hTemplateFile
- )
- {
- std::wstring wsFileName = lpFileName;
- if (wsFileName == L"COM1" || wsFileName == L"com1")
- {
- MessageBox(NULL, L"1111111111", L"111111111111111111111", MB_OK);
- }
- return Real_CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
- }
- HANDLE WINAPI Mine_CreateFileA(
- __in LPCSTR lpFileName,
- __in DWORD dwDesiredAccess,
- __in DWORD dwShareMode,
- __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
- __in DWORD dwCreationDisposition,
- __in DWORD dwFlagsAndAttributes,
- __in_opt HANDLE hTemplateFile
- )
- {
- std::string FileName = lpFileName;
- if (FileName == "COM1" || FileName == "com1")
- {
- MessageBox(NULL, L"1111111111", L"2222", MB_OK);
- }
- return Real_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
- }
- void Hook()
- {
- LONG error;
- DetourRestoreAfterWith();
- DetourTransactionBegin();
- DetourUpdateThread(GetCurrentThread());
- DetourAttach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW);
- DetourAttach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA);
- error = DetourTransactionCommit();
- if (error == NO_ERROR) {
- printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
- " Detoured Echo().\n");
- }
- else {
- printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
- " Error detouring Echo(): %d\n", error);
- }
- }
- void UnHook()
- {
- LONG error;
- DetourTransactionBegin();
- DetourUpdateThread(GetCurrentThread());
- DetourDetach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW);
- DetourDetach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA);
- error = DetourTransactionCommit();
- printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
- " Removed Echo() (result=%d)\n", error);
- fflush(stdout);
- }
|