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

How to add PdhParseCounterPath function #52

Closed
jwenz723 opened this issue Jun 1, 2018 · 2 comments
Closed

How to add PdhParseCounterPath function #52

jwenz723 opened this issue Jun 1, 2018 · 2 comments

Comments

@jwenz723
Copy link

jwenz723 commented Jun 1, 2018

I am trying to add the PdhParseCounterPath function (as documented here https://msdn.microsoft.com/en-us/library/windows/desktop/aa372657(v=vs.85).aspx ) into pdh.go, but I am a bit unsure of how to do so. Wondering if any of the contributors could offer a little help? Here is what I've added:


type PDH_COUNTER_PATH_ELEMENTS struct {
	szMachineName string
	szObjectName string
	szInstanceName string
	szParentInstance string
	dwInstanceIndex uintptr
	szCounterName string
}

var (
	// Library
	libpdhDll *syscall.DLL

	// Functions
	pdh_AddCounterW               *syscall.Proc
	pdh_AddEnglishCounterW        *syscall.Proc
	pdh_CloseQuery                *syscall.Proc
	pdh_CollectQueryData          *syscall.Proc
	pdh_GetFormattedCounterValue  *syscall.Proc
	pdh_GetFormattedCounterArrayW *syscall.Proc
	pdh_OpenQuery                 *syscall.Proc
	pdh_ValidatePathW             *syscall.Proc
	pdh_ParseCounterPathW		  *syscall.Proc  // added this
)

func init() {
	// Library
	libpdhDll = syscall.MustLoadDLL("pdh.dll")

	// Functions
	pdh_AddCounterW = libpdhDll.MustFindProc("PdhAddCounterW")
	pdh_AddEnglishCounterW, _ = libpdhDll.FindProc("PdhAddEnglishCounterW") // XXX: only supported on versions > Vista.
	pdh_CloseQuery = libpdhDll.MustFindProc("PdhCloseQuery")
	pdh_CollectQueryData = libpdhDll.MustFindProc("PdhCollectQueryData")
	pdh_GetFormattedCounterValue = libpdhDll.MustFindProc("PdhGetFormattedCounterValue")
	pdh_GetFormattedCounterArrayW = libpdhDll.MustFindProc("PdhGetFormattedCounterArrayW")
	pdh_OpenQuery = libpdhDll.MustFindProc("PdhOpenQuery")
	pdh_ValidatePathW = libpdhDll.MustFindProc("PdhValidatePathW")
	pdh_ParseCounterPathW = libpdhDll.MustFindProc("PdhParseCounterPathW") // added this
}

func PdhParseCounterPath(szFullPathBuffer string, pCounterPathElements *PDH_COUNTER_PATH_ELEMENTS) uint32 {
	ptxt, _ := syscall.UTF16PtrFromString(szFullPathBuffer)

	var test uint32

	ret, _, _ := pdh_ParseCounterPathW.Call(
		uintptr(unsafe.Pointer(ptxt)), // I think this is correct
		uintptr(unsafe.Pointer(pCounterPathElements)), // not sure what to do here
		uintptr(unsafe.Pointer(&test)), // not sure what to do here
		0) // I think this is correct

	return uint32(ret)
}
@jwenz723
Copy link
Author

jwenz723 commented Jun 1, 2018

Also, when I run it in this current condition I get the error code: PDH_INVALID_ARGUMENT, so I assume something is incorrect with the arguments I am passing into the function call.

@jwenz723
Copy link
Author

jwenz723 commented Jun 4, 2018

Figured it out finally after many hours of trial and error...

func PdhParseCounterPath(szFullPathBuffer string, pCounterPathElements *PDH_COUNTER_PATH_ELEMENTS, pdwBufferSize *uint32) uint32 {
	ptxt, _ := syscall.UTF16PtrFromString(szFullPathBuffer)

	ret, _, _ := pdh_ParseCounterPathW.Call(
		uintptr(unsafe.Pointer(ptxt)),
		uintptr(unsafe.Pointer(pCounterPathElements)),
		uintptr(unsafe.Pointer(pdwBufferSize)),
		0)

	return uint32(ret)
}

@jwenz723 jwenz723 closed this as completed Jun 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant