i trying sent keystrokes window in background. of time works good, crushes. i've noticed happens when user presses same key on keyboard @ same time message sent... can please give me advice how avoid crushes? here code call sendmessagecallback:
native.sendasyncproc del = new native.sendasyncproc(helpers.win32.native.sendmessage_callback); native.sendmessagecallback(mainwindowhandle, 256u, (int)system.windows.forms.keys.d3, 0, del, uintptr.zero); thread.sleep(5); native.sendmessagecallback(mainwindowhandle, 257u, (int)system.windows.forms.keys.d3, 0, del, uintptr.zero); and in native:
[dllimport("user32.dll")] public static extern bool sendmessagecallback(intptr hwnd, uint msg, int wparam, int lparam, sendasyncproc lpcallback, uintptr dwdata); public delegate void sendasyncproc(intptr hwnd, uint umsg, uintptr dwdata, intptr lresult); public static void sendmessage_callback(intptr hwnd, uint umsg, uintptr dwdata, intptr lresult) { //logmanager.logerror("sm cb {0}",(datetime.now - td).totalmilliseconds.tostring()); return; } also ive noticed never raises sendmessage_callback... can not understand why till now...
your problem you're trying simulate keyboard input using moral equivalent of postmessage. that doesn't work reliably. in particular, apps in background have lost focus , activation, , many applications rely on focus , activation messages handle input. raymond chen has more how windows handles input here.
also, you're not getting callback because you're not pumping messages while sleeping. use waithandle wait, pump messages while waiting. you'll need set event in callback, of course.
so, if code looked this:
[dllimport("user32.dll")] public static extern bool sendmessagecallback(intptr hwnd, uint msg, int wparam, int lparam, sendasyncproc lpcallback, uintptr dwdata); public delegate void sendasyncproc(intptr hwnd, uint umsg, uintptr dwdata, intptr lresult); waithandle waiter = new eventwaithandle (false, eventresetmode.manualreset); public static void sendmessage_callback(intptr hwnd, uint umsg, uintptr dwdata, intptr lresult) { waiter.set(); return; } native.sendasyncproc del = helpers.win32.native.sendmessage_callback; native.sendmessagecallback(mainwindowhandle, 256u, (int)system.windows.forms.keys.d3, 0, del, uintptr.zero); waiter.waitone(); native.sendmessagecallback(mainwindowhandle, 257u, (int)system.windows.forms.keys.d3, 0, del); waiter.waitone(); you should @ least callbacks. you'll need rethink keyboard simulation scratch, though. consider ui automation, @ bare minimum.
Comments
Post a Comment