Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Timing Windows commands

by BrowserUk (Pope)
on Feb 03, 2004 at 05:05 UTC ( #326109=note: print w/replies, xml ) Need Help??

in reply to Timing Windows commands

It gets a little complicated obtaining a process handle from the object returned by Win32::Process. You have to ask it for the process id, and then use the Kernel API OpenProcess() to convert that back to a native process handle so that you can call GetProcessTimes().

Once you have the times, they come back as 64-bit values of 100 nano second periods since 1/jan 1601. Unpacking and formating the these time into something reasonable is awkward. My formatting routine is very lazy and doesn't do leading zeros, and you'll need to check the math on the conversion of the kernel and user times. There may be APIs available to do the formatting and converting.

#! perl -slw use strict; use Win32::Process; use Win32::API::Prototype; $|++; ApiLink( 'kernel32', 'BOOL GetProcessTimes( HANDLE hProcess, LPFILETIME lpCreationTime, LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime )' ) or die $^E; ApiLink( 'kernel32', 'HANDLE OpenProcess( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId )' ) or die $^E; ApiLink( 'kernel32', 'BOOL FileTimeToSystemTime( FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime )' ) or die $^E; sub SystemTimeToString{ my( $y, $M, $dow, $d, $h, $m, $s, $milli ) = unpack 's8', $_[ 0 ]; # $dow = (qw[ Sunday Monday Tuesday Wednesday Thursday Friday Satu +rday ])[$dow]; # $d = ( qw[ undef Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec +] )[ $d ]; return "$y/$M/$d $h:$m:$s.$milli"; } Win32::Process::Create( my $pObj, "C:\\windows\\system32\\notepad.exe", "notepad temp.txt", 0, NORMAL_PRIORITY_CLASS, "." ) or die $^E; print 'Paused'; <>; my $pid = $pObj->GetProcessID; print "pid: $pid"; my $hProc = OpenProcess( 0x0400, 1, $pid ) or die $^E; print "hproc: $hProc"; my( $c, $e, $k, $u ) = ('0'x8) x 4; my( $cs, $es ) = ('0'x16) x 2; GetProcessTimes( $hProc, $c, $e, $k, $u ) or die $^E; FileTimeToSystemTime( $c, $cs ) or die $^E; print 'Process created: ', SystemTimeToString( $cs ); FileTimeToSystemTime( $e, $es ) or die $^E; print 'Process ended: ', SystemTimeToString( $es ); printf '%7.5f %7.5f', map{ unpack( 'Nx[N]', $_ ) / 10e8 }$k, $u; __END__ P:\test>326090 Paused pid: 3616 hproc: 72 Process created: 2004/2/3 4:53:50.390 Process ended: 2004/2/3 4:53:50.796 0.00000 15.16372

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Timing (and a little luck) are everything!

Replies are listed 'Best First'.
Re: Re: Timing Windows commands
by eyepopslikeamosquito (Chancellor) on Feb 03, 2004 at 05:28 UTC

    Thankyou very much for providing example code! I may yet take this approach. I was also thinking it may be cleaner to submit a patch for Win32::Process to allow it support GetProcessTimes natively (though this is a bit of a pest because this Win32 call is not supported on Win95 lineage).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://326109]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2018-06-19 11:11 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.