.net - SetWindowHookEx fails at runtime in C# application -
i building 1 application use hook, want start applicaton , after build application , applicaton automation window handle of applicaton , setwindowhook. can't it. pls me :( . sr english
variable window hwnd when applicaton windowform running
private bool addwnd(int hwnd, int lparam) { if (iswindowvisible(hwnd)) { stringbuilder sb = new stringbuilder(255); string classname = getclassname((intptr)hwnd).tostring(); if (classname.length > 10) { string getsubstringclassname = classname.substring(0, 11); if (getsubstringclassname.equals("windowsform") && iswindow(hwnd)!=0) { getwindowtext(hwnd, sb, sb.capacity); window = hwnd; } } } return true; } hookproc hookprocedure; private const int wh_cbt = 5; public void starthook() { if (hhook == 0) { hookprocedure = new hookproc(cbthookproc); int threadid = getwindowthreadprocessid((intptr)window, out processhandle); intptr hmod = system.runtime.interopservices.marshal.gethinstance(typeof(form1).module); hhook = setwindowshookex(wh_cbt, hookprocedure, hmod, threadid); if (hhook == 0) { messagebox.show("setwindowshookex failed"); return; } } else { bool ret = unhookwindowshookex(hhook); //if unhookwindowshookex function fails. if (ret == false) { messagebox.show("unhookwindowshookex failed"); return; } hhook = 0; button1.text = "set windows hook"; } }
there 2 types of hooks can install managed .net application: low-level keyboard (wh_keyboard_ll
) , low-level mouse (wh_mouse_ll
) hook. difference callback function these 2 hooks implemented own program. other types of hooks require callback function implemented in dll can injected hooked process(es). doesn't work managed code because managed dlls can't safely injected unmanaged processes.
so no matter fix c# code, never work. computer-based training hook (wh_cbt
) can never installed c# app or dll. need switch different language write hook dll; c , c++ common choices.
or, if care receiving notifications when application creates window, can use accessibility apis instead, work managed apps. start investigating setwineventhook
function. need specify winevent_outofcontext
flag in order ensure notifications delivered process; otherwise injectable dll expected, learned won't work. event_object_create
and/or event_object_show
events ones you're going want monitor.
Comments
Post a Comment