views:

1509

answers:

3

I'm trying to hook a 3rd party app so that I can draw to its screen. Drawing to the screen is easy and I need no help with it, but I seem to be having issues with using SetWindowsHookEx to handle WH_GETMESSAGE. I can't figure out what to pass for the last 2 parameters.

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;
    }





}
+1  A: 

I believe you need to P/Invoke GetModuleHandle and use the handle it returns for the third parameter of SetWindowsHookEx. I also believe 0 is correct for the fourth parameter, as you don't want to hook any one specific thread in the third-party application.

If this doesn't work for you, SetWindowsHookEx on MSDN might point you in the right direction.

Adam Maras
GetModuleHandle(string lpModuleName); What do I pass into it? The exe name?
Darthg8r
Pass `NULL` and it'll pull the handle from the calling process (your executable.)
Adam Maras
+1  A: 

SetWindowsHookEx specifics the last two parameters thusly:

  • hMod

[in] Handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.

  • dwThreadId

[in] Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.

I'm not sure you can use a .NET dll in the manner required, but you can certainly try.

Grab hMod via Marshal.GetHINSTANCE(typeof(Form1).Module) and dwThreadId via Process.Threads. Alternatively, set dwThreadId to 0 if you want a global hook (ie. a hook for all GetMessage() calls in the current desktop) but beware of the performance penalties.

Kevin Montrose
I modded the code above to reflect your idea. I'm still getting SetWindowsHookEx(WH_GETMESSAGE, PaintHookProcedure, hMod, threadID) == 0;
Darthg8r
There is the distinct possibility that you need to package your PaintHookProcedure in a separate DLL; which precludes the use of .NET. Grab the error message code with GetLastError(), and see what the problem is.
Kevin Montrose
And by GetLastError() I mean Marshal.GetLastWin32Error(); p/invoking GetLastError() directly is unreliable.
Kevin Montrose
GetLastError is returning 1428. That's an invalid hMod. Any ideas?
Darthg8r
Check to see if the return from `Marshal.GetHINSTANCE` is -1. If that's the case, you need to move your hook proc into a DLL, which means you need to write it in unmanaged code.
Kevin Montrose
+1  A: 

I don't know but if you're using parameter values which specify that you want to, as the API helps says, "inject a DLL into another process", then for all I know it might work only if you write an unmanaged DLL from which to call it.

ChrisW