├── Debug
└── MinHook.x86.dll
├── README.md
├── include
└── MinHook.h
├── lib
├── MinHook.x86.lib
└── libMinHook.x86.lib
├── minHook.ncb
├── minHook.sln
├── minHook.suo
└── minHook
├── ReadMe.txt
├── minHook.cpp
├── minHook.vcproj
├── stdafx.cpp
├── stdafx.h
├── targetver.h
├── test_hook_common_function.h
└── test_hook_win_api.h
/Debug/MinHook.x86.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liunan1111/use-example-of-minhook/b82b919159794a802647669349054c5ccb0d5063/Debug/MinHook.x86.dll
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # use-example-of-minhook
2 |
3 |
4 | [TOC]
5 |
6 |
7 | ## 1.说明
8 | * Debug文件夹下的MinHook.x86.dll、lib文件夹下的MinHook.x86.liblibMinHook.x86都是从minhook开源项目VC9目录下项目编译所得,本项目使用的也是vs2008.
9 | * 头文件只有一个再include文件夹下。
10 | * dll文件、头文件、lib文件的配置是好的,项目可以直接运行。
11 |
12 | ## 2.例子
13 |
14 | * 例子一:测试minhook在hook win api的使用。
15 | * 例子二:测试minhook在hook 本程序自定以函数的使用。
16 |
17 | ## 3.TODO
18 |
19 | * 测试所有minhook的接口。
20 | * 测试minhook在自定义dll的函数hook上的使用。
21 | * 剥析minhook源代码,形成文档。
22 |
23 |
24 |
--------------------------------------------------------------------------------
/include/MinHook.h:
--------------------------------------------------------------------------------
1 | /*
2 | * MinHook - The Minimalistic API Hooking Library for x64/x86
3 | * Copyright (C) 2009-2017 Tsuda Kageyu.
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | *
10 | * 1. Redistributions of source code must retain the above copyright
11 | * notice, this list of conditions and the following disclaimer.
12 | * 2. Redistributions in binary form must reproduce the above copyright
13 | * notice, this list of conditions and the following disclaimer in the
14 | * documentation and/or other materials provided with the distribution.
15 | *
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
20 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 | */
28 |
29 | #pragma once
30 |
31 | #if !(defined _M_IX86) && !(defined _M_X64) && !(defined __i386__) && !(defined __x86_64__)
32 | #error MinHook supports only x86 and x64 systems.
33 | #endif
34 |
35 | #include
36 |
37 | // MinHook Error Codes.
38 | typedef enum MH_STATUS
39 | {
40 | // Unknown error. Should not be returned.
41 | MH_UNKNOWN = -1,
42 |
43 | // Successful.
44 | MH_OK = 0,
45 |
46 | // MinHook is already initialized.
47 | MH_ERROR_ALREADY_INITIALIZED,
48 |
49 | // MinHook is not initialized yet, or already uninitialized.
50 | MH_ERROR_NOT_INITIALIZED,
51 |
52 | // The hook for the specified target function is already created.
53 | MH_ERROR_ALREADY_CREATED,
54 |
55 | // The hook for the specified target function is not created yet.
56 | MH_ERROR_NOT_CREATED,
57 |
58 | // The hook for the specified target function is already enabled.
59 | MH_ERROR_ENABLED,
60 |
61 | // The hook for the specified target function is not enabled yet, or already
62 | // disabled.
63 | MH_ERROR_DISABLED,
64 |
65 | // The specified pointer is invalid. It points the address of non-allocated
66 | // and/or non-executable region.
67 | MH_ERROR_NOT_EXECUTABLE,
68 |
69 | // The specified target function cannot be hooked.
70 | MH_ERROR_UNSUPPORTED_FUNCTION,
71 |
72 | // Failed to allocate memory.
73 | MH_ERROR_MEMORY_ALLOC,
74 |
75 | // Failed to change the memory protection.
76 | MH_ERROR_MEMORY_PROTECT,
77 |
78 | // The specified module is not loaded.
79 | MH_ERROR_MODULE_NOT_FOUND,
80 |
81 | // The specified function is not found.
82 | MH_ERROR_FUNCTION_NOT_FOUND
83 | }
84 | MH_STATUS;
85 |
86 | // Can be passed as a parameter to MH_EnableHook, MH_DisableHook,
87 | // MH_QueueEnableHook or MH_QueueDisableHook.
88 | #define MH_ALL_HOOKS NULL
89 |
90 | #ifdef __cplusplus
91 | extern "C" {
92 | #endif
93 |
94 | // Initialize the MinHook library. You must call this function EXACTLY ONCE
95 | // at the beginning of your program.
96 | MH_STATUS WINAPI MH_Initialize(VOID);
97 |
98 | // Uninitialize the MinHook library. You must call this function EXACTLY
99 | // ONCE at the end of your program.
100 | MH_STATUS WINAPI MH_Uninitialize(VOID);
101 |
102 | // Creates a Hook for the specified target function, in disabled state.
103 | // Parameters:
104 | // pTarget [in] A pointer to the target function, which will be
105 | // overridden by the detour function.
106 | // pDetour [in] A pointer to the detour function, which will override
107 | // the target function.
108 | // ppOriginal [out] A pointer to the trampoline function, which will be
109 | // used to call the original target function.
110 | // This parameter can be NULL.
111 | MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOriginal);
112 |
113 | // Creates a Hook for the specified API function, in disabled state.
114 | // Parameters:
115 | // pszModule [in] A pointer to the loaded module name which contains the
116 | // target function.
117 | // pszTarget [in] A pointer to the target function name, which will be
118 | // overridden by the detour function.
119 | // pDetour [in] A pointer to the detour function, which will override
120 | // the target function.
121 | // ppOriginal [out] A pointer to the trampoline function, which will be
122 | // used to call the original target function.
123 | // This parameter can be NULL.
124 | MH_STATUS WINAPI MH_CreateHookApi(
125 | LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal);
126 |
127 | // Creates a Hook for the specified API function, in disabled state.
128 | // Parameters:
129 | // pszModule [in] A pointer to the loaded module name which contains the
130 | // target function.
131 | // pszTarget [in] A pointer to the target function name, which will be
132 | // overridden by the detour function.
133 | // pDetour [in] A pointer to the detour function, which will override
134 | // the target function.
135 | // ppOriginal [out] A pointer to the trampoline function, which will be
136 | // used to call the original target function.
137 | // This parameter can be NULL.
138 | // ppTarget [out] A pointer to the target function, which will be used
139 | // with other functions.
140 | // This parameter can be NULL.
141 | MH_STATUS WINAPI MH_CreateHookApiEx(
142 | LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal, LPVOID *ppTarget);
143 |
144 | // Removes an already created hook.
145 | // Parameters:
146 | // pTarget [in] A pointer to the target function.
147 | MH_STATUS WINAPI MH_RemoveHook(LPVOID pTarget);
148 |
149 | // Enables an already created hook.
150 | // Parameters:
151 | // pTarget [in] A pointer to the target function.
152 | // If this parameter is MH_ALL_HOOKS, all created hooks are
153 | // enabled in one go.
154 | MH_STATUS WINAPI MH_EnableHook(LPVOID pTarget);
155 |
156 | // Disables an already created hook.
157 | // Parameters:
158 | // pTarget [in] A pointer to the target function.
159 | // If this parameter is MH_ALL_HOOKS, all created hooks are
160 | // disabled in one go.
161 | MH_STATUS WINAPI MH_DisableHook(LPVOID pTarget);
162 |
163 | // Queues to enable an already created hook.
164 | // Parameters:
165 | // pTarget [in] A pointer to the target function.
166 | // If this parameter is MH_ALL_HOOKS, all created hooks are
167 | // queued to be enabled.
168 | MH_STATUS WINAPI MH_QueueEnableHook(LPVOID pTarget);
169 |
170 | // Queues to disable an already created hook.
171 | // Parameters:
172 | // pTarget [in] A pointer to the target function.
173 | // If this parameter is MH_ALL_HOOKS, all created hooks are
174 | // queued to be disabled.
175 | MH_STATUS WINAPI MH_QueueDisableHook(LPVOID pTarget);
176 |
177 | // Applies all queued changes in one go.
178 | MH_STATUS WINAPI MH_ApplyQueued(VOID);
179 |
180 | // Translates the MH_STATUS to its name as a string.
181 | const char * WINAPI MH_StatusToString(MH_STATUS status);
182 |
183 | #ifdef __cplusplus
184 | }
185 | #endif
186 |
187 |
--------------------------------------------------------------------------------
/lib/MinHook.x86.lib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liunan1111/use-example-of-minhook/b82b919159794a802647669349054c5ccb0d5063/lib/MinHook.x86.lib
--------------------------------------------------------------------------------
/lib/libMinHook.x86.lib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liunan1111/use-example-of-minhook/b82b919159794a802647669349054c5ccb0d5063/lib/libMinHook.x86.lib
--------------------------------------------------------------------------------
/minHook.ncb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liunan1111/use-example-of-minhook/b82b919159794a802647669349054c5ccb0d5063/minHook.ncb
--------------------------------------------------------------------------------
/minHook.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 10.00
3 | # Visual Studio 2008
4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "minHook", "minHook\minHook.vcproj", "{A6D0513B-A242-417A-8FEA-F6C02F01FF1E}"
5 | EndProject
6 | Global
7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 | Debug|Win32 = Debug|Win32
9 | Release|Win32 = Release|Win32
10 | EndGlobalSection
11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 | {A6D0513B-A242-417A-8FEA-F6C02F01FF1E}.Debug|Win32.ActiveCfg = Debug|Win32
13 | {A6D0513B-A242-417A-8FEA-F6C02F01FF1E}.Debug|Win32.Build.0 = Debug|Win32
14 | {A6D0513B-A242-417A-8FEA-F6C02F01FF1E}.Release|Win32.ActiveCfg = Release|Win32
15 | {A6D0513B-A242-417A-8FEA-F6C02F01FF1E}.Release|Win32.Build.0 = Release|Win32
16 | EndGlobalSection
17 | GlobalSection(SolutionProperties) = preSolution
18 | HideSolutionNode = FALSE
19 | EndGlobalSection
20 | EndGlobal
21 |
--------------------------------------------------------------------------------
/minHook.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liunan1111/use-example-of-minhook/b82b919159794a802647669349054c5ccb0d5063/minHook.suo
--------------------------------------------------------------------------------
/minHook/ReadMe.txt:
--------------------------------------------------------------------------------
1 | ========================================================================
2 | CONSOLE APPLICATION : minHook Project Overview
3 | ========================================================================
4 |
5 | AppWizard has created this minHook application for you.
6 |
7 | This file contains a summary of what you will find in each of the files that
8 | make up your minHook application.
9 |
10 |
11 | minHook.vcproj
12 | This is the main project file for VC++ projects generated using an Application Wizard.
13 | It contains information about the version of Visual C++ that generated the file, and
14 | information about the platforms, configurations, and project features selected with the
15 | Application Wizard.
16 |
17 | minHook.cpp
18 | This is the main application source file.
19 |
20 | /////////////////////////////////////////////////////////////////////////////
21 | Other standard files:
22 |
23 | StdAfx.h, StdAfx.cpp
24 | These files are used to build a precompiled header (PCH) file
25 | named minHook.pch and a precompiled types file named StdAfx.obj.
26 |
27 | /////////////////////////////////////////////////////////////////////////////
28 | Other notes:
29 |
30 | AppWizard uses "TODO:" comments to indicate parts of the source code you
31 | should add to or customize.
32 |
33 | /////////////////////////////////////////////////////////////////////////////
34 |
--------------------------------------------------------------------------------
/minHook/minHook.cpp:
--------------------------------------------------------------------------------
1 | // minHook.cpp : Defines the entry point for the console application.
2 | //
3 |
4 | #include "stdafx.h"
5 | #include
6 |
7 | #include
8 | using namespace std;
9 |
10 | #include "test_hook_common_function.h"
11 | #include "test_hook_win_api.h"
12 |
13 | #if defined _M_X64
14 | #pragma comment(lib, "libMinHook.x64.lib")
15 | #elif defined _M_IX86
16 | #pragma comment(lib, "libMinHook.x86.lib")
17 | #endif
18 |
19 |
20 |
21 | int main()
22 | {
23 | cout<<"========================================="<>k)
31 | {
32 | switch(k)
33 | {
34 | case 1:
35 | {
36 | test_hook_win_api();
37 | break;
38 | }
39 | case 2:
40 | {
41 | test_hook_common_funciton();
42 | }
43 | }
44 |
45 | cout<<"input:"<
2 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
26 |
29 |
32 |
35 |
38 |
41 |
53 |
56 |
59 |
62 |
71 |
74 |
77 |
80 |
83 |
86 |
89 |
92 |
93 |
101 |
104 |
107 |
110 |
113 |
116 |
127 |
130 |
133 |
136 |
145 |
148 |
151 |
154 |
157 |
160 |
163 |
166 |
167 |
168 |
169 |
170 |
171 |
176 |
179 |
180 |
183 |
186 |
190 |
191 |
194 |
198 |
199 |
200 |
201 |
206 |
209 |
210 |
213 |
214 |
217 |
218 |
221 |
222 |
223 |
228 |
229 |
232 |
233 |
234 |
235 |
236 |
237 |
--------------------------------------------------------------------------------
/minHook/stdafx.cpp:
--------------------------------------------------------------------------------
1 | // stdafx.cpp : source file that includes just the standard includes
2 | // minHook.pch will be the pre-compiled header
3 | // stdafx.obj will contain the pre-compiled type information
4 |
5 | #include "stdafx.h"
6 |
7 | // TODO: reference any additional headers you need in STDAFX.H
8 | // and not in this file
9 |
--------------------------------------------------------------------------------
/minHook/stdafx.h:
--------------------------------------------------------------------------------
1 | // stdafx.h : include file for standard system include files,
2 | // or project specific include files that are used frequently, but
3 | // are changed infrequently
4 | //
5 |
6 | #pragma once
7 |
8 | #include "targetver.h"
9 |
10 | #include
11 | #include
12 |
13 |
14 |
15 | // TODO: reference additional headers your program requires here
16 |
--------------------------------------------------------------------------------
/minHook/targetver.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | // The following macros define the minimum required platform. The minimum required platform
4 | // is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run
5 | // your application. The macros work by enabling all features available on platform versions up to and
6 | // including the version specified.
7 |
8 | // Modify the following defines if you have to target a platform prior to the ones specified below.
9 | // Refer to MSDN for the latest info on corresponding values for different platforms.
10 | #ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.
11 | #define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.
12 | #endif
13 |
14 |
--------------------------------------------------------------------------------
/minHook/test_hook_common_function.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | //Because the tests are simple, the declarations and definitions are in the header file.
4 | #include "MinHook.h"
5 | #include
6 | using namespace std;
7 |
8 | void Function()
9 | {
10 | cout<<"real function"<(&fpFunction)) != MH_OK)
32 | {
33 | return 1;
34 | }
35 |
36 | // Enable the hook for MessageBoxW.
37 | if (MH_EnableHook(&Function) != MH_OK)
38 | {
39 | return 1;
40 | }
41 |
42 | // Expected to tell "Hooked!".
43 | Function();
44 |
45 | // Disable the hook for MessageBoxW.
46 | if (MH_DisableHook(&Function) != MH_OK)
47 | {
48 | return 1;
49 | }
50 |
51 | // Expected to tell "Not hooked...".
52 | Function();
53 |
54 | // Uninitialize MinHook.
55 | if (MH_Uninitialize() != MH_OK)
56 | {
57 | return 1;
58 | }
59 |
60 | return 0;
61 | }
--------------------------------------------------------------------------------
/minHook/test_hook_win_api.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | //Because the tests are simple, the declarations and definitions are in the header file.
4 |
5 | #include "MinHook.h"
6 | #include
7 | using namespace std;
8 |
9 | typedef int (WINAPI *MESSAGEBOXW)(HWND, LPCWSTR, LPCWSTR, UINT);
10 |
11 | // Pointer for calling original MessageBoxW.
12 | MESSAGEBOXW fpMessageBoxW = NULL;
13 |
14 | // Detour function which overrides MessageBoxW.
15 | int WINAPI DetourMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
16 | {
17 | return fpMessageBoxW(hWnd, L"Hooked!", lpCaption, uType);
18 | return 1;
19 | }
20 |
21 |
22 | int test_hook_win_api()
23 | {
24 | // Initialize MinHook.
25 | if (MH_Initialize() != MH_OK)
26 | {
27 | return 1;
28 | }
29 |
30 | // Create a hook for MessageBoxW, in disabled state.
31 | if (MH_CreateHook(&MessageBoxW, &DetourMessageBoxW,
32 | reinterpret_cast(&fpMessageBoxW)) != MH_OK)
33 | {
34 | return 1;
35 | }
36 |
37 | // or you can use the new helper funtion like this.
38 | //if (MH_CreateHookApiEx(
39 | // L"user32", "MessageBoxW", &DetourMessageBoxW, &fpMessageBoxW) != MH_OK)
40 | //{
41 | // return 1;
42 | //}
43 |
44 | // Enable the hook for MessageBoxW.
45 | if (MH_EnableHook(&MessageBoxW) != MH_OK)
46 | {
47 | return 1;
48 | }
49 |
50 | // Expected to tell "Hooked!".
51 | MessageBoxW(NULL, L"Not hooked...", L"MinHook Sample", MB_OK);
52 |
53 | // Disable the hook for MessageBoxW.
54 | if (MH_DisableHook(&MessageBoxW) != MH_OK)
55 | {
56 | return 1;
57 | }
58 |
59 | // Expected to tell "Not hooked...".
60 | MessageBoxW(NULL, L"Not hooked...", L"MinHook Sample", MB_OK);
61 |
62 | // Uninitialize MinHook.
63 | if (MH_Uninitialize() != MH_OK)
64 | {
65 | return 1;
66 | }
67 |
68 | return 0;
69 |
70 | }
--------------------------------------------------------------------------------