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
#define WIN32_LEAN_AND_MEAN
19
#include <minHook/newMinHook.h>
20
#include "nvdaControllerInternal.h"
26
typedef multiset<HMODULE> moduleSet_t;
27
typedef set<void*> functionSet_t;
29
moduleSet_t g_hookedModules;
30
functionSet_t g_hookedFunctions;
32
bool apiHook_initialize() {
33
LOG_DEBUG("calling MH_Initialize");
35
if ((res=MH_Initialize())!=MH_OK) {
36
LOG_ERROR("MH_CreateHook failed with " << res);
42
void* apiHook_hookFunction(const char* moduleName, const char* functionName, void* newHookProc) {
43
HMODULE moduleHandle=LoadLibraryA(moduleName);
45
LOG_ERROR("module " << moduleName << " not loaded");
48
void* realFunc=GetProcAddress(moduleHandle,functionName);
50
LOG_ERROR("function " << functionName << " does not exist in module " << moduleName);
51
FreeLibrary(moduleHandle);
54
LOG_DEBUG("requesting to hook function " << functionName << " at address 0X" << std::hex << realFunc << " in module " << moduleName << " at address 0X" << moduleHandle << " with new function at address 0X" << newHookProc);
57
if((res=MH_CreateHook(realFunc,newHookProc,&origFunc))!=MH_OK) {
58
LOG_ERROR("MH_CreateHook failed with " << res);
59
FreeLibrary(moduleHandle);
62
g_hookedModules.insert(moduleHandle);
63
g_hookedFunctions.insert(realFunc);
64
LOG_DEBUG("successfully hooked function " << functionName << " in module " << moduleName << " with hook procedure at address 0X" << std::hex << newHookProc << ", returning true");
68
bool apiHook_enableHooks() {
70
res=MH_EnableAllHooks();
75
bool apiHook_terminate() {
77
res=MH_DisableAllHooks();
79
g_hookedFunctions.clear();
80
//Give enough time for all hook functions to complete.
82
res=MH_Uninitialize();
84
for(moduleSet_t::iterator i=g_hookedModules.begin();i!=g_hookedModules.end();++i) {
87
g_hookedModules.clear();