WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Process ID

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Enumerating Process Objects


#DefineFunction UDF_ListObjects()
		If WinVersion(4) != 4
			Message("Error","This function is not designed to run on this platform")
			Return(0)
		Endif
		pdhStatus               = 0
		PERF_DETAIL_WIZARD      = 400 ; For the system designer
		PDH_MORE_DATA				= "-2147481646"
		ERROR_SUCCESS				= 0
		szObjectList          = ""
	
		dllhandle=DllLoad(strcat(dirwindows(1),"PDH.DLL"))
		
		;Determine the required buffer size for the data.
		;---- Begin PdhEnumObjectsA ------------------------------
		lpObjectList = BinaryAlloc(65535)
		BinaryEodSet(lpObjectList,0)
		lpObjectListSize = BinaryAlloc(4)
		BinaryEodSet(lpObjectListSize,0)
		
		pdhStatus = DllCall(dllhandle,long:"PdhEnumObjectsA",lpnull,lpnull,lpbinary:lpObjectList,lpbinary:lpObjectListSize,long:PERF_DETAIL_WIZARD,long:0)
		;---- End PdhEnumObjectsA ------------------------------
		
		if pdhStatus == PDH_MORE_DATA 
			;reallocate the buffers and try the call again.
			dwObjectListSize = BinaryPeek4(lpObjectListSize,0)
			BinaryFree(lpObjectList)
			lpObjectList = BinaryAlloc(dwObjectListSize)
	
			;---- Begin PdhEnumObjectsA ------------------------------
			pdhStatus = DllCall(dllhandle,long:"PdhEnumObjectsA",lpnull,lpnull,lpbinary:lpObjectList,lpbinary:lpObjectListSize,long:PERF_DETAIL_WIZARD,long:0)
			;---- End PdhEnumObjectsA ------------------------------

			BinaryEodset(lpObjectList,dwObjectListSize)
		endif

		if pdhStatus != ERROR_SUCCESS 
			Message("Error: Unable to Enumerate Objects.", pdhStatus)
			Gosub Cleanup
			Return(0)
		endif


		;Walk the return object list.
		
	   eod = BinaryEodGet(lpObjectList)
		ptr = 0
		While ptr < eod
         szObject = BinaryPeekStr(lpObjectList, ptr, 256)
			ptr = ptr + StrLen(szObject)+1
			szObjectList = StrCat(szObjectList,@tab,szObject)
		Endwhile

		GoSub Cleanup
		Return StrTrim(szObjectList)

		
		; Should not get this far
		GoSub Cleanup
		Return("*ERROR*")

		:Cleanup
		BinaryFree(lpObjectListSize)
		BinaryFree(lpObjectList)
		DllFree(dllhandle)
		return

#EndFunction



#DefineFunction UDF_ListObjectInstances(object)
		If WinVersion(4) != 4
			Message("Error","This function is not designed to run on this platform")
			Return(0)
		Endif
		pdhStatus               = 0
		PERF_DETAIL_WIZARD      = 400 ; For the system designer
		PDH_MORE_DATA				= "-2147481646"
		ERROR_SUCCESS				= 0
		szInstanceList          = ""
	
		dllhandle=DllLoad(strcat(dirwindows(1),"PDH.DLL"))
		
		;Determine the required buffer size for the data.
		;---- Begin PdhEnumObjectItemsA ------------------------------
		lpCounterList = BinaryAlloc(65535)
		BinaryEodSet(lpCounterList,0)
		lpCounterListSize = BinaryAlloc(4)
		BinaryEodSet(lpCounterListSize,0)
		lpInstanceList = BinaryAlloc(65535)
		BinaryEodSet(lpInstanceList,0)
		lpInstanceListSize = BinaryAlloc(4)
		BinaryEodSet(lpInstanceListSize,0)
		pdhStatus = DllCall(dllhandle,long:"PdhEnumObjectItemsA",lpnull,lpnull,lpstr:object,lpbinary:lpCounterList,lpbinary:lpCounterListSize,lpbinary:lpInstanceList,lpbinary:lpInstanceListSize,long:PERF_DETAIL_WIZARD,long:0)
		;---- End PdhEnumObjectItemsA ------------------------------
		
		if pdhStatus == PDH_MORE_DATA 
			;reallocate the buffers and try the call again.
			dwCounterListSize = BinaryPeek4(lpCounterListSize,0)
			BinaryFree(lpCounterList)
			lpCounterList = BinaryAlloc(dwCounterListSize)

			dwInstanceListSize = BinaryPeek4(lpInstanceListSize,0)
			BinaryFree(lpInstanceList)
			lpInstanceList = BinaryAlloc(dwInstanceListSize)

			;---- Begin PdhEnumObjectItemsA ------------------------------
			pdhStatus = DllCall(dllhandle,long:"PdhEnumObjectItemsA",lpnull,lpnull,lpstr:object,lpbinary:lpCounterList,lpbinary:lpCounterListSize,lpbinary:lpInstanceList,lpbinary:lpInstanceListSize,long:PERF_DETAIL_WIZARD,long:0)
			;---- End PdhEnumObjectItemsA ------------------------------

			BinaryEodset(lpCounterList,dwCounterListSize)
		Endif

		if pdhStatus != ERROR_SUCCESS
			Message("Error: Unable to List Object Instances", pdhStatus)
			Gosub Cleanup
			Return(0)
		endif

		BinaryEodset(lpCounterList,dwCounterListSize)
		BinaryEodset(lpInstanceList,dwInstanceListSize)
		
		;Walk the return instance list.
		eod = BinaryEodGet(lpInstanceList)
		ptr = 0
		While ptr < eod
			szInstance = BinaryPeekStr(lpInstanceList, ptr, 256)
			ptr = ptr + StrLen(szInstance)+1
			szInstanceList = StrCat(szInstanceList,@tab,szInstance)
		Endwhile
		
		GoSub Cleanup
		Return StrTrim(szInstanceList)

		
		; Should not get this far
		GoSub Cleanup
		Return("*ERROR*")

		:Cleanup
		BinaryFree(lpInstanceListSize)
		BinaryFree(lpInstanceList)
		BinaryFree(lpCounterListSize)
		BinaryFree(lpCounterList)
		DllFree(dllhandle)
		return
#EndFunction

#DefineFunction UDF_ListObjectCounters(object)
		If WinVersion(4) != 4
			Message("Error","This function is not designed to run on this platform")
			Return(0)
		Endif
		pdhStatus               = 0
		PERF_DETAIL_WIZARD      = 400 ; For the system designer
		PDH_MORE_DATA				= "-2147481646"
		ERROR_SUCCESS				= 0
		szCounterList          = ""
	
		dllhandle=DllLoad(strcat(dirwindows(1),"PDH.DLL"))
		
		;Determine the required buffer size for the data.
		;---- Begin PdhEnumObjectItemsA ------------------------------
		lpCounterList = BinaryAlloc(65535)
		BinaryEodSet(lpCounterList,0)
		lpCounterListSize = BinaryAlloc(4)
		BinaryEodSet(lpCounterListSize,0)
		lpInstanceList = BinaryAlloc(65535)
		BinaryEodSet(lpInstanceList,0)
		lpInstanceListSize = BinaryAlloc(4)
		BinaryEodSet(lpInstanceListSize,0)
		pdhStatus = DllCall(dllhandle,long:"PdhEnumObjectItemsA",lpnull,lpnull,lpstr:object,lpbinary:lpCounterList,lpbinary:lpCounterListSize,lpbinary:lpInstanceList,lpbinary:lpInstanceListSize,long:PERF_DETAIL_WIZARD,long:0)
		;---- End PdhEnumObjectItemsA ------------------------------
		
		if pdhStatus == PDH_MORE_DATA 
			;reallocate the buffers and try the call again.
			dwCounterListSize = BinaryPeek4(lpCounterListSize,0)
			BinaryFree(lpCounterList)
			lpCounterList = BinaryAlloc(dwCounterListSize)

			dwInstanceListSize = BinaryPeek4(lpInstanceListSize,0)
			BinaryFree(lpInstanceList)
			lpInstanceList = BinaryAlloc(dwInstanceListSize)

			;---- Begin PdhEnumObjectItemsA ------------------------------
			pdhStatus = DllCall(dllhandle,long:"PdhEnumObjectItemsA",lpnull,lpnull,lpstr:object,lpbinary:lpCounterList,lpbinary:lpCounterListSize,lpbinary:lpInstanceList,lpbinary:lpInstanceListSize,long:PERF_DETAIL_WIZARD,long:0)
			;---- End PdhEnumObjectItemsA ------------------------------

			BinaryEodset(lpCounterList,dwCounterListSize)
			BinaryEodset(lpInstanceList,dwInstanceListSize)
		endif

		if pdhStatus != ERROR_SUCCESS 
			Message("Error: Unable to List Object Counters.", pdhStatus)
			Gosub Cleanup
			Return(0)
		endif

		;Walk the return counters list.
 		eod = BinaryEodGet(lpcounterList)
		ptr = 0
		While ptr < eod
		   szCounter = BinaryPeekStr(lpcounterList, ptr, 1000)
			ptr = ptr + StrLen(szCounter)+1
			szCounterList = StrCat(szCounterList,@tab,szCounter)
		Endwhile
		
		GoSub Cleanup
		Return StrTrim(szCounterList)
				
		
		; Should not get this far
		GoSub Cleanup
		Return("*ERROR*")

		:Cleanup
		BinaryFree(lpInstanceListSize)
		BinaryFree(lpInstanceList)
		BinaryFree(lpCounterListSize)
		BinaryFree(lpCounterList)
		DllFree(dllhandle)
		return
#EndFunction

ret = UDF_ListObjects()
object = AskItemList("Object list - Choose one", ret, @tab,@sorted, @single)

ret = UDF_ListObjectInstances(object)
AskItemList(StrCat(object," Instance list"), ret, @tab,@sorted, @single)

ret = UDF_ListObjectCounters(object)
counter = AskItemList(StrCat(object," Counter list"), ret, @tab,@sorted, @single)
;handle for percent signs
counter = StrReplace(counter, "%%", "%%%%")
Message("Counter Chosen",counter)
exit

Article ID:   W16356
File Created: 2017:07:28:12:58:38
Last Updated: 2005:02:18:11:19:59