Thanks for your replies, just got back from holidays, couldn't answer earlier 
This is the code I quickly wrote, I really can't figure out why it always results in Access Violations ( Instruction at 0x12345678 attempted to write at 0x12345678 (where 0x12345678 is the address of my lua function ) after being called ingame via the /script command:
Code:
template < typename pfunction_t >
pfunction_t WriteFunctionJump(LPVOID targetAddress, pfunction_t function)
{
#pragma pack(push)
#pragma pack(1)
struct Jmp
{
BYTE instruction;
pfunction_t function;
};
#pragma pack(pop)
Jmp jmp = { 0xE9, function };
DWORD oldProtect;
::VirtualProtect( targetAddress,
sizeof(Jmp),
PAGE_EXECUTE_READWRITE,
&oldProtect);
::memcpy(targetAddress, static_cast<LPVOID>(&jmp), sizeof(Jmp));
return static_cast<pfunction_t>(targetAddress);
}
Code:
FrameScript::RegisterFunction("DoSomething", Utilities::WriteFunctionJump<lua_Function>( /*Some Codecave I use*/, CustomLua::DoSomething));
It's probably something stupid I guess.