Snippets

Алексей Корепанов C# IChannelHook impl

Created by Алексей Корепанов
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace sbrte_run_test
{
  class Hook : IDisposable
  {
    [DllImport("ole32.dll")]
    static extern int CoRegisterChannelHook([MarshalAs(UnmanagedType.LPStruct)] Guid ExtensionUuid, IChannelHook ChannelHook);

    static private Guid ChannelExtention = new Guid("{CDB80353-F3B3-4767-AA96-D9AC6211B782}");

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("1008C4A0-7613-11CF-9AF1-0020AF6E72F4")]
    [ComImport]
    interface IChannelHook
    {
      [MethodImpl(MethodImplOptions.PreserveSig)]
      void ClientGetSize([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, out int DataSize);

      [MethodImpl(MethodImplOptions.PreserveSig)]
      void ClientFillBuffer([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, ref int DataSize, IntPtr DataBuffer);

      [MethodImpl(MethodImplOptions.PreserveSig)]
      void ClientNotify([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, int DataSize, IntPtr DataBuffer, int lDataRep, int hrFault);

      [MethodImpl(MethodImplOptions.PreserveSig)]
      void ServerNotify([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, int DataSize, IntPtr DataBuffer, int lDataRep);

      [MethodImpl(MethodImplOptions.PreserveSig)]
      void ServerGetSize([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, int hrFault, out int DataSize);

      [MethodImpl(MethodImplOptions.PreserveSig)]
      void ServerFillBuffer([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, ref int DataSize, IntPtr DataBuffer, int hrFault);
    }

    class ChannelHook : IChannelHook
    {
      private readonly Stack<string> stack = new Stack<string>();

      public void ClientFillBuffer([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, ref int DataSize, IntPtr DataBuffer)
      {
        lock (stack)
        {
          var test = stack.Pop();
          var buffer = Encoding.UTF8.GetBytes(test);
          DataSize = test.Length;
          Marshal.Copy(buffer, 0, DataBuffer, DataSize);
        }
      }

      public void ClientGetSize([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, out int DataSize)
      {
        var test = Environment.StackTrace;
        DataSize = test.Length;
        lock (stack)
        {
          stack.Push(test);
        }
      }

      public void ClientNotify([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, int DataSize, IntPtr DataBuffer, int lDataRep, int hrFault)
      {

      }

      public void ServerFillBuffer([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, ref int DataSize, IntPtr DataBuffer, int hrFault)
      {

      }

      public void ServerGetSize([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, int hrFault, out int DataSize)
      {
        DataSize = 0;
      }

      public void ServerNotify([MarshalAs(UnmanagedType.LPStruct)] Guid uExtent, [MarshalAs(UnmanagedType.LPStruct)] Guid iid, int DataSize, IntPtr DataBuffer, int lDataRep)
      {

      }
    }

    #region IDisposable Support
    private bool disposedValue = false;

    protected virtual void Dispose(bool disposing)
    {
      if (!disposedValue)
      {
        if (disposing)
        {
        }

        CoRegisterChannelHook(ChannelExtention, null);

        disposedValue = true;
      }
    }

    public Hook()
    {
      CoRegisterChannelHook(ChannelExtention, new ChannelHook());
    }

    ~Hook()
    {
      Dispose(false);
    }

    public void Dispose()
    {
      Dispose(true);
      GC.SuppressFinalize(this);
    }
    #endregion

  }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.