Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PDH counter wildcard and localization support #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions pdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -169,6 +195,8 @@ 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
pdh_OpenQuery *windows.LazyProc
Expand All @@ -184,6 +212,8 @@ 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")
pdh_OpenQuery = libpdhDll.NewProc("PdhOpenQuery")
Expand Down Expand Up @@ -291,6 +321,31 @@ 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 {
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 {
Expand Down