~stomato463/+junk/nvdajp

« back to all changes in this revision

Viewing changes to nvdaHelper/minHook/newHook.cpp

  • Committer: Masataka Shinke
  • Date: 2011-10-25 12:35:26 UTC
  • mfrom: (4185 jpmain)
  • mto: This revision was merged to the branch mainline in revision 4211.
  • Revision ID: mshinke@users.sourceforge.jp-20111025123526-ze527a2rl3z0g2ky
lp:~nishimotz/nvdajp/main : 4185 をマージ

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
13
*/
 
14
 
 
15
#include "hook.cpp"
 
16
 
 
17
using namespace MinHook;
 
18
 
 
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;
 
24
        }
 
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());
 
30
        }
 
31
        {
 
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;
 
36
                        DWORD oldProtect;
 
37
                        if (!VirtualProtect(pHook->pTarget, sizeof(JMP_REL), PAGE_EXECUTE_READWRITE, &oldProtect)) {
 
38
                                return MH_ERROR_MEMORY_PROTECT;
 
39
                        }
 
40
                        if(enable) {
 
41
#if defined _M_X64
 
42
                                WriteRelativeJump(pHook->pTarget, pHook->pRelay);
 
43
#elif defined _M_IX86
 
44
                                WriteRelativeJump(pHook->pTarget, pHook->pDetour);
 
45
#endif
 
46
                        } else {
 
47
                                memcpy(pHook->pTarget, pHook->pBackup, sizeof(JMP_REL));
 
48
                        }
 
49
                        VirtualProtect(pHook->pTarget, sizeof(JMP_REL), oldProtect, &oldProtect);
 
50
                                pHook->isEnabled = enable;
 
51
                }
 
52
        }
 
53
        return MH_OK;
 
54
}
 
55
 
 
56
MH_STATUS MH_EnableAllHooks() {
 
57
        return _doAllHooks(true);
 
58
}
 
59
 
 
60
MH_STATUS MH_DisableAllHooks() {
 
61
        return _doAllHooks(false);
 
62
}
 
63