AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using dllcall & pdh.dll to query performance counters?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
bold



Joined: 20 Apr 2006
Posts: 15

PostPosted: Mon Mar 26, 2007 10:18 pm    Post subject: Using dllcall & pdh.dll to query performance counters? Reply with quote

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.
Back to top
View user's profile Send private message
bold



Joined: 20 Apr 2006
Posts: 15

PostPosted: Thu Mar 29, 2007 10:53 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Thu Mar 29, 2007 11:03 pm    Post subject: Reply with quote

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.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
bold



Joined: 20 Apr 2006
Posts: 15

PostPosted: Fri Mar 30, 2007 5:05 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1359

PostPosted: Sat Mar 31, 2007 12:32 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sat Mar 31, 2007 8:29 am    Post subject: Reply with quote

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.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Last edited by PhiLho on Sun Apr 01, 2007 10:21 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Sean



Joined: 12 Feb 2007
Posts: 1359

PostPosted: Sat Mar 31, 2007 9:52 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sat Mar 31, 2007 3:04 pm    Post subject: Reply with quote

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.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Sean



Joined: 12 Feb 2007
Posts: 1359

PostPosted: Sat Mar 31, 2007 6:04 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sun Apr 01, 2007 10:20 am    Post subject: Reply with quote

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.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Sean



Joined: 12 Feb 2007
Posts: 1359

PostPosted: Sun Apr 01, 2007 2:27 pm    Post subject: Reply with quote

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/details.aspx?familyid=6EC50B78-8BE1-4E81-B3BE-4E7AC4F0912D&displaylang=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
Back to top
View user's profile Send private message
bold



Joined: 20 Apr 2006
Posts: 15

PostPosted: Sun Apr 01, 2007 11:06 pm    Post subject: Reply with quote

Thanks for all your help. It seems to be working.

Cheers!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group