delphi - Error when I try EnumProcessModules in windows 8 64bit -


i'm trying pick modules of process, according msdn not possible handle modules th32cs_snapmodule function in 32bit applications , using enumprocessmodules function can not list modules of defined process , go infinite loop.

i had success compiling application windows 64bit.

here code error !

procedure getmodule(processid: cardinal); var   modules: array of hmodule;   cbneeded, i: cardinal;   moduleinfo: tmoduleinfo;   modulename: array[0..max_path] of char;   phandle: thandle;   item: tlistitem; begin   setlength(modules, 1024);   phandle := openprocess(process_query_information + process_vm_read, false, processid);   if (phandle <> 0)   begin     enumprocessmodules(phandle, @modules[0], 1024 * sizeof(hmodule), cbneeded); //getting enumeration of modules     setlength(modules, cbneeded div sizeof(hmodule)); //setting number of modules     := 0 length(modules) - 1 //start loop     begin       item := form1.listview2.items.add;       item.caption := inttostr(i); // testing     end;     closehandle(phandle);   end; end; 

you don't enter infinite loop; enter long loop. when enumprocessmodules fails, evidently sets cbneeded := 0. sets length of array zero, too. enter loop starting @ 0 , ending @ –1. interpreted cardinal, value –1 4294967295. takes long (but non-infinite) time count high, when you're adding items list view along way. got tired of waiting program , killed before finished running loop.

there @ least 2 changes need make in order proceed program:

  1. declare i integer calculating length(modules) - 1 doesn't underflow.

  2. pay attention return values of api calls. never ignore api return value. msdn says enumprocessmodules returns 0 when fails. check that. if returns zero, call getlasterror find out reason.

    also, don't assume 1024 big enough array; it's possible cbneeded set higher number. when happens, you'll iterate on more array items received. (the values null handles.)


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -