From afc98d2dae53e18302267e1986b889bfa5e0bf1f Mon Sep 17 00:00:00 2001 From: Brantley West Date: Thu, 12 Jan 2023 19:24:23 -0500 Subject: [PATCH 1/2] pdh.go: add PdhGetCounterInfo(), PDH_COUNTER_INFO, PDH_COUNTER_PATH_ELEMENTS --- pdh.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pdh.go b/pdh.go index a18b2608..a2cb3758 100644 --- a/pdh.go +++ b/pdh.go @@ -123,6 +123,32 @@ type ( PDH_HCOUNTER HANDLE // counter handle ) +// For struct details, see https://learn.microsoft.com/en-us/windows/win32/api/pdh/ns-pdh-pdh_counter_info_w. +type PDH_COUNTER_INFO struct { + dwLength uint32 + dwType uint32 + CVersion uint32 + CStatus uint32 + lScale int32 + lDefaultScale int32 + dwUserData *uint32 + dwQueryUserData *uint32 + szFullPath *uint16 // pointer to a string + CounterPath *PDH_COUNTER_PATH_ELEMENTS + szExplainText *uint16 // pointer to a string + DataBuffer *string +} + +// For struct details, see https://learn.microsoft.com/en-us/windows/win32/api/pdh/ns-pdh-pdh_counter_path_elements_w. +type PDH_COUNTER_PATH_ELEMENTS struct { + szMachineName *uint16 // pointer to a string + szObjectName *uint16 // pointer to a string + szInstanceName *uint16 // pointer to a string + szParentInstance *uint16 // pointer to a string + dwInstanceIndex *uint32 + szCounterName *uint16 // pointer to a string +} + // Union specialization for double values type PDH_FMT_COUNTERVALUE_DOUBLE struct { CStatus uint32 @@ -169,6 +195,7 @@ var ( pdh_AddEnglishCounterW *windows.LazyProc pdh_CloseQuery *windows.LazyProc pdh_CollectQueryData *windows.LazyProc + pdh_GetCounterInfo *windows.LazyProc pdh_GetFormattedCounterValue *windows.LazyProc pdh_GetFormattedCounterArrayW *windows.LazyProc pdh_OpenQuery *windows.LazyProc @@ -184,6 +211,7 @@ func init() { pdh_AddEnglishCounterW = libpdhDll.NewProc("PdhAddEnglishCounterW") pdh_CloseQuery = libpdhDll.NewProc("PdhCloseQuery") pdh_CollectQueryData = libpdhDll.NewProc("PdhCollectQueryData") + pdh_GetCounterInfo = libpdhDll.NewProc("PdhGetCounterInfoW") pdh_GetFormattedCounterValue = libpdhDll.NewProc("PdhGetFormattedCounterValue") pdh_GetFormattedCounterArrayW = libpdhDll.NewProc("PdhGetFormattedCounterArrayW") pdh_OpenQuery = libpdhDll.NewProc("PdhOpenQuery") @@ -291,6 +319,18 @@ func PdhCollectQueryData(hQuery PDH_HQUERY) uint32 { return uint32(ret) } +// Retrieves information about a counter, such as data size, counter type, path, and user-supplied data values. +// For more information, see https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetcounterinfow. +func PdhGetCounterInfo(hCounter PDH_HCOUNTER, bRetrieveExplainText uintptr, pdwBufferSize *uint32, lpBuffer *PDH_COUNTER_INFO) uint32 { + ret, _, _ := pdh_GetCounterInfo.Call( + uintptr(hCounter), + bRetrieveExplainText, + uintptr(unsafe.Pointer(pdwBufferSize)), + uintptr(unsafe.Pointer(lpBuffer))) + + return uint32(ret) +} + // Formats the given hCounter using a 'double'. The result is set into the specialized union struct pValue. // This function does not directly translate to a Windows counterpart due to union specialization tricks. func PdhGetFormattedCounterValueDouble(hCounter PDH_HCOUNTER, lpdwType *uint32, pValue *PDH_FMT_COUNTERVALUE_DOUBLE) uint32 { From f0403522c63b372a274be8e8f7e2e31296253eca Mon Sep 17 00:00:00 2001 From: Brantley West Date: Tue, 17 Jan 2023 09:52:15 -0500 Subject: [PATCH 2/2] pdh.go: add PdhExpandWildCardPath() --- pdh.go | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/pdh.go b/pdh.go index a2cb3758..dec07e84 100644 --- a/pdh.go +++ b/pdh.go @@ -125,28 +125,28 @@ type ( // For struct details, see https://learn.microsoft.com/en-us/windows/win32/api/pdh/ns-pdh-pdh_counter_info_w. type PDH_COUNTER_INFO struct { - dwLength uint32 - dwType uint32 + DwLength uint32 + DwType uint32 CVersion uint32 CStatus uint32 - lScale int32 - lDefaultScale int32 - dwUserData *uint32 - dwQueryUserData *uint32 - szFullPath *uint16 // pointer to a string - CounterPath *PDH_COUNTER_PATH_ELEMENTS - szExplainText *uint16 // pointer to a string + LScale int32 + LDefaultScale int32 + DwUserData *uint32 + DwQueryUserData *uint32 + SzFullPath *uint16 // pointer to a string + CounterPath PDH_COUNTER_PATH_ELEMENTS + SzExplainText *uint16 // pointer to a string DataBuffer *string } // For struct details, see https://learn.microsoft.com/en-us/windows/win32/api/pdh/ns-pdh-pdh_counter_path_elements_w. type PDH_COUNTER_PATH_ELEMENTS struct { - szMachineName *uint16 // pointer to a string - szObjectName *uint16 // pointer to a string - szInstanceName *uint16 // pointer to a string - szParentInstance *uint16 // pointer to a string - dwInstanceIndex *uint32 - szCounterName *uint16 // pointer to a string + SzMachineName *uint16 // pointer to a string + SzObjectName *uint16 // pointer to a string + SzInstanceName *uint16 // pointer to a string + SzParentInstance *uint16 // pointer to a string + DwInstanceIndex *uint32 + SzCounterName *uint16 // pointer to a string } // Union specialization for double values @@ -195,6 +195,7 @@ var ( pdh_AddEnglishCounterW *windows.LazyProc pdh_CloseQuery *windows.LazyProc pdh_CollectQueryData *windows.LazyProc + pdh_ExpandWildCardPath *windows.LazyProc pdh_GetCounterInfo *windows.LazyProc pdh_GetFormattedCounterValue *windows.LazyProc pdh_GetFormattedCounterArrayW *windows.LazyProc @@ -211,6 +212,7 @@ func init() { pdh_AddEnglishCounterW = libpdhDll.NewProc("PdhAddEnglishCounterW") pdh_CloseQuery = libpdhDll.NewProc("PdhCloseQuery") pdh_CollectQueryData = libpdhDll.NewProc("PdhCollectQueryData") + pdh_ExpandWildCardPath = libpdhDll.NewProc("PdhExpandWildCardPathW") pdh_GetCounterInfo = libpdhDll.NewProc("PdhGetCounterInfoW") pdh_GetFormattedCounterValue = libpdhDll.NewProc("PdhGetFormattedCounterValue") pdh_GetFormattedCounterArrayW = libpdhDll.NewProc("PdhGetFormattedCounterArrayW") @@ -319,6 +321,19 @@ func PdhCollectQueryData(hQuery PDH_HQUERY) uint32 { return uint32(ret) } +// Examines the specified computer or log file and returns those counter paths that match the given counter path which contains wildcard characters. +// For more information, see https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhexpandwildcardpathw. +func PdhExpandWildCardPath(szDataSource *uint16, szWildCardPath *uint16, mszExpandedPathList *uint16, pcchPathListLength *uint32, dwFlags *uint32) uint32 { + ret, _, _ := pdh_ExpandWildCardPath.Call( + uintptr(unsafe.Pointer(szDataSource)), + uintptr(unsafe.Pointer(szWildCardPath)), + uintptr(unsafe.Pointer(mszExpandedPathList)), + uintptr(unsafe.Pointer(pcchPathListLength)), + uintptr(unsafe.Pointer(dwFlags))) + + return uint32(ret) +} + // Retrieves information about a counter, such as data size, counter type, path, and user-supplied data values. // For more information, see https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetcounterinfow. func PdhGetCounterInfo(hCounter PDH_HCOUNTER, bRetrieveExplainText uintptr, pdwBufferSize *uint32, lpBuffer *PDH_COUNTER_INFO) uint32 {