[Question] How do I terminate this? menu

Shout-Out

User Tag List

Results 1 to 5 of 5
  1. #1
    nilum's Avatar Member
    Reputation
    9
    Join Date
    Jun 2009
    Posts
    47
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Question] How do I terminate this?

    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);
        }
    }

    [Question] How do I terminate this?
  2. #2
    Jens's Avatar Contributor
    Reputation
    179
    Join Date
    Sep 2006
    Posts
    251
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you might try this instead, just for simplicity's sake:
    Code:
            [DllImport("user32.dll")]
            private static extern short GetAsyncKeyState(Keys vKey);
    
            public static bool IsKeyPressed(Keys Key)
            {
                return 0 != (GetAsyncKeyState(Key) & 0x8000);
            }
    instead of making a keyboard hook, you would simply do:

    Code:
    if(IsKeyPressed(Keys.D1))
    {
         // Do something
    }

  3. #3
    dook123's Avatar Active Member
    Reputation
    21
    Join Date
    Oct 2008
    Posts
    115
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by nilum View Post
    The KeyHook does not terminate, and goes on endlessly.
    This is from the line Application.Run(). It begins the console application that outputs the keys you type.

    I guess I could do the same for unhooking, but I also want to terminate the Keyboard Hook.
    I would not recommend this, Im not 100% sure but it seems like a bad idea unhooking the code from within the hook. I could be wrong and the code may return properly but I would just unhook once you are done.

    To kill a console application hit ctrl+c.

  4. #4
    nilum's Avatar Member
    Reputation
    9
    Join Date
    Jun 2009
    Posts
    47
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to both of you.

    Feels good to finally be making some progress.

  5. #5
    dook123's Avatar Active Member
    Reputation
    21
    Join Date
    Oct 2008
    Posts
    115
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was incorrect, you should be able to unhook the keyhook inside of your method. I was thinking about it last night and it should work so you can do what you did with a certain key making it unhook.

    Congratulations on the progress

Similar Threads

  1. [Question] How do you do this?
    By _DEFiANT in forum Art & Graphic Design
    Replies: 3
    Last Post: 04-13-2009, 11:50 PM
  2. [Question] How many players does this hold?
    By LilleCarl in forum WoW EMU Questions & Requests
    Replies: 5
    Last Post: 01-04-2009, 06:39 AM
  3. [Question] How do i get this effect?
    By Elites360 in forum Art & Graphic Design
    Replies: 3
    Last Post: 09-30-2008, 09:59 AM
  4. [Question] How do I fix this?
    By Godofdeath19 in forum World of Warcraft Emulator Servers
    Replies: 4
    Last Post: 01-05-2008, 04:12 PM
  5. [Question] How do I make this work?
    By 80x08 in forum WoW ME Questions and Requests
    Replies: 2
    Last Post: 10-27-2007, 10:27 AM
All times are GMT -5. The time now is 11:48 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search