Habe das Tutorial angefangen und bin jetzt zu dem Punkt gekommen, wo ich das erste mal die DLL testen kann. Doch leider funktioniert es nicht.
Das ist mein Code:
Code:
// ======================================================================= //
#include "stdafx.h"
#include <windows.h>
#include <cstdio>
#include <d3d9.h>
#include <d3dx9.h>
#pragma once
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
// ======================================================================= //
void InitHook();
void *DetourFunc(BYTE *src, const BYTE *dst, const int len);
HRESULT __stdcall EndScene(LPDIRECT3DDEVICE9 pDevice);
typedef HRESULT(__stdcall* EndScene_t)(LPDIRECT3DDEVICE9);
EndScene_t pEndScene;
const D3DCOLOR txtPink = D3DCOLOR_ARGB(255, 255, 0, 255);
void add_log(char* string);
// ======================================================================= //
int WINAPI DllMain(HINSTANCE hInst,DWORD reason,LPVOID reserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
add_log("Versuche Thread zu erstellen!");
CreateThread(0, 0, (LPTHREAD_START_ROUTINE) InitHook, 0, 0, 0);
break;
}
return true;
}
void DrawRect (LPDIRECT3DDEVICE9 Device_t, int X, int Y, int L, int H, D3DCOLOR color)
{
D3DRECT rect = {X, Y, X+L, Y+H};
Device_t->Clear(1, &rect, D3DCLEAR_TARGET, color, 0, 0);
}
HRESULT __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice)
{
DrawRect ( pDevice, 10, 10, 200, 200, txtPink);
return pEndScene(pDevice);
}
void InitHook() {
add_log("Thread erstellt!");
HMODULE hModule = NULL;
while( !hModule )
{
hModule = GetModuleHandleA( "d3d9.dll" ); // Handle zur DLL holen
Sleep( 100 ); // 100ms warten
}
add_log("d3d9.dll gefunden!");
pEndScene = ( EndScene_t )DetourFunc((PBYTE) 0x0001CE09,(PBYTE)hkEndScene, 5);
}
void *DetourFunc(BYTE *src, const BYTE *dst, const int len) // credits to gamedeception
{
BYTE *jmp = (BYTE*)malloc(len+5);
DWORD dwback;
VirtualProtect(src, len, PAGE_READWRITE, &dwback);
memcpy(jmp, src, len); jmp += len;
jmp[0] = 0xE9;
*(DWORD*)(jmp+1) = (DWORD)(src+len - jmp) - 5;
src[0] = 0xE9;
*(DWORD*)(src+1) = (DWORD)(dst - src) - 5;
VirtualProtect(src, len, dwback, &dwback);
return (jmp-len);
}
void add_log(char* string)
{
HANDLE filehandle;
DWORD dwReadBytes;
char buffer[2048];
filehandle = CreateFile(L"Log.txt", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0);
SetFilePointer(filehandle, 0, 0, FILE_END);
sprintf_s(buffer, 1024, "Added Log: %s\r\n", string);
WriteFile(filehandle, buffer, strlen(buffer), &dwReadBytes, 0);
CloseHandle(filehandle);
}
Die add_log-Funktion habe ich von Dr.Coxxy (aus diesem Thread:
[Only registered and activated users can see links. Click Here To Register...])
Die EndScene-Adresse sollte richtig sein (habe IDA Pro verwendet):
[Only registered and activated users can see links. Click Here To Register...]
Die Log.TXT-Datei spuckt dabei folgendes aus:
Code:
Added Log: Versuche Thread zu erstellen!
Added Log: Thread erstellt!
Added Log: d3d9.dll gefunden!
Vielleicht habe ich auch einfach nur ein Brett vor dem Kopf, wäre euch sehr dankbar für eine Antwort!