我尝试通过push / retn方法钩住函数,程序编译并成功运行。它应该弹出一个“成功钩子”对话框,但是会弹出一个“不成功钩子”对话框。怎么了?
程序在VS 2017上运行,赢得10
#include "pch.h"
#include <iostream>
#include<Windows.h>
#pragma comment(linker, "/SECTION:.text,ERW")
//the original function
void func() {
MessageBox(0, L"not hook", L"info", MB_OK);
}
void hookproc() {
MessageBox(0, L"hook success",L"info", MB_OK);
}
void hook_code() {
BYTE *lpFunc1 = (BYTE*)func;
lpFunc1[0] = 0x68;//machine code of push
*(ULONG_PTR *)&lpFunc1[1] = (ULONG_PTR)hookproc;//hook function
lpFunc1[5] = 0xC3;//machine code of retn
}
int main()
{
hook_code();
func();
return 0;
}