使用Microsoft Detours时发生访问违规
Access violation when using Microsoft Detours
我在使用Microsoft Detours时遇到访问违规问题。我制作了一个dll,它被加载到第三方应用程序中。我正在使用Detours对Ida Pro显示为的未记录功能进行蹦床功能
void __thiscall sub_6142E0(int a2, int a3)
我的代码如下所示:#包括"stdafx.h"#包括#包括
typedef void(__stdcall* pFunc)(int d1, int d2);
pFunc FuncToDetour = (pFunc)(0x6142EC);
void MyFunc(int d1, int d2)//Function does not mach call convension __thiscall. Possible problem?
{
printf("a2 %i, a1 %i);n", d1, d2);
FuncToDetour(d1, d2);
}
void Init()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)FuncToDetour, MyFunc);
DetourTransactionCommit();
}
我想要拦截的函数的原始集合是这样的:
sub_6142E0 proc near
arg_0= dword ptr 8
arg_4= dword ptr 0Ch
push ebp
mov ebp, esp
mov eax, [ecx+8]
mov ecx, [ebp+arg_4]
mov edx, [ebp+arg_0]
Detours的改变导致了以下结果:
.text:006142EC jmp near ptr unk_F9C6802
...
d3d9.dll:0F9C6802 jmp near ptr unk_F9D5FE0 //jump to function in my dll
...
void MyFunc(int d1, int d2)//my function
{
printf("updateHealth(%i, %i);n", d1, d2);
}
...
Stack[00004A8C]:0019FB4C sub ah, bh
Stack[00004A8C]:0019FB4E sbb [eax], eax //eax=0x491B -> access violation
Stack[00004A8C]:0019FB50 cmc
Stack[00004A8C]:0019FB51 inc si
Stack[00004A8C]:0019FB53 add [eax], dl
Stack[00004A8C]:0019FB55 add [eax], eax
Stack[00004A8C]:0019FB57 add [eax+80019FDh], cl
Stack[00004A8C]:0019FB5D add byte_19FC6415[eax], dh
Stack[00004A8C]:0019FB5D ; -------------------------------------------------
我得到的错误信息是:
The instruction 0x19FB4E referenced memory at 0x491B. The memory could not be written -> 0000491B (exc.code c0000005, tid 19084)
我要试着回答我自己的问题。
这归结为两个函数之间的调用约定不匹配。我想要挂接的函数使用__thiscall,而我的函数则使用__cdecl(默认调用约定)__this调用被用作类中成员函数的调用约定,在调用成员函数时,"this指针"在ecx寄存器中传递。
在我的例子中,ecx是在调用MyFunc以设置堆栈框架时写入的(我认为)。然后,当我从蹦床函数调用我挂钩的函数时,它会得到一个无效的this指针。
请查看此链接,了解如何正确执行此操作的一些解释和示例。
相关文章:
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 用于访问容器<T>数据成员的正确 API
- 访问者访问变体并返回不同类型时出错
- 尝试通过多个向量访问变量时,向量下标超出范围
- 无法访问嵌套类.类的使用无效
- 写入位置0x0000000C时发生访问冲突
- 我们可以访问一个不存在的联盟的成员吗
- C++从另一个类访问公共静态向量的正确方法是什么
- 我的简单if-else语句是如何无法访问的代码
- 从C++dll访问C#中的一行主要参数
- 概念TS检查忽略私有访问修饰符
- 访问被拒绝后,c++中的故障保护代码
- 在c++中访问int到类对象的映射时出错
- Microsoft访问 ODBC 驱动程序管理器函数序列错误
- Microsoft访问问题
- 使用Microsoft Detours时发生访问违规
- 从c++程序访问Microsoft Word文档
- (Neessitas)Microsoft sql数据库访问在打开时出错