I am also stuck with this WriteCallback function from Apoc's LUA Wrapper
Code:
private static IntPtr WriteLuaCallback(IntPtr CallbackPtr)
{
uint InvalidPtr = (uint)Luas.Lua_InvalidPtrCheck; // From Apoc's thread: 0x0046ED80
int BytesWritten;
Log.Output("WriteLuaCallback() - Starting ...");
bool ReturnVal;
uint p = (uint)CallbackPtr - InvalidPtr - 5;
var buf = new byte[4];
var buf2 = new byte[1];
buf2[0] = 0xE9;
buf[3] = (byte)((p & 0xFF000000) >> 24);
buf[2] = (byte)((p & 0xFF0000) >> 16);
buf[1] = (byte)((p & 0xFF00) >> 8);
buf[0] = (byte)((p & 0xFF));
IntPtr hProcess = Kernel32.OpenProcess(Kernel32.ProcessAccessFlags.All, false, (uint)Memory.ProcessId); // Memory is the instance of BlackMagic that I use
Log.Output("WriteLuaCallback() - hProcess = {0:X}", (uint)hProcess);
ReturnVal = Kernel32.WriteProcessMemory(hProcess, (IntPtr)InvalidPtr, buf2, 1, out BytesWritten);
if (!ReturnVal) { Log.Output(LogType.Error, "WriteLuaCallback() - Error during first WriteProcessMemory"); }
Log.Output("WriteLuaCallback() - Written {0:d} bytes", BytesWritten);
ReturnVal = Kernel32.WriteProcessMemory(hProcess, (IntPtr)((uint)InvalidPtr + 1), buf, 4, out BytesWritten);
if (!ReturnVal) { Log.Output(LogType.Error, "WriteLuaCallback() - Error during second WriteProcessMemory"); }
Log.Output("WriteLuaCallback() - Written {0:d} bytes", BytesWritten);
Log.Output("WriteLuaCallback() - Success");
return CallbackPtr;
}
from my log I get
Code:
22:00:05 WriteLuaCallback() - Starting ...
22:00:05 WriteLuaCallback() - hProcess = 490
22:00:05 WriteLuaCallback() - Written 1 bytes
22:00:05 WriteLuaCallback() - Written 4 bytes
22:00:05 WriteLuaCallback() - Success
Yet I crash with an AccessViolationException:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
On this line:
RegisterCommandHandler(commandName, WriteLuaCallback(Marshal.GetFunctionPointerForDelegate(handler)));
EDIT: Just realized I am using old offsets, lol
I have updated everything except InvalidPtrCheck, does anyone have it?
Code:
internal enum Luas
{
Lua_DoString = 0x007F25C0,
Lua_Register = 0x007F1340,
Lua_GetTop = 0x00826D80,
Lua_ToString = 0x00827290,
Lua_InvalidPtrCheck = 0x0046ED80, // not updated
}