Jump to content
Sign in to follow this  
layer

GetSystemTimes with DllStruct! help

Recommended Posts

I've always wanted to be able to get CPU of, well, the computer : Except my compiler conditionally compiles the Dll I tried creating based on your OS because of GetSystemTimes... Grr... But, now I'm pretty sure I can do it with AutoIt... I must be doing something wrong because the MsgBox always contains 50! And my CPU isn't always 50! I checked in Task manager :)

$idle = DllStructCreate("dword")
$kernel = DllStructCreate("dword")
$user = DllStructCreate("dword")
$idleptr = DllStructPtr($idle)
$kernelptr = DllStructPtr($kernel)
$userptr = DllStructPtr($user)

$call = DllCall("kernel32.dll", "int", "GetSystemTimes", "ptr", $idleptr, "ptr", $kernelptr, "ptr", $userptr)
$sys = $kernelptr + $userptr
$cpu = int( ($sys - $idleptr) *100 / $sys )
MsgBox(0, " kernelptr", $kernelptr, )
MsgBox(0, " userptr", $userptr, )
MsgBox(0, " idleptr", $idleptr, )
MsgBox(0, " sys", $sys, )
MsgBox(0, " getsystemtimes call", $call[0])
MsgBox(0, "CPU", $cpu)
DllStructFree($idle)
DllStructFree($kernel)
DllStructFree($user)

Also, with the first three lines of code for what I supplied, can I just use one DllStructCreate? I wouldn't think so though because it's three seperate structures. Heres some reference for GetSystemTimes...

http://www.codeproject.com/threads/Get_CPU_Usage.asp

and

http://msdn.microsoft.com/library/default....systemtimes.asp

Thanks... I love testing this stuff :(

EDIT: I added some debug messageboxes to find I was getting SOME results, have a look for yourself :P But I still need to get the CPU :(

Edited by layer

FootbaG

Share this post


Link to post
Share on other sites

This what you want it to do?

$idle = DllStructCreate("dword")
$kernel = DllStructCreate("dword")
$user = DllStructCreate("dword")

$call = DllCall("kernel32.dll", "int", "GetSystemTimes",_
                "ptr", DllStructPtr($idle),_
                "ptr", DllStructPtr($kernel),_
                "ptr", DllStructPtr($user))
$sys = DllStructGet($kernel,1) + DllStructGet($user,1)
$cpu = int( ($sys - DllStructGet($idle,1)) *100 / $sys )
MsgBox(0, "", " kernel " & DllStructGet($kernel,1) & @crlf & _
    " user " & DllStructGet($user,1) & @CRLF & _
    " idle " & DllStructGet($idle,1) & @CRLF & _
    " sys "  & $sys & @CRLF & _
    " getsystemtimes call " & $call[0] & @CRLF & _
    " CPU " & $cpu)
DllStructFree($idle)
DllStructFree($kernel)
DllStructFree($user)

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs

Share this post


Link to post
Share on other sites

Sorry didnt goto MSDN first

typedef struct _FILETIME {

  DWORD dwLowDateTime;

  DWORD dwHighDateTime;

} FILETIME,

*PFILETIME;

You declared them as a single DWORD, reading more you can try:

$idle = DllStructCreate("int64")
$kernel = DllStructCreate("int64")
$user = DllStructCreate("int64")

My results were all positive numbers after that.

Made a new script:

#include <GUIConstants.au3>

$idle = DllStructCreate("int64")
$kernel = DllStructCreate("int64")
$user = DllStructCreate("int64")

GUICreate("Timer Test",300,300)
$h= GuiCtrlCreatelabel("",10,10,290,290)
GUISetState()

While 1
    $msg = GUIGetMSG()
    if $msg = $GUI_EVENT_CLOSE Then exitloop
    if $msg = 0 Then
        $call = DllCall("kernel32.dll", "int", "GetSystemTimes",_
                        "ptr", DllStructPtr($idle),_
                        "ptr", DllStructPtr($kernel),_
                        "ptr", DllStructPtr($user))
        $sys = DllStructGet($kernel,1) + DllStructGet($user,1)
        $cpu = int( ($sys - DllStructGet($idle,1)) *100 / $sys )
        GUICtrlSetData($h, " kernel " & DllStructGet($kernel,1) & @crlf & _
            " user " & DllStructGet($user,1) & @CRLF & _
            " idle " & DllStructGet($idle,1) & @CRLF & _
            " sys "  & $sys & @CRLF & _
            " getsystemtimes call " & $call[0] & @CRLF & _
            " CPU " & $cpu,1)
    EndIf
Wend

DllStructFree($idle)
DllStructFree($kernel)
DllStructFree($user)
Edited by Ejoc

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs

Share this post


Link to post
Share on other sites

Crap, I was going to try int64 because I know I saw that somewhere on the site : Well, your GUI example is always saying my CPU is 29 :( I'll hvae a look into tomorrow, or later on... Just got home... :(


FootbaG

Share this post


Link to post
Share on other sites

Garr!

MSDN:

Requirements

Client Requires Windows XP SP1.

Server Requires Windows Server 2003.

Surely an equivalent exist for 2000? (And NT. Possibly not for 9x?)

Edit.. Added:

If @error Then
            $error = DllCall ("kernel32.dll", "int", "GetLastError")
            MsgBox (0, "Error", $error[0])
            Exit
        EndIf

after the DllCall and it returns 127, which interprets as "The specified procedure could not be found"

Edited by Smed

601DisengageEnd Program

Share this post


Link to post
Share on other sites

I have SP2, I'm not sure about the server stuff... But it might not just work on my system, for some weird reason, because Dev-Cpp even spits out a compile error for my Dll about not being able to find GetSystemTimes in my winbase.h but it's in there, but conditionally compiled :(


FootbaG

Share this post


Link to post
Share on other sites

Stands for Central Processing Unit, a programmable logic device that performs all the instruction, logic, and mathematical processing in a computer.

- www.ontrack.com/glossary/

: This was my first test with this, but I'm so excited, because there was so many functions people wanted in DllCall that required Structs, and I'd always see Larry quote the "Structure" part out from MSDN and say, "structs...". Good work, really! :(:(


FootbaG

Share this post


Link to post
Share on other sites

What is CPU suppose to be?

<{POST_SNAPBACK}>

I think he was refering to your statement about your CPU being 29 ... He's asking what your CPU should be.

We have enough youth. How about a fountain of SMART?

Share this post


Link to post
Share on other sites

I think he was refering to your statement about your CPU being 29 ... He's asking what your CPU should be.

<{POST_SNAPBACK}>

Oooops, well, system takes like 100% CPU, but that's subtracted away I think from the average. I guess, under normal circumstances it should be like 0% or 5%, not too sure though. But I was gonna make a monitering program that moniters your CPU... But guess I can't :( Unless... I opened up Windows Task Manager, made it hide, and do a "StatusBarGetText"... But that's cheap :( Maybe I'll do it anyway...

FootbaG

Share this post


Link to post
Share on other sites

Here is what I have so far for Win2000. I apologize for not maintaining forward compatibility. It appears to work, but under close scrutiny, the timing numbers are all wrong.

#include <GUIConstants.au3>

$systemInfo = DllStructCreate ("short;short;dword;int;int;int;dword;dword;dword;short;short")
$processorInfo = DllStructCreate ("int64;int64;int64;int64[2];int")

Opt ("GUIOnEventMode", 1)
GUICreate ("Foo", 300, 300)
$font = "Lucida Console"
GUISetFont (9, 400, 0, $font)
$h = GuiCtrlCreatelabel ("", 10 , 10 ,280, 280)
GUISetState ()
GUISetOnEvent($GUI_EVENT_CLOSE, "DieNow")

DllCall ("kernel32.dll", "int", "GetSystemInfo", "ptr", DllStructPtr ($systemInfo))

$processorArchitecture = DllStructGet ($systemInfo, 1)
$numberOfProcessors = DllStructGet ($systemInfo, 7)
$processorType = DllStructGet ($systemInfo, 8)
$processorLevel = DllStructGet ($systemInfo, 10)
$processorRevision = DllStructGet ($systemInfo, 11)

$baseInfo = "Number of Processors:   " & $numberOfProcessors & @CRLF & _
            "Processor Architecture: " & $processorArchitecture & @CRLF & _
            "Processor Type:         " & $processorType & @CRLF & _
            "Processor Level:       " & $processorLevel & @CRLF & _
            "Processor Revision:     " & $processorRevision & @CRLF

$sic = 23 ; this was hard to find.
$pIStructSize = DllStructSize ($processorInfo)

$lastSystemTime = 0
$lastUpTime = 0

Do
    $call = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", "int", $sic, _
                                                                    "ptr", DllStructPtr($processorInfo), _
                                                                    "int", $pIStructSize, _
                                                                    "int", 0)
    If @error Then
        $error = DllCall ("kernel32.dll", "int", "GetLastError")
        MsgBox (0, "Error", $error[0])
        Exit
    EndIf

; who really needs to know the time with 10 picosecond granularity?
    $idleTime = int (DllStructGet ($processorInfo, 1)/100000000)/1000
    $kernelTime = int (DllStructGet ($processorInfo, 2)/100000000)/1000
    $userTime = int (DllStructGet ($processorInfo, 3)/100000000)/1000
    $systemTime = $kernelTime + $userTime
    $upTime = $systemTime + $idleTime
    $cpuUtilization = Round (($systemTime - $lastSystemTime)/($upTime - $lastUpTime)*100, 2)
    $updateInfo = $baseInfo & @CRLF & _
                  "Idle:   " & _ConvertSecondsToTimeString ($idleTime) & @CRLF & _
                  "Kernel: " & _ConvertSecondsToTimeString ($kernelTime) & @CRLF & _
                  "User:   " & _ConvertSecondsToTimeString ($userTime) & @CRLF & _
                  "System: " & _ConvertSecondsToTimeString ($systemTime) & @CRLF & _
                  "Up:   " & _ConvertSecondsToTimeString ($upTime) & @CRLF & _
                  "CPU: " & $cpuUtilization & "%"
    GUICtrlSetData ($h, $updateInfo)
    Sleep (1000)
    $lastSystemTime = $systemTime
    $lastUpTime = $upTime
Until 0

DllStructFree ($systemInfo)
DllStructFree ($processorInfo)

Func DieNow ()

  Exit
EndFunc

Func _ConvertSecondsToTimeString ($p1)
    
    $lp1 = $p1
    $seconds = Mod ($lp1, 60)
    $lp1 -= $seconds
    $lp1 /= 60
    $seconds = Round ($seconds, 2)
    $minutes = Mod ($lp1, 60)
    $lp1 -= $minutes
    $lp1 /= 60
    $hours = Mod ($lp1, 24)
    $lp1 -= $hours
    $lp1 /= 24
    $days = $lp1 ; stopping here, not concerned about years at the moment
    $out = ""
    If $days Then $out = $days & "Days "
    $out &= $hours & ":"
    $out &= $minutes & ":"
    $out &= $seconds
    Return $out
EndFunc

Does anyone have a real copy of WINTERNL.H?


601DisengageEnd Program

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...