Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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!


In reply to Re: Timing Windows commands by BrowserUk
in thread Timing Windows commands by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found