2012-07-06 36 views
5

Tôi muốn tận dụng máy học để mô hình hóa ý định của người dùng và có khả năng tự động hóa các tác vụ thường được thực hiện. Để thực hiện điều này, tôi muốn có quyền truy cập vào một ống thông tin về hành động của người dùng và trạng thái máy. Để kết thúc này, đó là suy nghĩ hiện tại của tôi rằng việc truy cập vào luồng thông điệp cửa sổ có lẽ là con đường phía trước.Chụp tất cả các Tin nhắn Windows

Tôi muốn có càng nhiều thông tin càng tốt, lọc thông tin vào đó là thích hợp tôi muốn để lại cho công cụ học máy.

Điều này sẽ được thực hiện như thế nào? (Tốt nhất là trong C#).

Hãy giả sử rằng tôi biết cách quản lý và sử dụng lượng lớn dữ liệu này.

Mọi trợ giúp sẽ được đánh giá cao.

+0

C# không thể thực hiện. C++ có thể. – Alex

+0

@alex - tại sao C# không thể thực hiện việc này? –

+0

@ DanielA.White Hàng đợi tin nhắn nền tảng chỉ có thể được xử lý trong mã gốc theo như tôi hiểu, vì vậy nó không giống như C# có thể làm điều đó (được quản lý và tất cả) ... Tôi sẽ hạnh phúc hơn nếu tôi đã sai vì vậy nếu bạn biết khác nhau cảm thấy tự do mâu thuẫn với tôi (tôi đã nghiên cứu các chủ đề bản thân mình không có avail cho một thời gian) – Alex

Trả lời

14

Bạn có thể sử dụng SetWindowsHookEx để đặt móc mức thấp để nắm bắt thông báo cửa sổ (cụ thể). Cụ thể những hook-id có thể là thú vị để theo dõi:

WH_CALLWNDPROC (4) Cài đặt một thủ tục hook theo dõi thông điệp trước khi hệ thống sẽ gửi chúng đến các thủ tục cửa sổ đích. Để biết thêm thông tin về , hãy xem quy trình móc CallWndProc.

WH_CALLWNDPROCRET (12) Cài đặt quy trình móc theo dõi các thông báo sau khi chúng được xử lý theo cửa sổ đích quy trình. Để biết thêm thông tin, hãy xem thủ tục CallWndRetProc hook .

Đã lâu rồi kể từ khi tôi triển khai, nhưng ví dụ tôi đã đăng lớp cơ sở tôi sử dụng để móc các thư cụ thể. (Ví dụ, tôi đã sử dụng nó trong một tra cứu con lăn chuột toàn cầu, đảm bảo rằng các ứng dụng winforms của tôi hoạt động giống như trình thám hiểm internet: cuộn điều khiển bên dưới con trỏ, thay vì điều khiển hoạt động).

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 
using Subro.Win32; 

namespace Subro 
{ 
    /// <summary> 
    /// Base class to relatively safely register global windows hooks 
    /// </summary> 
    public abstract class GlobalHookTrapper : FinalizerBase 
    { 
     [DllImport("user32", EntryPoint = "SetWindowsHookExA")] 
     static extern IntPtr SetWindowsHookEx(int idHook, Delegate lpfn, IntPtr hmod, IntPtr dwThreadId); 

     [DllImport("user32", EntryPoint = "UnhookWindowsHookEx")] 
     private static extern int UnhookWindowsHookEx(IntPtr hHook); 

     [DllImport("user32", EntryPoint = "CallNextHookEx")] 
     static extern int CallNextHook(IntPtr hHook, int ncode, IntPtr wParam, IntPtr lParam); 

     [DllImport("kernel32.dll")] 
     static extern IntPtr GetCurrentThreadId(); 

     IntPtr hook; 
     public readonly int HookId; 
     public readonly GlobalHookTypes HookType; 

     public GlobalHookTrapper(GlobalHookTypes Type):this(Type,false) 
     { 
     } 

     public GlobalHookTrapper(GlobalHookTypes Type, bool OnThread) 
     { 
      this.HookType = Type; 
      this.HookId = (int)Type; 
      del = ProcessMessage; 
      if (OnThread) 
       hook = SetWindowsHookEx(HookId, del, IntPtr.Zero, GetCurrentThreadId()); 
      else 
      { 
       var hmod = IntPtr.Zero; // Marshal.GetHINSTANCE(GetType().Module); 
       hook = SetWindowsHookEx(HookId, del, hmod, IntPtr.Zero); 
      } 

      if (hook == IntPtr.Zero) 
      { 
       int err = Marshal.GetLastWin32Error(); 
       if (err != 0) 
        OnHookFailed(err); 
      } 
     } 

     protected virtual void OnHookFailed(int Error) 
     { 
      throw Win32Functions.TranslateError(Error); 
     } 

     private const int HC_ACTION = 0; 

     [MarshalAs(UnmanagedType.FunctionPtr)] 
     private MessageDelegate del; 

     private delegate int MessageDelegate(int code, IntPtr wparam, IntPtr lparam); 

     private int ProcessMessage(int hookcode, IntPtr wparam, IntPtr lparam) 
     { 
      if (HC_ACTION == hookcode) 
      { 
       try 
       { 
        if (Handle(wparam, lparam)) return 1; 
       } 
       catch { } 
      } 
      return CallNextHook(hook, hookcode, wparam, lparam); 
     } 

     protected abstract bool Handle(IntPtr wparam, IntPtr lparam); 



     protected override sealed void OnDispose() 
     { 
      UnhookWindowsHookEx(hook); 
      AfterDispose(); 
     } 

     protected virtual void AfterDispose() 
     { 
     } 

    } 

    public enum GlobalHookTypes 
    { 
     BeforeWindow = 4, //WH_CALLWNDPROC 
     AfterWindow = 12, //WH_CALLWNDPROCRET 
     KeyBoard = 2, //WH_KEYBOARD 
     KeyBoard_Global = 13, //WH_KEYBOARD_LL 
     Mouse = 7, //WH_MOUSE 
     Mouse_Global = 14, //WH_MOUSE_LL 
     JournalRecord = 0, //WH_JOURNALRECORD 
     JournalPlayback = 1, //WH_JOURNALPLAYBACK 
     ForeGroundIdle = 11, //WH_FOREGROUNDIDLE 
     SystemMessages = 6, //WH_SYSMSGFILTER 
     MessageQueue = 3, //WH_GETMESSAGE 
     ComputerBasedTraining = 5, //WH_CBT 
     Hardware = 8, //WH_HARDWARE 
     Debug = 9, //WH_DEBUG 
     Shell = 10, //WH_SHELL 
    } 

    public abstract class FinalizerBase : IDisposable 
    { 
     protected readonly AppDomain domain; 
     public FinalizerBase() 
     { 
      System.Windows.Forms.Application.ApplicationExit += new EventHandler(Application_ApplicationExit); 
      domain = AppDomain.CurrentDomain; 
      domain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); 
      domain.DomainUnload += new EventHandler(domain_DomainUnload);    
     } 

     private bool disposed; 
     public bool IsDisposed{get{return disposed;}} 
     public void Dispose() 
     { 
      if (!disposed) 
      { 
       GC.SuppressFinalize(this); 
       if (domain != null) 
       { 
        domain.ProcessExit -= new EventHandler(CurrentDomain_ProcessExit); 
        domain.DomainUnload -= new EventHandler(domain_DomainUnload); 
        System.Windows.Forms.Application.ApplicationExit -= new EventHandler(Application_ApplicationExit); 
       } 
       disposed = true; 
       OnDispose(); 
      } 
     } 

     void Application_ApplicationExit(object sender, EventArgs e) 
     { 
      Dispose(); 
     } 

     void domain_DomainUnload(object sender, EventArgs e) 
     { 
      Dispose(); 
     } 

     void CurrentDomain_ProcessExit(object sender, EventArgs e) 
     { 
      Dispose(); 
     } 

     protected abstract void OnDispose(); 
       /// Destructor 
     ~FinalizerBase() 
     { 
      Dispose(); 
     } 
    } 


} 
+1

Tuyệt vời, cảm ơn bạn :) – Marcus

+0

Người đàn ông tuyệt vời .... –

Các vấn đề liên quan