• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

VB6 - Netstat code breaking under Windows Server 2003 SBS


Here's my problem.  I'm writing a program in VB6 that does the following:  does a netstat output, ignores all listen sockets, and counts the amount of established sockets on port 27046, and dumps that value in a .csv.  Problem is, none of the code examples I find online work on SBS2003.  They work great on XP, but once I throw them on SBS, I get a vary of error messages.  The one that pertains to this section of code is '6 - Overflow'  The code is taken from http://www.ostrosoft.com/vb/projects/iphlpapi.asp, and modified accordingly.  Even the authors code breaks... which is making me pull hairs!

Thanks for any help!

Private Sub DoNetstat()
  Open fileName For Append As #1
  Dim pTcpTable As MIB_TCPTABLE
  Dim pdwSize As Long
  Dim bOrder As Long
  Dim nRet As Long
  Dim i As Integer, s As String
frmdebug.Text1 = frmdebug.Text1 & "DoNetStat called (break here)" & vbCrLf
  license = 0
  nRet = GetTcpTable(pTcpTable, pdwSize, bOrder)
  nRet = GetTcpTable(pTcpTable, pdwSize, bOrder)
  For i = 0 To pTcpTable.dwNumEntries - 1
  frmdebug.Text1 = frmdebug.Text1 & "DoNetStat: in for i=" & i & vbCrLf
    If pTcpTable.table(i).dwState - 1 <> MIB_TCP_STATE_LISTEN Then

    frmdebug.Text1 = frmdebug.Text1 & c_ip(pTcpTable.table(i).dwLocalAddr) & ":" & _
        c_port(pTcpTable.table(i).dwLocalPort) & vbTab & _
        c_ip(pTcpTable.table(i).dwRemoteAddr) & ":" & _
        c_port(pTcpTable.table(i).dwRemotePort) & vbTab & _
        c_state(pTcpTable.table(i).dwState - 1) & vbCrLf & "*************" & vbCrLf

    frmdebug.Text1 = frmdebug.Text1 & "DoNetStat: after if, right before bot=" & vbCrLf
        bot = c_port(pTcpTable.table(i).dwLocalPort)
        frmdebug.Text1 = frmdebug.Text1 & "DoNetStat: bot " & bot & vbCrLf
                If (bot = 27046) Then
                    license = license + 1
                End If
    End If
  frmdebug.Text1 = frmdebug.Text1 & "DoNetStat finished.. writing file" & vbCrLf
  Label5.Caption = license
  Write #1, DateTime.Date$, DateTime.Time$, license
  Close #1
  frmdebug.Text1 = frmdebug.Text1 & "File written and closed\n" & vbCrLf
End Sub
---end snip--
1 Solution
The problem is in the c_Port function

Function c_port(s) As Long
    c_port = CLng(Asc(Mid(s, 1, 1))) * clng(256) + CLng(Asc(Mid(s, 2, 1)))
End Function

Also you need to make sure output is a multiline box.

Hope this helkps:~)
arombergAuthor Commented:
Thanks!  That's what I needed for the most part... the next problem, which I'll detail here... was that the index array was too small... which caused a "9 - Subscript out of bounds error", so I had to go into my module and change the line that said 'table(100) As MIB_TCPROW   'array of TCP connections' to 'table(250) As MIB_TCPROW   'array of TCP connections'.

Thanks a bunch!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now