Skip to content

Instantly share code, notes, and snippets.

@xcvd
Created June 1, 2012 16:26
Show Gist options
  • Save xcvd/2853352 to your computer and use it in GitHub Desktop.
Save xcvd/2853352 to your computer and use it in GitHub Desktop.
Hooks example
// -----------------------------------------------------------------------
// <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