VB6 - Netstat code breaking under Windows Server 2003 SBS

Posted on 2006-05-14
Last Modified: 2008-02-01

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, 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--
Question by:aromberg
    LVL 17

    Accepted 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:~)
    LVL 4

    Author Comment

    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

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now