2016-11-14 1 views
1

메모장으로 테스트하는 동안 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으로 초기화 된 문자열의 차이점일까요? 나는 그들의 기본 메모리가 어떻게 초기화되었는지에 따라 문자열에서 마셜링 문제를 경험했다.

해결 방법은 텍스트 매개 변수에 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

코드를 보면서 문제를 진단하기가 일반적으로 어렵 기 때문에 코드를 재현해야하므로 시간과 노력이 필요하기 때문에 도움이되기 때문에 기꺼이 도와 드리겠습니다. –

관련 문제