Search code examples
c#setwindowshookex

get findow title from setwindowhook


first of all im not sure if Im doing this right(setSetWindowsHookEx). I want to be notified of all windows created from a process(third party application) and get the title to see if it is a window I want to do something with, if it is then get his position and place some forms on it and also SetWinEventHook so if this window moves the forms will keep beeing on the window. How can I get the title from the new window?

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public enum HookType : int
    {
        WH_JOURNALRECORD = 0,
        WH_JOURNALPLAYBACK = 1,
        WH_KEYBOARD = 2,
        WH_GETMESSAGE = 3,
        WH_CALLWNDPROC = 4,
        WH_CBT = 5,
        WH_SYSMSGFILTER = 6,
        WH_MOUSE = 7,
        WH_HARDWARE = 8,
        WH_DEBUG = 9,
        WH_SHELL = 10,
        WH_FOREGROUNDIDLE = 11,
        WH_CALLWNDPROCRET = 12,
        WH_KEYBOARD_LL = 13,
        WH_MOUSE_LL = 14
    }
    public enum ShellEvents : int {
        HSHELL_WINDOWCREATED = 1,
        HSHELL_WINDOWDESTROYED = 2,
        HSHELL_ACTIVATESHELLWINDOW = 3,
        HSHELL_WINDOWACTIVATED = 4,
        HSHELL_GETMINRECT = 5,
        HSHELL_REDRAW = 6,
        HSHELL_TASKMAN = 7,
        HSHELL_LANGUAGE = 8,
        HSHELL_ACCESSIBILITYSTATE = 11
    }


    private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
    static IntPtr hHook;


    HookProc myCallbackDelegate;



    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);

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


    [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);
   //const int HSHELL_WINDOWCREATED = 1;

    public int MyCallbackFunction(int nCode, IntPtr wParam, IntPtr lParam)
    {

        if (Convert.ToInt32 (nCode) >0 )//HSHELL_WINDOWCREATED)
        {

            //MessageBox.Show("hola");
        }

       //Is this correct or should be on a else?

        return CallNextHookEx(hHook, nCode, wParam, lParam);
    }


    [DllImport("kernel32.dll")]
    static extern uint GetCurrentThreadId();
    private void Form1_Load(object sender, EventArgs e)
    {
        myCallbackDelegate = new HookProc(MyCallbackFunction);
       processHandle);



        hHook = SetWindowsHookEx(HookType.WH_SHELL, myCallbackDelegate, (IntPtr)0,GetCurrentThreadId());

    }
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool UnhookWindowsHookEx(IntPtr hhk);
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        UnhookWindowsHookEx(hHook);
    }
}

Solution

  • SetWindowsHookEx is a function which give the right to install a hook (local not global except for mouse and keyboard))

    to have the title of windows, you could use that link List Processes