EDN Admin
Well-known member
I am trying to hook function that takes 2 aruments that are wchar_t *wMessage, int nColor but when I try to compile it I get 1 error. Could anyone be so nice and tell me what causes this error and how could I fix this.
This code is
<div style="color:Black;background-color:White; <pre>
#undef UNICODE
#include <windows.h>
#include <cstdio>
#define SIZE 6 <span style="color:Green; //Number of bytes needed to redirect
<span style="color:Blue; typedef <span style="color:Blue; int (WINAPI *pMessageBoxW)(HWND, LPCWSTR, LPCWSTR, UINT);
<span style="color:Blue; void __stdcall MyMessageBoxW(<span style="color:Blue; wchar_t* , <span style="color:Blue; int);
<span style="color:Blue; void BeginRedirect(LPVOID);
pMessageBoxW pOrigMBAddress = NULL;
BYTE oldBytes[SIZE] = {0}; <span style="color:Green; //This will hold the overwritten bytes
BYTE JMP[SIZE] = {0}; <span style="color:Green; //This holds the JMP to our code
DWORD oldProtect, myProtect = PAGE_EXECUTE_READWRITE; <span style="color:Green; //Protection settings on memory
<span style="color:Blue; char debugBuffer[128]; <span style="color:Green; //Used for DbgView
INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
<span style="color:Blue; switch(Reason)
{
<span style="color:Blue; case DLL_PROCESS_ATTACH:
pOrigMBAddress = (pMessageBoxW) <span style="color:Green; //Get MessageBoxW pointer
0x6FB2D850;<span style="color:Green; // MY CUSTOM ADDRESS IN GAME
<span style="color:Blue; if(pOrigMBAddress != NULL)
BeginRedirect(MyMessageBoxW); <span style="color:Green; //Valid? Redirect
<span style="color:Blue; break;
<span style="color:Blue; case DLL_PROCESS_DETACH:
memcpy(pOrigMBAddress, oldBytes, SIZE);
<span style="color:Blue; case DLL_THREAD_ATTACH:
<span style="color:Blue; case DLL_THREAD_DETACH:
<span style="color:Blue; break;
}
<span style="color:Blue; return TRUE;
}
<span style="color:Blue; void BeginRedirect(LPVOID newFunction)
{
sprintf_s(debugBuffer, 128, <span style="color:#A31515; "pOrigMBAddress: %x", pOrigMBAddress);
OutputDebugString(debugBuffer);
BYTE tempJMP[SIZE] = {0xE9, 0x90, 0x90, 0x90, 0x90, 0xC3}; <span style="color:Green; //JMP <NOP> RET for now
memcpy(JMP, tempJMP, SIZE); <span style="color:Green; //Copy into global for convenience later
DWORD JMPSize = ((DWORD)newFunction - (DWORD)pOrigMBAddress - 5); <span style="color:Green; //Get address difference
VirtualProtect((LPVOID)pOrigMBAddress, SIZE, PAGE_EXECUTE_READWRITE, &oldProtect);
<span style="color:Green; //Change memory settings to make sure we can write the JMP in
memcpy(oldBytes, pOrigMBAddress, SIZE); <span style="color:Green; //Copy old bytes before writing JMP
sprintf_s(debugBuffer, 128, <span style="color:#A31515; "Old bytes: %x%x%x%x%x", oldBytes[0], oldBytes[1],
oldBytes[2], oldBytes[3], oldBytes[4], oldBytes[5]);
OutputDebugString(debugBuffer);
memcpy(&JMP[1], &JMPSize, 4); <span style="color:Green; //Write the address to JMP to
sprintf_s(debugBuffer, 128, <span style="color:#A31515; "JMP: %x%x%x%x%x", JMP[0], JMP[1],
JMP[2], JMP[3], JMP[4], JMP[5]);
OutputDebugString(debugBuffer);
memcpy(pOrigMBAddress, JMP, SIZE); <span style="color:Green; //Write it in process memory
VirtualProtect((LPVOID)pOrigMBAddress, SIZE, oldProtect, NULL); <span style="color:Green; //Change setts back
}
<span style="color:Blue; void WINAPI MyMessageBoxW(<span style="color:Blue; wchar_t* Message, <span style="color:Blue; int color)
{
VirtualProtect((LPVOID)pOrigMBAddress, SIZE, myProtect, NULL); <span style="color:Green; //ReadWrite again
memcpy(pOrigMBAddress, oldBytes, SIZE); <span style="color:Green; //Unhook API
MessageBoxW(NULL, L<span style="color:#A31515; "This should pop up", L<span style="color:#A31515; "Hooked MBW", MB_ICONEXCLAMATION);
memcpy(pOrigMBAddress, JMP, SIZE); <span style="color:Green; //Rehook API
VirtualProtect((LPVOID)pOrigMBAddress, SIZE, oldProtect, NULL); <span style="color:Green; //Normal setts
}
[/code]
<br/>
<br/>
View the full article
This code is
<div style="color:Black;background-color:White; <pre>
#undef UNICODE
#include <windows.h>
#include <cstdio>
#define SIZE 6 <span style="color:Green; //Number of bytes needed to redirect
<span style="color:Blue; typedef <span style="color:Blue; int (WINAPI *pMessageBoxW)(HWND, LPCWSTR, LPCWSTR, UINT);
<span style="color:Blue; void __stdcall MyMessageBoxW(<span style="color:Blue; wchar_t* , <span style="color:Blue; int);
<span style="color:Blue; void BeginRedirect(LPVOID);
pMessageBoxW pOrigMBAddress = NULL;
BYTE oldBytes[SIZE] = {0}; <span style="color:Green; //This will hold the overwritten bytes
BYTE JMP[SIZE] = {0}; <span style="color:Green; //This holds the JMP to our code
DWORD oldProtect, myProtect = PAGE_EXECUTE_READWRITE; <span style="color:Green; //Protection settings on memory
<span style="color:Blue; char debugBuffer[128]; <span style="color:Green; //Used for DbgView
INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
<span style="color:Blue; switch(Reason)
{
<span style="color:Blue; case DLL_PROCESS_ATTACH:
pOrigMBAddress = (pMessageBoxW) <span style="color:Green; //Get MessageBoxW pointer
0x6FB2D850;<span style="color:Green; // MY CUSTOM ADDRESS IN GAME
<span style="color:Blue; if(pOrigMBAddress != NULL)
BeginRedirect(MyMessageBoxW); <span style="color:Green; //Valid? Redirect
<span style="color:Blue; break;
<span style="color:Blue; case DLL_PROCESS_DETACH:
memcpy(pOrigMBAddress, oldBytes, SIZE);
<span style="color:Blue; case DLL_THREAD_ATTACH:
<span style="color:Blue; case DLL_THREAD_DETACH:
<span style="color:Blue; break;
}
<span style="color:Blue; return TRUE;
}
<span style="color:Blue; void BeginRedirect(LPVOID newFunction)
{
sprintf_s(debugBuffer, 128, <span style="color:#A31515; "pOrigMBAddress: %x", pOrigMBAddress);
OutputDebugString(debugBuffer);
BYTE tempJMP[SIZE] = {0xE9, 0x90, 0x90, 0x90, 0x90, 0xC3}; <span style="color:Green; //JMP <NOP> RET for now
memcpy(JMP, tempJMP, SIZE); <span style="color:Green; //Copy into global for convenience later
DWORD JMPSize = ((DWORD)newFunction - (DWORD)pOrigMBAddress - 5); <span style="color:Green; //Get address difference
VirtualProtect((LPVOID)pOrigMBAddress, SIZE, PAGE_EXECUTE_READWRITE, &oldProtect);
<span style="color:Green; //Change memory settings to make sure we can write the JMP in
memcpy(oldBytes, pOrigMBAddress, SIZE); <span style="color:Green; //Copy old bytes before writing JMP
sprintf_s(debugBuffer, 128, <span style="color:#A31515; "Old bytes: %x%x%x%x%x", oldBytes[0], oldBytes[1],
oldBytes[2], oldBytes[3], oldBytes[4], oldBytes[5]);
OutputDebugString(debugBuffer);
memcpy(&JMP[1], &JMPSize, 4); <span style="color:Green; //Write the address to JMP to
sprintf_s(debugBuffer, 128, <span style="color:#A31515; "JMP: %x%x%x%x%x", JMP[0], JMP[1],
JMP[2], JMP[3], JMP[4], JMP[5]);
OutputDebugString(debugBuffer);
memcpy(pOrigMBAddress, JMP, SIZE); <span style="color:Green; //Write it in process memory
VirtualProtect((LPVOID)pOrigMBAddress, SIZE, oldProtect, NULL); <span style="color:Green; //Change setts back
}
<span style="color:Blue; void WINAPI MyMessageBoxW(<span style="color:Blue; wchar_t* Message, <span style="color:Blue; int color)
{
VirtualProtect((LPVOID)pOrigMBAddress, SIZE, myProtect, NULL); <span style="color:Green; //ReadWrite again
memcpy(pOrigMBAddress, oldBytes, SIZE); <span style="color:Green; //Unhook API
MessageBoxW(NULL, L<span style="color:#A31515; "This should pop up", L<span style="color:#A31515; "Hooked MBW", MB_ICONEXCLAMATION);
memcpy(pOrigMBAddress, JMP, SIZE); <span style="color:Green; //Rehook API
VirtualProtect((LPVOID)pOrigMBAddress, SIZE, oldProtect, NULL); <span style="color:Green; //Normal setts
}
[/code]
<br/>
<br/>
View the full article