Created
June 1, 2012 16:26
-
-
Save xcvd/2853352 to your computer and use it in GitHub Desktop.
Hooks example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------------------------------------------------------- | |
// <copyright file="Hooks.cs" company="Home"> | |
// TODO: Update copyright text. | |
// </copyright> | |
// ----------------------------------------------------------------------- | |
namespace PacketLogger | |
{ | |
using System; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
using Detours; | |
/// <summary> | |
/// TODO: Update summary. | |
/// </summary> | |
public static class Hooks | |
{ | |
/// <summary> | |
/// Address of the CClientSocket::SendPacket function. | |
/// </summary> | |
public static readonly IntPtr SendPacketAddress = (IntPtr)0x004E2C00; | |
/// <summary> | |
/// The maplestory CClientSocket::SendPacket function pointer. | |
/// </summary> | |
public static readonly DSendPacket SendPacketOriginal = (DSendPacket)Marshal.GetDelegateForFunctionPointer(SendPacketAddress, typeof(DSendPacket)); | |
/// <summary> | |
/// Delegate type for CClientSocket::SendPacket function. | |
/// </summary> | |
/// <param name="this"> | |
/// The this pointer (CClientSocket instance). | |
/// </param> | |
/// <param name="packetPointer"> | |
/// The packet pointer. | |
/// </param> | |
/// <returns> | |
/// Unknown int. | |
/// </returns> | |
[UnmanagedFunctionPointer(CallingConvention.ThisCall, SetLastError = true)] | |
public delegate int DSendPacket(IntPtr @this, IntPtr packetPointer); | |
/// <summary> | |
/// Installs all hooks. | |
/// </summary> | |
public static void Install() | |
{ | |
var sendPacketHooked = new DSendPacket(SendPacketHooked); | |
HookManager.Add(SendPacketOriginal, sendPacketHooked, "CClientSocket::SendPacket"); | |
try | |
{ | |
HookManager.Install("CClientSocket::SendPacket"); | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show(e.Message); | |
} | |
} | |
/// <summary> | |
/// Detour for CClientSocket::SendPacket. | |
/// </summary> | |
/// <param name="this"> | |
/// The this pointer (CClientSocket instance). | |
/// </param> | |
/// <param name="packetPointer"> | |
/// The packet pointer. | |
/// </param> | |
/// <returns> | |
/// Unknown int. | |
/// </returns> | |
private static int SendPacketHooked(IntPtr @this, IntPtr packetPointer) | |
{ | |
return (int)HookManager.CallOriginal("CClientSocket::SendPacket", new object[] { @this, packetPointer }); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment