OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 #include <windows.h> | 6 #include <windows.h> |
7 | 7 |
8 #define _DLL_EXPORTING | 8 #define BUILDING_DLL |
9 #include "integration_tests_common.h" | 9 #include "hooking_dll.h" |
10 | 10 |
11 // This data section creates a common area that is accessible | 11 // This data section creates a common area that is accessible |
12 // to all instances of the DLL (in every process). They map to | 12 // to all instances of the DLL (in every process). They map to |
13 // the same physical memory location. | 13 // the same physical memory location. |
14 // **Note that each instance of this DLL runs in the context of | 14 // **Note that each instance of this DLL runs in the context of |
15 // the process it's injected into, so things like pointers and | 15 // the process it's injected into, so things like pointers and |
16 // addresses won't work. | 16 // addresses won't work. |
17 // **Note that any variables must be initialized to put them in | 17 // **Note that any variables must be initialized to put them in |
18 // the specified segment, otherwise they will end up in the | 18 // the specified segment, otherwise they will end up in the |
19 // default data segment. | 19 // default data segment. |
20 #pragma data_seg(".hook") | 20 #pragma data_seg(".hook") |
21 HHOOK hook = NULL; | 21 HHOOK hook = NULL; |
22 bool hook_called = false; | 22 bool hook_called = false; |
23 #pragma data_seg() | 23 #pragma data_seg() |
24 #pragma comment(linker, "/SECTION:.hook,RWS") | 24 #pragma comment(linker, "/SECTION:.hook,RWS") |
25 | 25 |
26 namespace { | 26 namespace { |
27 | |
28 HANDLE event = NULL; | 27 HANDLE event = NULL; |
29 } | 28 } |
30 | 29 |
| 30 namespace hooking_dll { |
| 31 |
31 void SetHook(HHOOK hook_handle) { | 32 void SetHook(HHOOK hook_handle) { |
32 hook = hook_handle; | 33 hook = hook_handle; |
33 | 34 |
34 return; | 35 return; |
35 } | 36 } |
36 | 37 |
37 bool WasHookCalled() { | 38 bool WasHookCalled() { |
38 return hook_called; | 39 return hook_called; |
39 } | 40 } |
40 | 41 |
41 LRESULT HookProc(int code, WPARAM w_param, LPARAM l_param) { | 42 LRESULT HookProc(int code, WPARAM w_param, LPARAM l_param) { |
42 hook_called = true; | 43 hook_called = true; |
43 if (event) | 44 if (event) |
44 ::SetEvent(event); | 45 ::SetEvent(event); |
45 | 46 |
46 // Recent versions of Windows do not require the HHOOK to be passed along, | 47 // Recent versions of Windows do not require the HHOOK to be passed along, |
47 // but I'm doing it here to show the shared use of the HHOOK variable in | 48 // but I'm doing it here to show the shared use of the HHOOK variable in |
48 // the shared data segment. It is set by the instance of the DLL in the | 49 // the shared data segment. It is set by the instance of the DLL in the |
49 // main test process. | 50 // main test process. |
50 return CallNextHookEx(hook, code, w_param, l_param); | 51 return CallNextHookEx(hook, code, w_param, l_param); |
51 } | 52 } |
52 | 53 |
| 54 } // namespace hooking_dll |
| 55 |
53 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) { | 56 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) { |
54 if (reason == DLL_PROCESS_ATTACH) { | 57 if (reason == DLL_PROCESS_ATTACH) { |
55 // The testing process should have set up this named event already | 58 // The testing process should have set up this named event already |
56 // (if the test needs this event to be signaled). | 59 // (if the test needs this event to be signaled). |
57 event = ::OpenEventW(EVENT_MODIFY_STATE, FALSE, g_hook_event); | 60 event = ::OpenEventW(EVENT_MODIFY_STATE, FALSE, hooking_dll::g_hook_event); |
58 } | 61 } |
59 | 62 |
60 if (reason == DLL_PROCESS_DETACH) | 63 if (reason == DLL_PROCESS_DETACH && event != nullptr) |
61 ::CloseHandle(event); | 64 ::CloseHandle(event); |
62 | 65 |
63 return TRUE; | 66 return TRUE; |
64 } | 67 } |
OLD | NEW |