Active Member
Calling Lua Functions from DLL
Hi everyone,
I finally managed to use EasyHook to inject DLLs in some processes and now I was trying to use it for WoW. My DLL gets injected but I am not able to call DoString(). I am just testing DoEmote("dance"); currently but nothing happens. I was logging the steps in my DLL and everything seems fine. Any advice on this would be great.
This is the content of the class from my DLL.
Code:
private static uint Lua;
private static Lua_DoStringDelegate Lua_DoString;
private static Lua_GetStateDelegate Lua_GetState;
private static Lua_RegisterDelegate Lua_Register;
private static Lua_GetTopDelegate Lua_GetTop;
private static Lua_ToStringDelegate Lua_ToString;
public static List<string> Values = new List<string>();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void Lua_DoStringDelegate(string command, string fileName, uint luaState);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void Lua_RegisterDelegate(string name, IntPtr function);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate uint Lua_GetStateDelegate();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate uint Lua_GetTopDelegate(uint luaState);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate string Lua_ToStringDelegate(uint luaState, uint idx, uint length);
[DllImport("kernel32.dll")]
static extern uint SuspendThread(IntPtr hThread);
[DllImport("kernel32.dll")]
static extern uint ResumeThread(IntPtr hThread);
[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentProcess();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int CommandHandlerDelegate(uint luaState);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);
private static int ReturnBytes;
private static readonly CommandHandlerDelegate CmdHandler = InputHandler;
public static IntPtr CmdHandlerPtr = Marshal.GetFunctionPointerForDelegate(CmdHandler);
Hooker.MyInterface Interface;
public static IntPtr WowProcessHandle;
public static IntPtr WowThreadHandle;
public MyDLL(RemoteHooking.IContext InContext, String InChannelName,
IntPtr processHandle,
IntPtr threadHandle)
{
WowProcessHandle = processHandle;
WowThreadHandle = threadHandle;
Interface = RemoteHooking.IpcConnectClient<Hooker.MyInterface>(InChannelName);
}
public void Run(RemoteHooking.IContext InContext, String InChannelName,
IntPtr processHandle,
IntPtr threadHandle)
{
Lua_Register =
(Lua_RegisterDelegate)
Marshal.GetDelegateForFunctionPointer((IntPtr)Constants.Lua_Register, typeof(Lua_RegisterDelegate));
Lua_DoString =
(Lua_DoStringDelegate)
Marshal.GetDelegateForFunctionPointer((IntPtr)Constants.Lua_DoString, typeof(Lua_DoStringDelegate));
Lua_GetState =
(Lua_GetStateDelegate)
Marshal.GetDelegateForFunctionPointer((IntPtr)Constants.Lua_GetState, typeof(Lua_GetStateDelegate));
Lua_GetTop =
(Lua_GetTopDelegate)
Marshal.GetDelegateForFunctionPointer((IntPtr)Constants.Lua_GetTop, typeof(Lua_GetTopDelegate));
Lua_ToString =
(Lua_ToStringDelegate)
Marshal.GetDelegateForFunctionPointer((IntPtr)Constants.Lua_ToString, typeof(Lua_ToStringDelegate));
Lua = Lua_GetState();
Lua_Register("InputHandler", (IntPtr)Constants.PatchOffset);
PatchPointer(CmdHandlerPtr);
// NOW TRY TO DANCE
DoString("DoEmote(\"dance\");");
}
public static int InputHandler(uint luaState)
{
lock (Values)
{
Values.Clear();
uint n = Lua_GetTop(luaState);
for (uint i = 1; i <= n; i++)
{
string res = Lua_ToString(luaState, i, 0);
Values.Add(res);
}
}
return 0;
}
public static void DoString(string command)
{
try
{
string cmd = string.Format("{0}", command);
SuspendThread(WowThreadHandle);
Lua_DoString(cmd, "MyLUA.lua", Lua);
ResumeThread(WowThreadHandle);
}
catch (SEHException e)
{
// log this later
}
}
public static int PatchPointer(IntPtr pointer)
{
bool ReturnVal;
uint p = (uint)pointer - Constants.PatchOffset - 5;
var buffer1 = new byte[4];
var buffer2 = new byte[1];
buffer2[0] = 0xE9;
buffer1[3] = (byte)((p & 0xFF000000) >> 24);
buffer1[2] = (byte)((p & 0xFF0000) >> 16);
buffer1[1] = (byte)((p & 0xFF00) >> 8);
buffer1[0] = (byte)((p & 0xFF));
IntPtr hProcess = GetCurrentProcess();
ReturnVal = WriteProcessMemory(hProcess, (IntPtr)Constants.PatchOffset, buffer2, 1, out ReturnBytes);
ReturnVal = WriteProcessMemory(hProcess, (IntPtr)(Constants.PatchOffset + 1), buffer1, 4, out ReturnBytes);
return ReturnBytes;
}
Last edited by Viano; 07-28-2009 at 02:07 AM.
Viano
These ads disappear when you log in.