GetProcessTimes in Kernel32.dll

Chris Kelleher

Administrator
Staff member
Hi,

Mr.Mondial (is it a pseudonym??) wrote on the peg-list:

> Hi
>
> Anyone try to get process CPU time using Win32 API
> (kernel32.dll)?
>
> Please enlighten me.
>
> Thanks

I've got a simple test-programm for that:

<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>

/* -------------------------------------------------------------------------
----
// File: e:\eo\tst_procTime.p
// Desc: query the process-times of the current process
//
// Parm: ---
//
//
// Author: Michael Rüsweg-Gilbert
// Created: 20. Sept. 1999
----------------------------------------------------------------------------
- */
DEF VAR RetVal AS INT NO-UNDO.
DEF VAR me_Crea AS MEMPTR NO-UNDO.
DEF VAR me_Exit AS MEMPTR NO-UNDO.
DEF VAR me_Kern AS MEMPTR NO-UNDO.
DEF VAR me_User AS MEMPTR NO-UNDO.
DEF VAR hProc AS INT NO-UNDO.
DEF VAR PID AS INT NO-UNDO.

&GLOB TRUE 1
&GLOB FALSE 0
&GLOB PROCESS_ALL_ACCESS 2035711 /* 0xF0000 | 0X100000 | 0xFFF */

/* Convert FileTime into a readable LocalTime-String */
function proTimeString returns char
( ip_filetime as memptr):
def var tmp_sysTime as memptr no-undo.
def var Ret as int no-undo.
def var cTime as char no-undo init ?.

set-size(tmp_sysTime) = 16.
/* Convert UTC-Time to Local Time */
RUN FileTimeToSystemTime ( INPUT ip_filetime,
OUTPUT tmp_systime,
OUTPUT Ret ).
if Ret = {&TRUE} then do:
/* a DAY.MONTH.YEAR HOUR:MINUTE:SECOND-string */
cTime = string(get-short(tmp_sysTime, 7)) + "." +
string(get-short(tmp_sysTime, 3)) + "." +
string(get-short(tmp_sysTime, 1)) + " " +
string(get-short(tmp_sysTime, 9)) + ":" +
string(get-short(tmp_sysTime, 11)) + ":" +
string(get-short(tmp_sysTime, 13)).
end.

set-size(tmp_sysTime) = 0.

if cTime <> ?
then return cTime.
else return "Error in FileTimeToSystemTime; Ret=" + string(Ret).
end function.


/* 1st obtain the current Process Token (add Debug rights) */
RUN GetCurrentProcessId(OUTPUT PID).

RUN OpenProcess ( {&Process_All_Access},
0,
PID,
OUTPUT hProc).
if hProc < 1 THEN DO:
MESSAGE "Can't open current PID" PID
VIEW-AS ALERT-BOX INFO BUTTONS OK.
RETURN.
END.
HProc0:
DO:

set-size(me_Crea) = 8.
set-size(me_Exit) = 8.
set-size(me_Kern) = 8.
set-size(me_User) = 8.

RUN GetProcessTimes ( hProc,
me_Crea,
me_Exit,
me_Kern,
me_User,
OUTPUT RetVal).
if RetVal <> {&TRUE} then do:
message "GetProcessTimes returned" RetVal
view-as alert-box.
leave.
end.

message "Creation Time:" ProTimeString(me_Crea) skip
"Exit Time:" ProTimeString(me_Exit) skip
"Kernel Time:" ProTimeString(me_Kern) skip
"User Time:" ProTimeString(me_User)
view-as alert-box.


end.

set-size(me_Crea) = 0.
set-size(me_Exit) = 0.
set-size(me_Kern) = 0.
set-size(me_User) = 0.

RUN CloseHandle ( hProc, OUTPUT RetVal).


RETURN.


PROCEDURE CloseHandle EXTERNAL "kernel32":
def input parameter hObject as long .
def return parameter retval as long .
END PROCEDURE.

PROCEDURE GetCurrentProcessId EXTERNAL "kernel32":
def return parameter PID as long .
END PROCEDURE.

PROCEDURE GetLastError EXTERNAL "kernel32":
def return parameter dwError as long .
END PROCEDURE.

PROCEDURE OpenProcess EXTERNAL "kernel32.dll" :
DEFINE INPUT PARAMETER dwDesiredAccess AS LONG.
DEFINE INPUT PARAMETER bInheritHandle AS LONG.
DEFINE INPUT PARAMETER dwProcessId AS LONG.
DEFINE RETURN PARAMETER hProcess AS LONG.
END PROCEDURE.

PROCEDURE GetProcessTimes EXTERNAL "kernel32.dll" :
DEFINE INPUT PARAMETER hProcess AS LONG.
DEFINE INPUT PARAMETER lpCreationTime AS MEMPTR.
DEFINE INPUT PARAMETER lpExitTime AS MEMPTR.
DEFINE INPUT PARAMETER lpKernelTime AS MEMPTR.
DEFINE INPUT PARAMETER lpUserTime AS MEMPTR.
DEFINE RETURN PARAMETER RetBool AS LONG.
END PROCEDURE.

PROCEDURE FileTimeToSystemTime EXTERNAL "KERNEL32.DLL":
define INPUT parameter lpFileTime as memptr. /* L = 8 */
define OUTPUT parameter lpSystemTime as memptr. /* L = 16 */
define return parameter retBool as LONG. /* = 0, if
failure */
END PROCEDURE.

[/code]

have fun & bye

Michael Rüsweg-Gilbert
mrz Kaufring AG
Düsseldorf
Germany
rg@kaufring.de
 
Top