2
This file is a part of the NVDA project.
3
URL: http://www.nvda-project.org/
4
Copyright 2006-2010 NVDA contributers.
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License version 2.0, as published by
7
the Free Software Foundation.
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
This license can be found at:
12
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17
using namespace MinHook;
19
//Based on MH_EnableHook from minHook
20
MH_STATUS _doAllHooks(bool enable) {
21
CriticalSection::ScopedLock lock(gCS);
22
if (!gIsInitialized) {
23
return MH_ERROR_NOT_INITIALIZED;
25
std::vector<uintptr_t> oldIPs;
26
std::vector<uintptr_t> newIPs;
27
for(std::vector<HOOK_ENTRY>::iterator hooksIter=gHooks.begin();hooksIter!=gHooks.end();++hooksIter) {
28
oldIPs.insert(oldIPs.end(),hooksIter->oldIPs.begin(),hooksIter->oldIPs.end());
29
newIPs.insert(newIPs.end(),hooksIter->newIPs.begin(),hooksIter->newIPs.end());
32
ScopedThreadExclusive tex(oldIPs, newIPs);
33
for(std::vector<HOOK_ENTRY>::iterator hooksIter=gHooks.begin();hooksIter!=gHooks.end();++hooksIter) {
34
HOOK_ENTRY*pHook=&(*hooksIter);
35
if (pHook->isEnabled==enable) continue;
37
if (!VirtualProtect(pHook->pTarget, sizeof(JMP_REL), PAGE_EXECUTE_READWRITE, &oldProtect)) {
38
return MH_ERROR_MEMORY_PROTECT;
42
WriteRelativeJump(pHook->pTarget, pHook->pRelay);
44
WriteRelativeJump(pHook->pTarget, pHook->pDetour);
47
memcpy(pHook->pTarget, pHook->pBackup, sizeof(JMP_REL));
49
VirtualProtect(pHook->pTarget, sizeof(JMP_REL), oldProtect, &oldProtect);
50
pHook->isEnabled = enable;
56
MH_STATUS MH_EnableAllHooks() {
57
return _doAllHooks(true);
60
MH_STATUS MH_DisableAllHooks() {
61
return _doAllHooks(false);