So here's my rough attempt to Convert the C++ code to VB, but it's seems it's had something that missing listed below (Marked in Bold) :
1. I thought this is an importing a lib/dll statement in C++, so how to get the pdh.lib/dll or Referencing it on VB ?
#pragma comment(lib, "pdh.lib")
2. This one
"sizeof" always known of not has an equivalent in VB, so any hint for this one ?
ZeroMemory(&BrowseDlgData, sizeof(PDH_BROWSE_DLG_CONFIG));
3. what is wprintf in C++ ? it's a same like "Console.writeline(...), or debug.writeline(...) ?
wprintf(...)
Any help would be appreciated! :)
The Full Code:
Converted from
https://msdn.microsoft.com/en-us/library/windows/desktop/aa371886(v=vs.85).aspx[
^]
Imports System
Imports Microsoft.VisualBasic
Public Class PDHMonitor
'Declare
Private Const SAMPLE_INTERVAL_MS As ULong = 1000
Private Const BROWSE_DIALOG_CAPTION As String = "Select a counter to monitor."
'C++ TO VB ?
#pragma comment(lib, "pdh.lib")
Private Sub wmain()
Dim Status As New PDH_STATUS()
Dim Query As HQUERY = Nothing
Dim Counter As New HCOUNTER()
Dim DisplayValue As New PDH_FMT_COUNTERVALUE()
Dim CounterType As UInteger
Dim SampleTime As New SYSTEMTIME()
Dim BrowseDlgData As New PDH_BROWSE_DLG_CONFIG()
Dim CounterPathBuffer As New String(New Char(PDH_MAX_COUNTER_PATH - 1) {})
'Create a query.
Status = PdhOpenQuery(Nothing, Nothing, Query)
If Status IsNot ERROR_SUCCESS Then
wprintf(LvbLf & "PdhOpenQuery failed with status 0x%x.", Status)
GoTo Cleanup
End If
'Initialize the browser dialog window settings.
ZeroMemory(CounterPathBuffer, Len(CounterPathBuffer))
'C++ TO VB equivalent of 'sizeof' ?
ZeroMemory(BrowseDlgData, sizeof(PDH_BROWSE_DLG_CONFIG))
With BrowseDlgData
.bIncludeInstanceIndex = 0
.bSingleCounterPerAdd = 1
.bSingleCounterPerDialog = 1
.bLocalCountersOnly = 0
.bWildCardInstances = 1
.bHideDetailBox = 1
.bInitializePath = 0
.bDisableMachineSelection = 0
.bIncludeCostlyObjects = 0
.bShowObjectBrowser = 0
.hWndOwner = Nothing
.szReturnPathBuffer = CounterPathBuffer
.cchReturnPathLength = PDH_MAX_COUNTER_PATH
.pCallBack = Nothing
.dwCallBackArg = 0
.CallBackStatus = ERROR_SUCCESS
.dwDefaultDetailLevel = PERF_DETAIL_WIZARD
.szDialogBoxCaption = BROWSE_DIALOG_CAPTION
End With
' Display the counter browser window. The dialog is configured
' to return a single selection from the counter list.
Status = PdhBrowseCounters(BrowseDlgData)
If Status IsNot ERROR_SUCCESS Then
If Status Is PDH_DIALOG_CANCELLED Then
Console.Write(LvbLf & "Dialog canceled by user.")
Else
wprintf(LvbLf & "PdhBrowseCounters failed with status 0x%x.", Status)
End If
GoTo Cleanup
ElseIf CounterPathBuffer.Length = 0 Then
Console.Write(LvbLf & "User did not select any counter.")
GoTo Cleanup
Else
wprintf(LvbLf & "Counter selected: %s" & vbLf, CounterPathBuffer)
End If
'Add the selected counter to the query.
Status = PdhAddCounter(Query, CounterPathBuffer, 0, Counter)
If Status IsNot ERROR_SUCCESS Then
wprintf(LvbLf & "PdhAddCounter failed with status 0x%x.", Status)
GoTo Cleanup
End If
' Most counters require two sample values to display a formatted value.
' PDH stores the current sample value and the previously collected
' sample value. This call retrieves the first value that will be used
' by PdhGetFormattedCounterValue in the first iteration of the loop
' Note that this value is lost if the counter does not require two
' values to compute a displayable value.
Status = PdhCollectQueryData(Query)
If Status IsNot ERROR_SUCCESS Then
wprintf(LvbLf & "PdhCollectQueryData failed with 0x%x." & vbLf, Status)
GoTo Cleanup
End If
'Print counter values until a key is pressed.
Do While Not _kbhit()
Sleep(SAMPLE_INTERVAL_MS)
GetLocalTime(SampleTime)
Status = PdhCollectQueryData(Query)
If Status IsNot ERROR_SUCCESS Then
wprintf(LvbLf & "PdhCollectQueryData failed with status 0x%x.", Status)
End If
wprintf(LvbLf & """%2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d.%3.3d""", SampleTime.wMonth, SampleTime.wDay, SampleTime.wYear, SampleTime.wHour, SampleTime.wMinute, SampleTime.wSecond, SampleTime.wMilliseconds)
'Compute a displayable value for the counter.
Status = PdhGetFormattedCounterValue(Counter, PDH_FMT_DOUBLE, CounterType, DisplayValue)
If Status IsNot ERROR_SUCCESS Then
wprintf(LvbLf & "PdhGetFormattedCounterValue failed with status 0x%x.", Status)
GoTo Cleanup
End If
Console.Write(",""{0:g20}""", DisplayValue.doubleValue)
Loop
Cleanup:
' Close the query.
If Query IsNot Nothing Then
PdhCloseQuery(Query)
End If
End Sub
End Class