c++函数钩子(dll, asm)
c++ function hook(dll, asm)
我编写了一个dll。在这个dll中,我想将另一个dll的函数加载到内存中。这是长时间工作的结果:
typedef int (__fastcall *def_cry)(int a,int b,int fromlen);
def_cry Real_cry;
int __fastcall custom_cry(int a,int b,int fromlen) {
Log("cry ...");
__asm nop;
return Real_cry(a, b, fromlen);
}
DWORD imageBaseOtherDll = 0x39500000;
DWORD functionOffset = 0x395742F8;
DWORD imageBase = (DWORD)GetModuleHandle("otherDll.dll");
DWORD functionOffset = imageBase + (functionOffset - imageBaseOtherDll);
Real_cry = (def_cry)DetourFunction((PBYTE)functionOffset,(PBYTE)&custom_cry);
我的钩子好像坏了。我想我把一些逻辑错误的代码,但我是一个初学者,需要帮助! 您确定要挂接的函数使用__fastcall
调用约定吗?
为了钩住从DLL导出的函数,您需要修补调用或的所有模块(DLL/exe)的导入表,或者在运行时重写函数的入口点。在CodeProject (这里)上可以找到一篇不错的关于修补导入表的文章。关于使用MS Detours的一个很好的教程可以在这里找到。
在调用DetourFunction时,需要提供要钩子的函数的地址。这些值不应该硬编码,因为在DLL中不能保证在特定地址加载。这可以用下面的代码很容易地完成:
// Get the module containing the function to hook
HMODULE targetModule = GetModuleHandle("otherDll.dll");
// Get the address of the function to hook
FARPROC targetFunction = GetProcAddress(targetModule, "cry");
// Go hook it.
Real_cry = (def_cry)DetourFunction((PBYTE)targetFunction,(PBYTE)&custom_cry);
相关文章:
- 挂起和取消挂起一个文件DLL
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 导入库可以跨dll版本工作吗
- 从C++dll访问C#中的一行主要参数
- 链接到自行创建的dll失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- C++:将外部库链接到dll库
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 不同的Visual Studio版本中缺少.dll
- 从DLL中删除类的实例
- 如何包装第三方DLL在R中使用
- 使用c#访问c++dll中带有char*参数的函数时发生AccessViolationException
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 从 DLL 导出 ASM 函数 - Visual Studio C++
- 带有DLL注入的Asm到C++
- c++函数钩子(dll, asm)