[python-win32] Ctypes and PdhMakeCounterPath

Tim Roberts timr at probo.com
Thu Sep 3 20:21:48 CEST 2009


Steve Bonam wrote:
>
> I'm trying to learn ctypes so I figured I'd reproduce something I did earlier with pywin32 but it seems I fail it.
> I'm sure I'm missing something here
>
> PdhMakeCounterPath Doc on msdn: http://msdn.microsoft.com/en-us/library/aa372649%28VS.85%29.aspx
>
> [code]
>
> from ctypes import *
> from ctypes.wintypes import *
> pdh = windll.pdh
> class PDH_COUNTER_PATH_ELEMENTS_A(Structure):
>     _fields_ = [("szMachineName",LPSTR),
>                 ("szObjectName",LPSTR),
>                 ("szInstanceName", LPSTR),
>                 ("szParentInstance",LPSTR),
>                 ("dwInstanceIndex", DWORD),
>                 ("szCounterName",LPSTR)]
>
> pCounterPathElements = PDH_COUNTER_PATH_ELEMENTS_A(LPSTR(None), LPSTR('Network Interface'), LPSTR("Realtek RTL8168C[P]_8111C[P] Family PCI-E GBE NIC") , LPSTR(None), DWORD(-1), LPSTR("Bytes Received/sec"))
> szFullPathBuffer = LPCSTR(0)
> pcchbufferSize = DWORD(0)
>
> dwFlags = DWORD(0)
>
> result = pdh.PdhMakeCounterPathA(pCounterPathElements,
>                                  pointer(szFullPathBuffer),
>                                  pointer(pcchbufferSize), dwFlags)
>   

I get an error when I run this:

C:\tmp>x.py
Traceback (most recent call last):
  File "C:\tmp\x.py", line 32, in <module>
    pointer(pcchbufferSize), dwFlags)
ValueError: Procedure probably called with too many arguments (20 bytes
in excess)

The reason is that Python is trying to pass the elements of the tuple as
individual parameters, resulting in too many arguments. You need
"pointer(pCounterPathElements)".

After that, pcchbufferSize is 89 for me.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list