[Re] RE: [fpc-pascal]getprocesstimes- help wanted pls

tom_at_work at yline.com tom_at_work at yline.com
Thu Apr 12 19:37:02 CEST 2001


Hello,

>Thanks for prog - works ok under win2000 & I can get times for processes...
>BTW here is the way I get process ids etc - I received the original befoire
>I hacked it around from someone else via [fpc-pascal] a while ago..., so
>thanks to them for it!

Well, it doesn't do what it should.... it does something very close to retrieving processes though.

>
>BTW when I compare the o/p from your prog with the 'windows task manager'
>list of processes (on win2000) I get error from your program & no times o/p
>for process with pid=0 (system idle process).  I'm really trying to get the
>system idle process cpu time... ie time processor isn't foing anything.
>Anyone any idea on best way to get this in win2000? 

Well, after taking a look at the docs for a second time I think I found out how it's done properly (and working with all NT versions). The way to do this is parsing the 'HKEY_PERFORMANCE_DATA' key in the registry....
Since this process of retrieving this data is more complex I am sorry but I don't have the time to create a sample application; neither I do have something already working. I recommend reading the 'Performance Monitoring' chapter in the MSDN docs in detail, there is lots of information about how to do that though (and some small example applications using C).

> 
[... skipping some stuff ...]
>
>Function OurEnumWindowsCallBack(TheHWND:HWND;  LV:Longint):Longint; 
>stdcall; export;
>var S  :String;
>    C  :String;
>    PID : DWORD;
>    PDD : DWORD;
>    t1,t2,t3,t4:dword;
>    xx:int64;    
>BaseAddr : DWORD;
>    TmpHandle, tmphandle1 : HANDLE;
>    f : File;
>    NumRead : DWORD;
>    P : Pointer;
>    TmpB : WinBool;
>    n,j,k,l,m:longint; 
>    eq:boolean; 
>Begin
>  Inc(WCount,1);
>  S[0] := Chr( Lo(Lo(GetWindowTextA(TheHWND, @S[1], 255))) );
>  C[0] := Chr( Lo(Lo(GetClassNameA(TheHWND, @C[1], 255))) );
>      PDD := GetWindowThreadProcessId(TheHWND, @PID);
>
>      Write(StrHex(TheHWND,8),', "',S,'" - "',C,'"');
>
>      Write('   [PDD=',strhex(PDD,8),', PID=',strhex(PID,8),' ',pid,']');
>
>      TmpHandle := OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);
>      If GetLastError=0 then  { I'm doing this part wrong I think! }
>        begin

Replace this line with:

"if (TmpHandle <> -1) then begin"

and it works as it should (not as it is intended to) imho. Because it only enumerates _windows_ and retrieves the associated process (and threads, but a few lines of code already seem to be missing ;)  but not the processes directly. E.g. processes not having allocated a window handle are not listed (this includes the idle process btw), so my approach is actually better.
It is also possible (very likely) that one process has more than one window open and you get lots of data several times.

>    
>      Writeln(',  = ',GetPriorityClass(Tmphandle));
>    tmpb:=getprocesstimes(tmphandle, at t1, at t2, at t3, at t4);
>    if tmpb then 
>begin 
>

Regards,
	Thomas

---------------------------------------------------------------------
Sent through MailGateway - http://www.ssw.uni-linz.ac.at:2000/
Send or read your emails anywhere.
---------------------------------------------------------------------




More information about the fpc-pascal mailing list