2011-02-11 23 views
5

में खिड़की हुक सीएसआरपी से अन्य खिड़कियों तक पहुंचने की कोशिश कर रहा है। मैं SetWindowsHookEx का उपयोग कर रहा हूं, लेकिन इसे fom c ​​++ t C# में परिवर्तित करने के साथ कोई भाग्य नहीं है। मुझे यह धागा here मिला लेकिन यह हल नहीं हुआ था।सी #

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowDrawer 
{ 
    public partial class Form1 : Form 
    { 
     private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam); 
     static IntPtr hHook; 
     IntPtr windowHandle; 
     uint processHandle; 

     HookProc PaintHookProcedure;  

     [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] 
     static extern System.IntPtr FindWindowByCaption(int ZeroOnly, string lpWindowName); 

     [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowsHookEx", SetLastError = true)] 
     static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId); 

     [System.Runtime.InteropServices.DllImport("user32.dll")] 
     static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); 

     // When you don't want the ProcessId, use this overload and pass IntPtr.Zero for the second parameter 
     [System.Runtime.InteropServices.DllImport("user32.dll")] 
     static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 

     [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet =System.Runtime.InteropServices.CharSet.Auto)] 
     public static extern IntPtr GetModuleHandle(string lpModuleName); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

      PaintHookProcedure = new HookProc(PaintHookProc); 
      windowHandle = FindWindowByCaption(0, "Untitled - Notepad"); 
      uint threadID = GetWindowThreadProcessId(windowHandle, out processHandle); 
      IntPtr hMod = System.Runtime.InteropServices.Marshal.GetHINSTANCE(typeof(Form1).Module); 

      // HERE IS THE PROBLEM. WHAT THE HECK DO I PASS INTO THE LAST 2 PARAMS? I get a null pointer 
      hHook = SetWindowsHookEx(WH_GETMESSAGE, PaintHookProcedure, hMod, threadID); 


     } 

     public int PaintHookProc(int nCode, IntPtr wParam, IntPtr lParam) 
     { 
      // Do some painting here. 
      return CallNextHookEx(hHook, nCode, wParam, lParam); 
     } 

     private const int WM_PAINT = 15; 
     private const int WH_GETMESSAGE = 3; 
    } 
} 

किसी भी मदद की, सलाह: समस्या SetWindowsHookEx रिटर्न कि 0. यह सबसे अच्छा कोड samle मैंने पाया भी शामिल है?

उत्तर

1

क्या आपने EasyHook प्रोजेक्ट देखा है? यह एक सुंदर सक्रिय परियोजना प्रतीत होता है। माइक्रोसॉफ्ट के पास उनकी साइट पर an example भी है।

+0

वे अपनी खिड़की पर झुका रहे हैं, इसलिए 3-रेड पैरा 0 है। मुझे अन्य प्रक्रियाओं को हुक करने की आवश्यकता है। – b0xer

+0

लेकिन आपका पहला लिंक दिलचस्प है। मैं इस परियोजना के बारे में पढ़ा। – b0xer

8

WH_GETMESSAGE हुक एक वैश्विक हुक है। इसके लिए एक डीएलएल की आवश्यकता होती है जिसे किसी अन्य प्रक्रिया में इंजेक्शन दिया जा सकता है। HMod तर्क। एक समस्या है, आप एक प्रबंधित भाषा में ऐसे डीएलएल नहीं लिख सकते हैं। लक्ष्य प्रक्रिया में सीएलआर शुरू नहीं होगा।

एक code project है जो एक डीएलएल प्रदान करता है, शायद आप इसे काम कर सकते हैं। ब्लैक बेल्ट की आवश्यकता है।

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^