// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <d3d9.h>
#include <D3dx9core.h>
#include <d3dx9math.h>
#pragma comment(lib, "D3D9.lib")
#pragma comment(lib, "D3Dx9.lib")
ULONG_PTR onHookInit();
bool hookDrawIndexedPrimitive();
DWORD oldProtect;
DWORD jmp = NULL;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
hookDrawIndexedPrimitive();
return TRUE;
}
//定义原函数
HRESULT WINAPI or_DrawIndexedPrimitive(
LPDIRECT3DDEVICE9 m_Device,
D3DPRIMITIVETYPE type ,
INT BaseVertexIndex,
UINT MinIndex,
UINT NumVertices,
UINT StartIndex,
UINT PrimitiveCount
) {
_asm {
mov edi,edi
push ebp
mov ebp,esp
mov eax,jmp
jmp eax
}
};
//定义自己的函数
HRESULT WINAPI My_DrawIndexedPrimitive(
LPDIRECT3DDEVICE9 m_Device,
D3DPRIMITIVETYPE type,
INT BaseVertexIndex,
UINT MinIndex,
UINT NumVertices,
UINT StartIndex,
UINT PrimitiveCount
)
{
OutputDebugStringA("OK\r\n");
return or_DrawIndexedPrimitive(m_Device, type,BaseVertexIndex,MinIndex, NumVertices,StartIndex,PrimitiveCount);
};
//获取模块地址 原函数地址
ULONG_PTR onHookInit()
{
ULONG_PTR addr = 0;
HMODULE hModule = LoadLibraryA("D3D9.dll");
addr = (ULONG_PTR) GetProcAddress(hModule, "Direct3DCreate9");
return addr;
}
//开始hook
bool hookDrawIndexedPrimitive()
{
ULONG_PTR addr = onHookInit();
jmp = addr + 5;
if (addr == 0)
{
MessageBoxA(NULL, "error", NULL, 0);
}
if (VirtualProtect((LPVOID)addr, 5, PAGE_EXECUTE_READWRITE, &oldProtect))
{
DWORD value = (DWORD)My_DrawIndexedPrimitive - addr - 5;
_asm {
mov eax, addr
mov byte ptr[eax], 0x9
add eax, 1
mov ebx,value
mov dword ptr[eax], ebx
}
VirtualProtect((LPVOID)addr, 5, oldProtect, &oldProtect);
}
}