2016-11-14 4 views
1

私は、ヘルプ - >バージョン情報を開いて、メモ帳でテストしているときに、EasyTookへのEasyHookのフックコールを使用しています。しかし、「ファイル」 - >「開く」を開くと、メモ帳がクラッシュします。私はそれがテキストをキャプチャするとは思っていませんが、なぜノートパッドがクラッシュするのか理解できません。どんな助けもありがとう。EasyHook DrawTextW user32.dll Injection、Application crashing

using EasyHook; 
using System; 
using System.Text; 
using System.Runtime.InteropServices; 
using System.Threading; 
using UI; 

namespace MyClassLibrary 
{ 
    public class Main : IEntryPoint 
    { 
     [StructLayout(LayoutKind.Sequential)] 
     public struct HDC__ 
     { 
      public int unused; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     public struct tagRECT 
     { 
      public int left; 
      public int top; 
      public int right; 
      public int bottom; 
     } 

     [DllImport("user32.dll", EntryPoint = "DrawTextW")] 
     public static extern int DrawTextW([In()] IntPtr hdc, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpchText, int cchText, ref tagRECT lprc, uint format); 


     [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)] 
     public delegate int TDrawTextW(
      [In()] IntPtr hdc, 
      [MarshalAs(UnmanagedType.LPWStr)] 
      StringBuilder lpchText, 
      int cchText, 
      ref tagRECT lprc, 
      uint format); 


     static string ChannelName; 
     RemoteMon Interface; 

     LocalHook DrawTextWHook; 
     public Main(RemoteHooking.IContext InContext, String InChannelName) 
     { 
      try 
      { 
       Interface = RemoteHooking.IpcConnectClient<RemoteMon>(InChannelName); 
       ChannelName = InChannelName; 
       Interface.IsInstalled(RemoteHooking.GetCurrentProcessId()); 
      } 
      catch (Exception ex) 
      { 
       Interface.ErrorHandler(ex); 
      } 
     } 

     static int hkDrawTextW(
        [In()] IntPtr hdc, 
        [MarshalAs(UnmanagedType.LPWStr)] 
        StringBuilder lpchText, 
        int cchText, 
        ref tagRECT lprc, 
        uint format) 
     { 
      try 
      { 
       ((Main)HookRuntimeInfo.Callback).Interface.GotDrawTextW(lpchText); 
       return DrawTextW(hdc, lpchText, cchText, ref lprc, format); 
      } 
      catch (Exception ex) 
      { 
       ((Main)HookRuntimeInfo.Callback).Interface.ErrorHandler(ex); 
       return 0; 
      } 
     } 

     public void Run(RemoteHooking.IContext InContext, String InChannelName) 
     { 
      try 
      { 
       DrawTextWHook = LocalHook.Create(LocalHook.GetProcAddress("user32.dll", "DrawTextW"), 
               new TDrawTextW(hkDrawTextW), this); 
       DrawTextWHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 }); 
      } 
      catch (Exception ex) 
      { 
       Interface.ErrorHandler(ex); 
      } 
      try 
      { 
       RemoteHooking.WakeUpProcess(); 
      } 
      catch (Exception ex) 
      { 
       Interface.ErrorHandler(ex); 
      } 
      while (true) 
      { 
       Thread.Sleep(10000); 
      } 
     } 
    } 
} 
+0

メモ帳にデバッガを接続し、それが役立つかどうかを確認することをお勧めします。また、元の値を返す以外の何もしないようにフック関数を変更してみてください。 –

+0

私は前にGotDrawTextW呼び出しをコメントアウトしようとしましたし、同じ結果を得ました。私はデバッガでより多くの情報を見つけることができるかどうかを調べるつもりです。 – williamt

答えて

0

[開く]ダイアログが表示されたときに、テキストをStringBuilderにマーシャリングする際に問題があります。私はその理由を100%確信していませんが、おそらくCOM初期化文字列とmallocで初期化された文字列の違いは?私は、それらの基礎となるメモリがどのように初期化されたかに応じて、文字列のマーシャリング問題を過去に経験しました。

解決策は、textパラメータにStringBuilderの代わりにStringを使用することです。 externとdelegate、およびその他の適切な領域を文字列パラメータで置き換えます。

[DllImport("user32.dll", EntryPoint = "DrawTextW")] 
    public static extern int DrawTextW([In()] IntPtr hdc, [MarshalAs(UnmanagedType.LPWStr)] String lpchText, int cchText, ref tagRECT lprc, uint format); 

    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)] 
    public delegate int TDrawTextW(
     [In()] IntPtr hdc, 
     [MarshalAs(UnmanagedType.LPWStr)] 
     String lpchText, 
     int cchText, 
     ref tagRECT lprc, 
     uint format); 

    static int hkDrawTextW(
       [In()] IntPtr hdc, 
       [MarshalAs(UnmanagedType.LPWStr)] 
       String lpchText, 
       int cchText, 
       ref tagRECT lprc, 
       uint format) 
    { 
     ... 
    } 
+0

私の問題をうまく解決していただきありがとうございます。このタイプのものに関する情報を見つけるのは難しいです。 – williamt

+0

コードを見るだけで問題を診断するのが一般的に難しく、しばしばコードでそれを再現する必要があり、時間と労力がかかるため、これらを手助けするのは難しいです。 –

関連する問題