Results 1 to 18 of 18

Thread: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2013
    Posts
    43

    GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Does anyone have an example of how to call these? I have managed to get GetTcpTable working but have not been able to find any source examples of the stats functions in vb6.

    I am trying to get IP traffic stats per connection for a monitoring project.

    cheers

  2. #2
    Addicted Member
    Join Date
    Feb 2014
    Location
    USA
    Posts
    128

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    I was somewhat interested in those APIs too and tried the obligatory "declare function GetPerTcpConnectionEStats" (with quotes) Google search with no luck. I can code some APIs, but it's admittedly a weakness of mine. In these cases, I'll sometimes go to freelancer.com if it's something I really need.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    That API doesn't look especially difficult, and as it is MSDN already gives a code sample. Maybe if you try to port that to VB people could help answer individual specific questions.

    But I doubt anybody is going to write the code for you. It just isn't of very broad interest.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2013
    Posts
    43

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Thanks for the replies. I did see the msdn example. Unfortunately my c++ is not strong and I am unable to figure out how to declare the functions in vb. If someone could help with that I can probably figure out the rest.

    @crazyDude. Thanks for the offer will consider it.

  5. #5
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Code:
    Public Enum TCP_ESTATS_TYPE
        TcpConnectionEstatsSynOpts
        TcpConnectionEstatsData
        TcpConnectionEstatsSndCong
        TcpConnectionEstatsPath
        TcpConnectionEstatsSendBuff
        TcpConnectionEstatsRec
        TcpConnectionEstatsObsRec
        TcpConnectionEstatsBandwidth
        TcpConnectionEstatsFineRtt
        TcpConnectionEstatsMaximum
    End Enum
    Private Type MIB_TCPROW
        dwState As MIB_TCP_STATE
        dwLocalAddr As Long
        dwLocalPort As Long
        dwRemoteAddr As Long
        dwRemotePort As Long
    End Type
    
    Private Enum MIB_TCP_STATE
        MIB_TCP_STATE_CLOSED = 1
        MIB_TCP_STATE_LISTEN = 2
        MIB_TCP_STATE_SYN_SENT = 3
        MIB_TCP_STATE_SYN_RCVD = 4
        MIB_TCP_STATE_ESTAB = 5
        MIB_TCP_STATE_FIN_WAIT1 = 6
        MIB_TCP_STATE_FIN_WAIT2 = 7
        MIB_TCP_STATE_CLOSE_WAIT = 8
        MIB_TCP_STATE_CLOSING = 9
        MIB_TCP_STATE_LAST_ACK = 10
        MIB_TCP_STATE_TIME_WAIT = 11
        MIB_TCP_STATE_DELETE_TCB = 12
    End Enum
    
    Private Type IN6_ADDR
        'Byte_(0 To 16 - 1) As Byte
        Word(0 To 8 - 1) As Integer
    End Type
    
    Private Type MIB_TCP6ROW
        State As MIB_TCP_STATE
        LocalAddr As IN6_ADDR
        dwLocalScopeId As Long
        dwLocalPort As Long
        RemoteAddr As IN6_ADDR
        dwRemoteScopeId As Long
        dwRemotePort As Long
    End Type
    
    Public Declare Function GetPerTcpConnectionEStats Lib "iphlpapi" ( _
        ByRef Row As Any, _
        ByVal EstatsType As TCP_ESTATS_TYPE, _
        ByRef Rw As Byte, _
        ByVal RwVersion As Long, _
        ByVal RwSize As Long, _
        ByRef Ros As Byte, _
        ByVal RosVersion As Long, _
        ByVal RosSize As Long, _
        ByRef Rod As Byte, _
        ByVal RodVersion As Long, _
        ByVal RodSize As Long _
        ) As Long
    
    Public Declare Function SetPerTcpConnectionEStats Lib "iphlpapi" ( _
        ByRef Row As Any, _
        ByVal EstatsType As TCP_ESTATS_TYPE, _
        ByRef Rw As Byte, _
        ByVal RwVersion As Long, _
        ByVal RwSize As Long, _
        ByVal Offset As Long _
        ) As Long
    or the ByRef As Byte can be ByRef as Any for more flexibility.

    Code:
    Public Declare Function GetPerTcpConnectionEStats Lib "iphlpapi" ( _
        ByRef Row As Any, _
        ByVal EstatsType As TCP_ESTATS_TYPE, _
        ByRef Rw As Any, _
        ByVal RwVersion As Long, _
        ByVal RwSize As Long, _
        ByRef Ros As Any, _
        ByVal RosVersion As Long, _
        ByVal RosSize As Long, _
        ByRef Rod As Any, _
        ByVal RodVersion As Long, _
        ByVal RodSize As Long _
        ) As Long
    
    Public Declare Function SetPerTcpConnectionEStats Lib "iphlpapi" ( _
        ByRef Row As Any, _
        ByVal EstatsType As TCP_ESTATS_TYPE, _
        ByRef Rw As Any, _
        ByVal RwVersion As Long, _
        ByVal RwSize As Long, _
        ByVal Offset As Long _
        ) As Long
    Last edited by DEXWERX; Apr 19th, 2018 at 12:33 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2013
    Posts
    43

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Much appreciated thank you.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2013
    Posts
    43

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Alas not so simple. SetPerTcpConnectionEStats always returns 87 ERROR_INVALID_PARAMETER even though I can see the source, destination IP & ports in the MIB_TCPROW structure returned from GetTcpTable.

    msdn says

    "The parameter is incorrect. This error is returned if the Row parameter is a NULL pointer"

    It is clearly not null so I must be passing it the wrong way. It returns invalid param if I pass the row by ref or pass VarPtr(row) by value.

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2013
    Posts
    43

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Don't know what happened to my last post. SetPerTcpConnectionEStats always returns error 87 invalid param although I can see the source & dest IP and ports in the MIB_TCPROW structure returned from GetTcpTable.

    MSDN says that is because the pointer to the row is null. So I am passing it incorrectly or something.

  9. #9
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    You may need admin rights for SetPerTcpConnectionEStats. (I did)

    Also all except TcpConnectionEstatsBandwidth, require a single Byte for the Rw parameter.

    (A WinAPI BOOLEAN = a VB6 Byte)

    That's probably what you got wrong, as an invalid parameter.
    Either that or you're passing an invalid Row. Needs to be either an MIB_TCP6ROW or an MIB_TCPROW.

    but without your code, it's only a guess.

    Here's toggleEstat() translated from MSDN
    Code:
    '
    ' Enable or disable the supplied EStat type on a TCP connection.
    '
    Sub ToggleEStat(ByVal Row As Long, _
                    ByVal EStatsType As TCP_ESTATS_TYPE, _
                    ByVal Enable As Boolean, _
                    ByVal V6 As Boolean _
                    )
        Dim Status As Long
        Dim Size As Long
        Dim Rw As Long
        Dim bEnable As Byte ' BOOLEAN
        Dim bandwidthRw As TCP_ESTATS_BANDWIDTH_RW_v0
        
        If Enable Then bEnable = TRUE_ ' TRUE = 1 = TcpBoolOptEnabled
        
        Select Case EStatsType
            Case TcpConnectionEStatsBandwidth
                bandwidthRw.EnableCollectionInbound = bEnable
                bandwidthRw.EnableCollectionOutbound = bEnable
                Rw = VarPtr(bandwidthRw)
                Size = LenB(bandwidthRw)
            Case TcpConnectionEStatsData To TcpConnectionEStatsBandwidth
                Rw = VarPtr(bEnable)
                Size = LenB(bEnable)
            Case Else
                Exit Sub
        End Select
    
        If V6 Then
            Status = SetPerTcp6ConnectionEStats(ByVal Row, EStatsType, _
                                                ByVal Rw, 0, Size, 0)
        Else
            Status = SetPerTcpConnectionEStats(ByVal Row, EStatsType, _
                                               ByVal Rw, 0, Size, 0)
        End If
    
        If Status <> NO_ERROR Then
            wprintf "SetPerTcp{3}ConnectionEStats {0} {1} failed. status = {2} - {4}", _
                    EStatsTypeNames(EStatsType), IIf(Enable, "enabled", "disabled"), _
                    Status, IIf(V6, "6", ""), IPHLPError(Status)
        End If
    End Sub
    Last edited by DEXWERX; Apr 25th, 2018 at 01:23 PM. Reason: Updated to remove typelib specific functions

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2013
    Posts
    43

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Thanks Dexwerx. I did have admin rights but probably was not passing a single byte. Will give your kind suggestion a go.

  11. #11
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    1 more gotcha. There are a few Types that needed explicit Padding to keep in line with VC++

    Code:
    Public Type TCP_ESTATS_SYN_OPTS_ROS_v0
        ActiveOpen          As Byte ' BOOLEAN
        Padding(3 - 1)      As Byte
        MssRcvd             As Long
        MssSent             As Long
    End Type
    
    Public Type TCP_ESTATS_DATA_ROD_v0
        DataBytesOut        As Currency
        DataSegsOut         As Currency
        DataBytesIn         As Currency
        DataSegsIn          As Currency
        SegsOut             As Currency
        SegsIn              As Currency
        SoftErrors          As Long
        SoftErrorReason     As Long
        SndUna              As Long
        SndNxt              As Long
        SndMax              As Long
        Padding1            As Long
        ThruBytesAcked      As Currency
        RcvNxt              As Long
        Padding2            As Long
        ThruBytesReceived   As Currency
    End Type
    
    Public Type TCP_ESTATS_REC_ROD_v0
        CurRwinSent         As Long
        MaxRwinSent         As Long
        MinRwinSent         As Long
        LimRwin             As Long
        DupAckEpisodes      As Long
        DupAcksOut          As Long
        CeRcvd              As Long
        EcnSent             As Long
        EcnNoncesRcvd       As Long
        CurReasmQueue       As Long
        MaxReasmQueue       As Long
        CurAppRQueue        As Long
        MaxAppRQueue        As Long
        WinScaleSent        As Byte ' UCHAR
        Padding(3 - 1)      As Byte
    End Type
    
    Public Type TCP_ESTATS_OBS_REC_ROD_v0
        CurRwinRcvd         As Long
        MaxRwinRcvd         As Long
        MinRwinRcvd         As Long
        WinScaleRcvd        As Byte ' UCHAR
        Padding(3 - 1)      As Byte
    End Type
    Public Type TCP_ESTATS_BANDWIDTH_ROD_v0
        OutboundBandwidth   As Currency
        InboundBandwidth    As Currency
        OutboundInstability As Currency
        InboundInstability  As Currency
        OutboundBandwidthPeaked As Byte ' BOOLEAN
        InboundBandwidthPeaked As Byte  ' BOOLEAN
        Padding(6 - 1)      As Byte
    End Type

  12. #12
    Addicted Member
    Join Date
    Feb 2014
    Location
    USA
    Posts
    128

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Super nice work Dex. I'll be able to use this as well!

  13. #13
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Here's a handy function I use to display unsigned data...

    Code:
    Public Function U(ByVal Var As Variant) As String
    ' Reinterpret unsigned data for display
        Const VT_UI2 As Integer = 18
        Const VT_UI4 As Integer = 19
        Const VT_UI8 As Integer = 21
        Select Case VarType(Var)
            Case vbByte:        ' already unsigned
            Case vbInteger:     GetMem2 VT_UI2, Var
            Case vbLong:        GetMem2 VT_UI4, Var
            Case vbCurrency:    GetMem2 VT_UI8, Var
            Case Else:          Err.Raise 5
        End Select
        U = Format$(Var, "0")   ' show all digits including 0
    End Function

  14. #14
    Addicted Member
    Join Date
    Feb 2014
    Location
    USA
    Posts
    128

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Good deal. Now I just need to put all the latest code you posted together and give it a try!

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2013
    Posts
    43

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Would this be the correct type def for TCP_ESTATS_PATH_ROD_V0

    Public Type TCP_ESTATS_PATH_ROD_v0
    FastRetran As Long
    Timeouts As Long
    SubsequentTimeouts As Long
    CurTimeoutCount As Long
    AbruptTimeouts As Long
    PktsRetrans As Long
    BytesRetrans As Long
    DupAcksIn As Long
    SacksRcvd As Long
    SackBlocksRcvd As Long
    CongSignals As Long
    PreCongSumCwnd As Long
    PreCongSumRtt As Long
    PostCongSumRtt As Long
    PostCongCountRtt As Long
    EcnSignals As Long
    EceRcvd As Long
    SendStall As Long
    QuenchRcvd As Long
    RetranThresh As Long
    SndDupAckEpisodes As Long
    SumBytesReordered As Long
    NonRecovDa As Long
    NonRecovDaEpisodes As Long
    AckAfterFr As Long
    DsackDups As Long
    SampleRtt As Long
    SmoothedRtt As Long
    RttVar As Long
    MaxRtt As Long
    MinRtt As Long
    SumRtt As Long
    CountRtt As Long
    CurRto As Long
    MaxRto As Long
    MinRto As Long
    CurMss As Long
    MaxMss As Long
    MinMss As Long
    SpuriousRtoDetections As Long
    End Type

  16. #16

    Thread Starter
    Member
    Join Date
    Jun 2013
    Posts
    43

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    Quote Originally Posted by CrazyDude View Post
    Good deal. Now I just need to put all the latest code you posted together and give it a try!
    When and if you do I would appreciate it if you could post the code. Thanks all for their help.

  17. #17
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats

    your TCP_ESTATS_PATH_ROD_v0 is correct.
    I'll just post the entire conversion of the MSDN example to the codebank.

  18. #18
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: GetPerTcpConnectionEStats & SetPerTcpConnectionEStats


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width