AutoHotkey Community

It is currently May 24th, 2012, 9:13 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: March 26th, 2007, 10:18 pm 
Offline

Joined: April 20th, 2006, 9:54 pm
Posts: 21
Hi,

I'm trying to query the windows performance counters with dllcall without any luck. The problem is that I do not know anything about the windows api.

I found the msdn pages here: http://msdn2.microsoft.com/en-us/library/aa373214.aspx

This statement seems to work (phdStatus is 0):
Code:
pdhStatus := DllCall("pdh.dll\PdhOpenQuery", "Int", 0, "UInt", 0, "UInt", &hQuery)


After this I do:
Code:
pdhStatus := DllCall("pdh.dll\PdhAddCounterA", "UInt", hQuery, "str", "\Processor(_Total)\% Processor Time", "UInt", 0, "UInt", &hCounter)

at this point phdStatus = -1073738820 and I am lost.

Can somebody help?

Thanks,

Bold

PS. I edited this post to add some info.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2007, 10:53 pm 
Offline

Joined: April 20th, 2006, 9:54 pm
Posts: 21
I found out that -1073738820 is actually 0xc0000bbc.

This means 0xC0000BBC (PDH_INVALID_HANDLE) The handle is not a valid PDH object.

Even when I open the query with
Code:
pdhStatus := DllCall("pdh.dll\PdhOpenQuery", Int, 0, UInt, 0, UInt, &hQuery, UInt)

and try to close it with
Code:
pdhStatus := DllCall("pdh.dll\PdhCloseQuery", UInt, hQuery, UInt)


I get the 0xC0000BBC error code.

Any help is welcome.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2007, 11:03 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
I haven't much time to play with this, but to get an Int in an AutoHotkey variable, I think you must write instead:
Code:
pdhStatus := DllCall("pdh.dll\PdhOpenQuery", "Int", 0, "UInt", 0, "UInt *", hQuery, "UInt")
pdhStatus := DllCall("pdh.dll\PdhAddCounterA", "UInt", hQuery, "Str", "\Processor(_Total)\% Processor Time", "UInt", 0, "UInt *", hCounter, "UInt")
Untested.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2007, 5:05 pm 
Offline

Joined: April 20th, 2006, 9:54 pm
Posts: 21
Thanks, I tried it to no avail. I still get the same error.

I Also tried
Code:
pdhStatus := DllCall("pdh.dll\PdhOpenQuery", "Int", 0, "UInt", 0, "UIntP", hQuery, "UInt")
pdhStatus := DllCall("pdh.dll\PdhAddCounterA", "UInt", hQuery, "Str", "\Processor(_Total)\% Processor Time", "UInt", 0, "UIntP", hCounter, "UInt")


Thanks anyway


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 12:32 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
bold wrote:
Thanks, I tried it to no avail. I still get the same error.

Your DllCall() syntax seems correct. However, you forgot one thing in this case: put the following code at the start/end of the script.

Code:
hModule := DllCall("LoadLibrary", "str", "pdh.dll") ; Add this at the start of the script.

Code:
DllCall("FreeLibrary", "Uint", hModule) ; Add this at the end of the script.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 8:29 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Sean, AutoHotkey does that automatically. Such calls just avoid unloading the DLL after each DllCall.
I played a bit more, the first call is correct, the next one returns a PDH_INVALID_HANDLE error code.
Here is code I did, googling for examples on these functions, I probably won't go further.
Code:
#Include DllCallStruct.ahk

PDH_NO_DATA                      = 0x800007D5
PDH_MEMORY_ALLOCATION_FAILURE    = 0xC0000BBB
PDH_INVALID_HANDLE               = 0xC0000BBC
PDH_INVALID_ARGUMENT             = 0xC0000BBD

PDH_FMT_RAW                      = 0x00000010
PDH_FMT_ANSI                     = 0x00000020
PDH_FMT_UNICODE                  = 0x00000040
PDH_FMT_LONG                     = 0x00000100
PDH_FMT_DOUBLE                   = 0x00000200
PDH_FMT_LARGE                    = 0x00000400
PDH_FMT_NOSCALE                  = 0x00001000
PDH_FMT_1000                     = 0x00002000
PDH_FMT_NODATA                   = 0x00004000
PDH_FMT_NOCAP100                 = 0x00008000

SetFormat, integer, hex
hModule := DllCall("LoadLibrary", "Str", "pdh.dll")

hQuery = 0000
pdhStatus := DllCall("pdh.dll\PdhOpenQuery", "UInt", 0, "UInt", 0, "UInt *", hQuery, "UInt")
MsgBox %hQuery% - %pdhStatus% %ErrorLevel%
pdhStatus := DllCall("pdh.dll\PdhAddCounterA", "UInt", hQuery, "Str", "\Processor(_Total)\% Processor Time", "UInt", 0, "UInt *", hCounter, "UInt")
MsgBox %pdhStatus% %ErrorLevel%
pdhStatus := DllCall("pdh.dll\PdhCollectQueryData", "UInt", hQuery, "UInt")
MsgBox %pdhStatus% %ErrorLevel%
Sleep 1000
pdhStatus := DllCall("pdh.dll\PdhCollectQueryData", "UInt", hQuery, "UInt")
MsgBox %pdhStatus% %ErrorLevel%
VarSetCapacity(value, 12, 0)
pdhStatus := DllCall("pdh.dll\PdhGetFormattedCounterValue", "UInt", hCounter, "UInt", PDH_FMT_LONG, "UInt", 0, "UInt", &value, "UInt")
MsgBox %pdhStatus% %ErrorLevel%
pdhStatus := DllCall("pdh.dll\PdhCloseQuery", "UInt", hQuery, "UInt")
MsgBox %pdhStatus% %ErrorLevel%
MsgBox % DumpDWORDs(value, 12)
DllCall("FreeLibrary", "Uint", hModule)
[EDIT] Changed following Sean's advices.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Last edited by PhiLho on April 1st, 2007, 10:21 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 9:52 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
PhiLho wrote:
Sean, AutoHotkey does that automatically. Such calls just avoid unloading the DLL after each DllCall.

Yes, that was the point, to not unload pdh.dll after first/each call as pdh.dll is not loaded into AHK defaultly.
I think the situation was like this: After the first call, pdh.dll was unloaded, so, the successfully created hQuery in the first call also became invalid afterwards. Thus, the second call produced PDH_INVALID_HANDLE.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 3:04 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Ah, good idea. Indeed, the second call of my code takes much longer time on my computer, as if doing something. I have still an error I have no time to investigate, but this is interesting.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 6:04 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
PhiLho wrote:
Ah, good idea. Indeed, the second call of my code takes much longer time on my computer, as if doing something. I have still an error I have no time to investigate, but this is interesting.

I successfully ran your code after a minor modification ("UInt *", value -> "Uint", &value) with Load/FreeLibrary pre/appended in my system. So, I guess the appropriate performance counter is disabled in your system, although I don't know which one it is.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2007, 10:20 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Indeed, I first thought it was a value, then found it was a structure, but forgot to change the call. Anyway, I don't reach it, I have a PDH_CSTATUS_NO_OBJECT error, like you guessed.
I update my script anyway, but won't explore the possible counters/syntaxes.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2007, 2:27 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
PhiLho wrote:
Anyway, I don't reach it, I have a PDH_CSTATUS_NO_OBJECT error, like you guessed.

You may use the MS's tool exctrlst.exe to check and enable/disable the counters. I think I extracted it from this:
http://www.microsoft.com/downloads/deta ... laylang=en

Or, may directly edit the registry. I reckon this counter is part of PerfOS, so go to

Code:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfOS\Performance

and set to 0 or delete the value if it exists. May need reboot afterward.

Code:
Disable Performance Counters


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2007, 11:06 pm 
Offline

Joined: April 20th, 2006, 9:54 pm
Posts: 21
Thanks for all your help. It seems to be working.

Cheers!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: [VxE], engunneer, Klark92, NarutoMan, rbrtryn, Tegno and 60 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group