Jump to content
Sign in to follow this  

how can I measure the time of computer use?

Recommended Posts


I'm working to a time limitation program, so how I would like to know if there is a function to measure time; if it doesn't exists, please suggest me a similar function.

Share this post

Link to post
Share on other sites


Go and read the Help file. There are plenty of commands in AutoIt to read and measure both time and date.

If you run into problems, post the code here and we will help you get over them. :(


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites

There is not one function I know of... but maybe this gives you want you need :(...

It extracts when explorer.exe was created... not sure, but I think it might be the time the user logged on for the first time...


$tFiletime = _WinAPI_GetProcessCreationTime(ProcessExists("explorer.exe"))
ConsoleWrite(_WinAPI_FileTimeConvert(DllStructGetData($tFiletime,1),DllStructGetData($tFiletime,2)) & @crlf)

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_GetProcessCreationTime
; Description....: Retrieves the creation time of the process.
; Syntax.........: _WinAPI_GetProcessCreationTime( [$PID] )
; Parameters.....: $PID    - The PID of the process. Default (0) is the current process.
; Return values..: Success - $tagFILETIME structure that contains the creation time of the process.
;                  Failure - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ GetProcessTimes
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_GetProcessCreationTime($PID = 0)

    If Not $PID Then
        $PID = _WinAPI_GetCurrentProcessID()
        If Not $PID Then
            Return SetError(1, 0, 0)

    Local $hProc = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'dword', 0x00000400, 'int', 0, 'dword', $PID)

    If (@error) Or ($hProc[0] = 0) Then
        Return SetError(1, 0, 0)

    $hProc = $hProc[0]

    Local $tFILETIME = DllStructCreate('dword;dword')
    Local $Ret = DllCall('kernel32.dll', 'int', 'GetProcessTimes', 'ptr', $hProc, 'ptr', DllStructGetPtr($tFILETIME), 'ptr*', 0, 'ptr*', 0, 'ptr*', 0)

    If (@error) Or ($Ret[0] = 0) Then
        $Ret = 0
    If Not IsArray($Ret) Then
        Return SetError(1, 0, 0)
    Return $tFILETIME
EndFunc   ;==>_WinAPI_GetProcessCreationTime

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_FreeHandle
; Description....: Closes an open object handle.
; Syntax.........: _WinAPI_FreeHandle ( $hObject )
; Parameters.....: $hObject - Handle to an open object.
; Return values..: Success  - 1.
;                  Failure  - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: The _WinAPI_FreeHandle() function closes handles to the following objects:
;                  Access token
;                  Communications device
;                  Console input
;                  Console screen buffer
;                  Event
;                  File
;                  File mapping
;                  I/O completion port
;                  Job
;                  Mailslot
;                  Memory resource notification
;                  Mutex
;                  Named pipe
;                  Pipe
;                  Process
;                  Semaphore
;                  Thread
;                  Transaction
;                  Waitable timer
; Related........:
; Link...........: @@MsdnLink@@ CloseHandle
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_FreeHandle($hObject)

    Local $Ret = DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $hObject)

    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    Return 1
EndFunc   ;==>_WinAPI_FreeHandle

; ===================================================================================================
; Func _WinAPI_FileTimeConvert($iFileDateTimeLo,$iFileDateTimeHi,$DLL=-1)
; Internal method to Convert Date-Time double-dword values stored in Windows FILETIME structures
;   NOTE: It *has* to be passed as two dwords, otherwise there are issues with 64-bit parameters.
; When used with _WinAPI_FileFind.. functions, the *PREFERRED* METHOD of calling this is:
;   _WinAPI_FileFindTimeConvert()
; $iFileDateTimeLo = Lo word of date/time info returned in a the FILETIME structure
; $iFileDateTimeHi = Hi word of date/time info returned in a FILETIME structure.
; $DLL = DLL handle or -1
; Return:
;   Success: formatted string : YYYYMMDDHHMMSS (24-hour clock)
;   Failure: empty string, with @error=2 (DLL call fail)
; Author: Ascend4nt
; ===================================================================================================

Func _WinAPI_FileTimeConvert($iFileDateTimeLo,$iFileDateTimeHi,$DLL=-1)
;Func _WinAPI_FileTimeConvert($iFileDateTimeLo,$iFileDateTimeHi,$DLL=-1)
    Local $sDateTimeStr,$stLocalFileTime,$stFileTime,$stSystemTime,$aRet
    ; FILETIME structures [DateTimeLo,DateTimeHi]
    ; SYSTEMTIME structure [Year,Month,DayOfWeek,Day,Hour,Min,Sec,Milliseconds]

    If $DLL==-1 Then $DLL="Kernel32.dll"
    ; Set the appropriate data members of the FileTime structure

    ; First convert file time (UTC-based file time) to 'local file time'
    If @error Or Not IsArray($aRet) Or Not $aRet[0] Then Return SetError(2,0,"")

    ; Then convert file time to a system time structure
    If @error Or Not IsArray($aRet) Or Not $aRet[0] Then Return SetError(2,0,"")

    ; Now format it and return it in a string. Format: YYYYMMDDHHSSMM
    $sDateTimeStr = DllStructGetData($stSystemTime,1) & StringRight('0' & DllStructGetData($stSystemTime,2),2) & _
        StringRight('0' & DllStructGetData($stSystemTime,4),2) & _
        StringRight('0' & DllStructGetData($stSystemTime,5),2) & StringRight('0' & DllStructGetData($stSystemTime,6),2) & _
        StringRight('0' & DllStructGetData($stSystemTime,7),2)

    ; DLLStructDelete()'s

    Return $sDateTimeStr

Share this post

Link to post
Share on other sites

PStools - every Windows Admin's friend:

Sysinternals PSTools

I believe the particular tool you want to call on is "psloggedon.exe" running the aforementioned command w/o switches will give you the current logon status of all local users, including the user's original logon time (I've been logged into this machine for 36 weeks??? Madness!!)

use psloggedon.exe /? for a list of switches and options

Be nice to the newbies... we were all there once...[size="3"]Being right too soon is socially unacceptable. - R. Heinlein[/size]My Projects:OpenKiosk Browser

Share this post

Link to post
Share on other sites

This is not fast but it's about as accurate as you can get.

$sTempFile = @TempDir & "\sys.tmp"
$sSystem = RunWait(@ComSpec & " /c " & 'SystemInfo > ' & $sTempFile, "", @SW_HIDE)
$s_Str = FileRead($sTempFile)
$aBootTime = StringRegExp($s_Str, "(?i)(?m:^).+time:\s*(\d.*)\v", 1)
If Not @Error Then
    $s_Rtn = StringRegExpReplace($aBootTime[0], "\s*,\s*", ",")
    $a_Rtn = StringSplit($s_Rtn, ",", 2)

Where $a_Rtn[0] Contains the startup date and $a_Rtn[1] contains the time. You could easily work this up into a function.


Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver: - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this