locked
SetWindowsHookEx

    Question

  • Spent all day get this one working... no luck ..
    Im trying to hook up to another window using SetWindowsHookEx but it just doesnt want to work from c# ..
    It doesnt throw an error.. just doesnt hook ..
    i tried to hook to the c# window and it hooked up.. but I need it to hook to another window ..
    btw, GetLastError returns no error ..

    WinAPI.SetWindowsHookEx( WinAPI.HookType.WH_CALLWNDPROC, hook, Marshal.GetHINSTANCE( System.Reflection.Assembly.GetExecutingAssembly( ).GetModules( )[0] ), WinAPI.GetWindowThreadProcessId( lhWnd, IntPtr.Zero ) );


    /////
    public static extern IntPtr SetWindowsHookEx( HookType hook, HookProc callback, IntPtr hMod, uint dwThreadId );
    public static extern uint GetWindowThreadProcessId( IntPtr hWnd, IntPtr lpdwProcessId );
    public delegate IntPtr HookProc( int code, IntPtr wParam, IntPtr lParam );
    public enum HookType : int {
    WH_CALLWNDPROC = 4
    }

    Tuesday, June 06, 2006 4:38 PM

Answers

  • Not if the thread was created by another process, no. You can still write the hook library in native code and consume it from a C# application.

     

    Wednesday, June 07, 2006 8:57 AM

All replies

  • You can't do this in C# or any other managed language.

     

    Wednesday, June 07, 2006 7:16 AM
  • So there is no way I can hook to a window which is in another thread in c#?? thats kinda sucks..
    I would really like to know if there is any solution for that... on MSDN article about managed hooks I read a remark of a possability to hook on to a window from another thread ..

    Thanks
    Wednesday, June 07, 2006 7:52 AM
  • Not if the thread was created by another process, no. You can still write the hook library in native code and consume it from a C# application.

     

    Wednesday, June 07, 2006 8:57 AM
  • Thanks, thats a great idea !
    Wednesday, June 07, 2006 11:29 AM
  •  

    hi ALL

    i had a problem when i made Local hock Like that

    hKeyboardHook = SetWindowsHookEx(
                       WH_KEYBOARD,
                       KeyboardHookProcedure,
                       IntPtr.Zero,
                       (int)AppDomain.GetCurrentThreadId());

    i face this unhandeld Exception "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    this Exception fire when i try to monitor the KeyBoard Only but not occur with mouse


    M.ADEL
    Wednesday, August 27, 2008 8:03 AM
  • Can you show us your code for the KeyboardHookProcedure?

    Also keep in mind that the delegate you pass to SetWindowsHookEx must be kept alive until you unhook.
    Mattias, C# MVP
    Wednesday, August 27, 2008 8:16 AM
  • this the code

    private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)

    {

    //indicates if any of underlaing events set e.Handled flag

    bool handled = false;

    //it was ok and someone listens to events

    if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))

    {

    //read structure KeyboardHookStruct at lParam

    KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

    //raise KeyDown

    if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))

    {

    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;

    KeyEventArgs e = new KeyEventArgs(keyData);

    KeyDown(this, e);

    handled = handled || e.Handled;

    }

    // raise KeyPress

    if (KeyPress != null && wParam == WM_KEYDOWN)

    {

    bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);

    bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

    byte[] keyState = new byte[256];

    GetKeyboardState(keyState);

    byte[] inBuffer = new byte[2];

    if (ToAscii(MyKeyboardHookStruct.vkCode,

    MyKeyboardHookStruct.scanCode,

    keyState,

    inBuffer,

    MyKeyboardHookStruct.flags) == 1)

    {

    char key = (char)inBuffer[0];

    if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key);

    KeyPressEventArgs e = new KeyPressEventArgs(key);

    KeyPress(this, e);

    handled = handled || e.Handled;

    }

    }

    // raise KeyUp

    if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))

    {

    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;

    KeyEventArgs e = new KeyEventArgs(keyData);

    KeyUp(this, e);

    handled = handled || e.Handled;

    }

    }

    //if event handled in application do not handoff to other listeners

    if (handled)

    return 1;

    else

    return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);

    }


    M.ADEL
    Wednesday, August 27, 2008 8:28 AM
  • mohamed adel_10 said:

    //read structure KeyboardHookStruct at lParam

    KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));




    lParam for a WH_KEYBOARD hook is not a pointer to a structure. Maybe you have it confused with WH_KEYBOARD_LL?

    http://msdn.microsoft.com/en-us/library/ms644984(VS.85).aspx


    Mattias, C# MVP
    Wednesday, August 27, 2008 9:02 AM