0

I'm writing an application in c# and I need to know when the for the ground window has changed I used SetWindowsHookEx but I don't get the call back when I switch between windows

my code:

private const int WH_CALLWNDPROC = 4;

private delegate IntPtr windowName(int nCode, IntPtr wParam, IntPtr lParam);
private static windowName _name = HookCallback;
private static IntPtr _hook = IntPtr.Zero;

public static void start()
{   
    _hook = SetHook(_name);
    Application.Run();
    UnhookWindowsHookEx(_hook);
}

private static IntPtr SetHook(windowName proc)
{
    using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_CALLWNDPROC, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
}

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{   
    browser = GetActiveWindow();
    Console.WriteLine(browser);
    return CallNextHookEx(_hook, nCode, wParam, lParam);
}
ucMedia
  • 4,105
  • 4
  • 38
  • 46
user1839169
  • 207
  • 2
  • 4
  • 16
  • You are not checking for errors at all so you don't know why it doesn't work. You cannot set global hooks like the one you are using in C#. Or WH_SHELL, the one you should be using. It requires injecting a DLL into every process, you cannot inject managed code. Check this project: http://www.codeproject.com/Articles/18638/Using-Window-Messages-to-Implement-Global-System-H – Hans Passant Nov 20 '12 at 17:29
  • this code works for keyboard or mouse events i do use DLL in the process but i dont know how to implement it for the forground window – user1839169 Nov 20 '12 at 18:11

1 Answers1

1

ok i have an answer

    public static void start()
    {
        WinEventDelegate dele = new WinEventDelegate(WinEventProc);
        IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
        string window = GetActiveWindowTitle();
        Console.WriteLine(window);
        while (true)
        {
            if (window != GetActiveWindowTitle())
            {
                window = GetActiveWindowTitle();
                Console.WriteLine(window);
            }
        }
    }

    delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

    private static string GetActiveWindowTitle()
    {
        const int nChars = 256;
        IntPtr handle = IntPtr.Zero;
        StringBuilder Buff = new StringBuilder(nChars);
        handle = GetForegroundWindow();

        if (GetWindowText(handle, Buff, nChars) > 0)
        {
            return Buff.ToString();
        }
        return null;
    }

    public static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
        Console.WriteLine(GetActiveWindowTitle());
    }

    #region imports
    [DllImport("user32.dll")]
    static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

    private const uint WINEVENT_OUTOFCONTEXT = 0;
    private const uint EVENT_SYSTEM_FOREGROUND = 3;

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
    #endregion

its a bit messy but it works

CoderPi
  • 12,985
  • 4
  • 34
  • 62
user1839169
  • 207
  • 2
  • 4
  • 16