delphi - SetWindowsHookEx inside Thread instability -


ok guys, i'll try explain problem in complete form. i'm using 1 dll injected in process (injected using virtualallocex/writeprocessmemory/createremotethread don't matter) , dll when run in entrypoint have 1 thing:

procedure entrypoint(reason: integer); begin   if reason = dll_process_attach     begin       mymainthread.create;     end 

ok, work being done inside mymainthread (tthread)... in mymainthread set 2 timers, , hook keyboard events using setwindowshookex (wh_keyboard_ll). working fine when separately, is: or setwindowshookex or 2 timers... when both things unknown reason hook works few characters typed in keyboard (less 10) , timers stops, mymainthread doesn't terminate. tests on windows 7 / 2008 perfectly, when running in windows 2003 problems started. mymainthread execute this:

procedure mymainthread.execute; begin   while not terminated     begin       mythread:= self;       startkeyboardhook;       startup;       settimer(0, 0, 600000, @mymainthread.contacthome);       settimer(0, 0, 40000, @mymainthread.mapproc);       createmessagepump;     end; end; 

the 2 timers , 'startup' things contact 1 php via indy doing post/get requests, list processes running, , things this... , startkeyboardhook simple this:

procedure mymainthread.startkeyboardhook; begin   if llkeyboardhook = 0     llkeyboardhook:= setwindowshookex(wh_keyboard_ll, @lowlevelkeyboardhook, hinstance, 0); end; 

as can see, startkeyboardhook inside mymainthread , llkeyboardhook/lowlevelkeyboardhook global var/method... if put lowlevelkeyboardhook procedure inside thread don't work hook. believe problem not in lowlevelkeyboardhook (the code itself) because said, if don't setup 2 timers, hook works perfectly, if want can post here. said, hook started inside thread, callback procedure , hhook variable global (maybe that's problem)... createmessagepump (last call in thread's execute) procedure necessary timers , hook, since it's lowlevel hook need message queue. why getting instability (for tests show in win2k3), , if put keyboard hook without timers, or timers without hook, works? messagepump is:

procedure mymainthread.createmessagepump; var   appmsg: tmsg; begin   while getmessage(appmsg, 0, 0, 0)     begin       translatemessage(appmsg);       dispatchmessage(appmsg);     end;   //if needed quit procedure use postquitmessage(0); end; 

some general advice first.

you asked question, previous question, how organise thread's execute method. answer gave accurate. should heed it.

at each , every question have asked on topic myself, sertac , others have told problems of mis-matching function declarations win32 callbacks. seems did not heed advice. continue use broken api declarations provided rtl, , @ operator. in previous question, sertac showed how correct failings of rtl declarations. if cannot check callbacks match must let compiler that.

you said in comments tried sertac's type safe settimer, "didn't work". mis-diagnosis. sertac's code worked perfectly. error received compiler checking callback declared correctly. since not, compiler stopped. desired behaviour. chose ignore compiler, suppress error, , continue broken callback. correct response have been fix callback.

it rather pointless keep asking questions, receive advice, fail heed advice, , ask same question on , again. must heed advice if want make progress. why ask if won't that?


as detail here, see 2 main problems:

  1. the thread loop never terminates. use exact loop gave in previous question. rather copying it, try understand how works , why right.

  2. your timer call functions not match required signatures. cannot instance methods (or class methods matter). should functions declared @ unit scope. must stdcall. parameter lists must match. since finding hard meet these requirements best if use sertac's code earlier question , compiler enforce type safety.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -