Okay, this code is based off the example provided here:
http://www.mmowned.com/forums/world-...injection.html
I modified using a KeyHook (also found in that topic), but I am having difficulties figuring out how to get the program to terminate.
The iHook program automatically unhooked once it had completed.
The KeyHook does not terminate, and goes on endlessly.
Right now, the way I have modified the example is so that it runs DoString when a certain key is pressed.
I guess I could do the same for unhooking, but I also want to terminate the Keyboard Hook.
I know this code is horrendous. I am a complete amateur (not even amateur really).
Anyway, I am happy I have at least gotten this far.
Please let me know how I should go about improving the code:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Diagnostics;
using System.Runtime.InteropServices;
using iHook;
namespace iHookExample
{
class Program
{
//process hooking stuff
public static uint Direct3D9__Device = 0x97F7F4;
public static uint Direct3D9__Device__OffsetA = 0x27C4;
public static uint Direct3D9__Device__OffsetB = 0xA8;
public static uint ClntObjMgrGetActivePlayerObjAddress = 0x3580;
public static uint Lua_DoStringAddress = 0x39D8C0;
public static uint Lua_GetLocalizedTextAddress = 0x1C4280;
//keyboard hooking stuff
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
public static void Main(string[] args)
{
//process hooking stuff:
Console.WriteLine("Welcome to the iHook Example.");
Console.WriteLine("Finding wow.");
Process[] processes = Process.GetProcessesByName("Wow");
if (processes.Length < 0)
{
Console.WriteLine("Wow not found.");
Console.ReadLine();
}
else
{
Console.WriteLine("Wow process: " + processes[0].Id + " found!");
Console.WriteLine("Attaching...");
if(Memory.OpenProcess(processes[0].Id))
{
Console.WriteLine("Attached!");
Console.WriteLine("Press any ENTER to continue...");
Console.ReadLine();
//Lets apply the hook.
Console.WriteLine("Applying Hook");
Hook.Apply(GetEndscene(), ClntObjMgrGetActivePlayerObjAddress, Lua_DoStringAddress, Lua_GetLocalizedTextAddress);
//keyboard hooking stuff:
_hookID = SetHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);
Console.WriteLine("Attemping to restore EndScene");
Hook.Restore();
if (!Hook.IsApplied)
{
Console.WriteLine("Hook removed, and EndScene restored.");
Console.ReadLine();
}
}
else
{
Console.WriteLine("Failed to attach!");
Console.ReadLine();
}
}
}
//process hooking stuff:
internal static uint GetEndscene()
{
uint D3D9_Device;
D3D9_Device = Memory.Read<uint>(Memory.BaseAddress + Direct3D9__Device);
D3D9_Device = Memory.Read<uint>(D3D9_Device + Direct3D9__Device__OffsetA);
D3D9_Device = Memory.Read<uint>(D3D9_Device);
D3D9_Device = Memory.Read<uint>(D3D9_Device + Direct3D9__Device__OffsetB);
return D3D9_Device;
}
//keyboard hooking stuff:
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelKeyboardProc(
int nCode, IntPtr wParam, IntPtr lParam);
//most of the magic happens here
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
Console.WriteLine((Keys)vkCode);
string keypressed = ((Keys)vkCode).ToString();
//process hook stuff:
if (keypressed == "D5") //when user presses 5
{
Console.WriteLine("Lets dance!");
Hook.DoString("DoEmote(\"Dance\")");
}
if (keypressed == "D6") //when user presses 6
{
Hook.DoString("freeslots = GetContainerNumFreeSlots(0) + GetContainerNumFreeSlots(1) + GetContainerNumFreeSlots(2) + GetContainerNumFreeSlots(3) + GetContainerNumFreeSlots(4)");
Console.WriteLine("Bag Space: " + Hook.GetLocalizedText("freeslots"));
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}