Hi, i have code the Table TCP, but need the PID the connection.
Any can help me in Sample code the : AllocateAndGetTcpExTableFromStack
This is code the TCPTable :
http://vbnet.mvps.org/index.html?code/network/gettcpconnectiontable.htm
I need Get vale the dwProcessId
Thank you.
Imports System.Runtime.InteropServices
#Region " Modulo : IP - Port "
Public Event ThreadComplete(ByVal zenc As Boolean, ByVal ziplocal As String, ByVal zlocalport As Long, ByVal zipremote As String)
Public Event ThreadCompleteX(ByVal zenc As Boolean)
Dim ziplocal As String
Dim zipremote As String
Dim zremoteport As Long
Dim zlocalport As Long
Dim zstatus As String
Dim zpidtcp As Integer
<StructLayout(LayoutKind.Sequential)> Public Class MIB_TCPROW
Public dwState As Integer
Public dwLocalAddr As Integer
Public dwLocalPort As Integer
Public dwRemoteAddr As Integer
Public dwRemotePort As Integer
Public dwProcessId As Integer
End Class
Private Const ERROR_BUFFER_OVERFLOW As Short = 111
Private Const ERROR_INVALID_PARAMETER As Short = 87
Private Const ERROR_NO_DATA As Short = 232
Private Const ERROR_NOT_SUPPORTED As Short = 50
Private Const ERROR_SUCCESS As Short = 0
'
Private Const MIB_TCP_STATE_CLOSED As Short = 1
Private Const MIB_TCP_STATE_LISTEN As Short = 2
Private Const MIB_TCP_STATE_SYN_SENT As Short = 3
Private Const MIB_TCP_STATE_SYN_RCVD As Short = 4
Private Const MIB_TCP_STATE_ESTAB As Short = 5
Private Const MIB_TCP_STATE_FIN_WAIT1 As Short = 6
Private Const MIB_TCP_STATE_FIN_WAIT2 As Short = 7
Private Const MIB_TCP_STATE_CLOSE_WAIT As Short = 8
Private Const MIB_TCP_STATE_CLOSING As Short = 9
Private Const MIB_TCP_STATE_LAST_ACK As Short = 10
Private Const MIB_TCP_STATE_TIME_WAIT As Short = 11
Private Const MIB_TCP_STATE_DELETE_TCB As Short = 12
'
<StructLayout(LayoutKind.Sequential)> Public Class MIB_TCPTABLE
Public dwNumEntries As Integer
Public table(1600) As MIB_TCPROW
End Class
Declare Function GetTcpTable Lib "Iphlpapi" (ByVal pTcpTable As IntPtr, ByRef pdwSize As Integer, ByVal bOrder As Boolean) As Integer
Declare Function SetTcpEntry Lib "iphlpapi.dll" (ByVal pTcpTable As MIB_TCPROW) As Integer
Private arrTcpTableRows() As MIB_TCPROW
Public Declare Function AllocateAndGetTcpExTableFromStack Lib "iphlpapi.dll" (ByVal pTcpTableEx As IntPtr, ByVal bOrder As Long, ByVal heap As Long, ByVal zero As Long, ByVal Flags As Long) As Long
Private Function GetIpFromLong(ByRef lngIPAddress As Integer) As String
'
Dim arrIpParts() As Byte = BitConverter.GetBytes(lngIPAddress)
GetIpFromLong = CStr(arrIpParts(0)) & "." & CStr(arrIpParts(1)) & "." & CStr(arrIpParts(2)) & "." & CStr(arrIpParts(3))
'
End Function
Private Function GetState(ByRef lngState As Integer) As String
'
Select Case lngState
Case MIB_TCP_STATE_CLOSED : GetState = "CLOSED"
Case MIB_TCP_STATE_LISTEN : GetState = "LISTEN"
Case MIB_TCP_STATE_SYN_SENT : GetState = "SYN_SENT"
Case MIB_TCP_STATE_SYN_RCVD : GetState = "SYN_RCVD"
Case MIB_TCP_STATE_ESTAB : GetState = "ESTAB"
Case MIB_TCP_STATE_FIN_WAIT1 : GetState = "FIN_WAIT1"
Case MIB_TCP_STATE_FIN_WAIT2 : GetState = "FIN_WAIT2"
Case MIB_TCP_STATE_CLOSE_WAIT : GetState = "CLOSE_WAIT"
Case MIB_TCP_STATE_CLOSING : GetState = "CLOSING"
Case MIB_TCP_STATE_LAST_ACK : GetState = "LAST_ACK"
Case MIB_TCP_STATE_TIME_WAIT : GetState = "TIME_WAIT"
Case MIB_TCP_STATE_DELETE_TCB : GetState = "DELETE_TCB"
End Select
'
End Function
Private Function GetTcpPortNumber(ByRef DWord As Integer) As Integer
GetTcpPortNumber = CInt(DWord / 256 + (DWord Mod 256) * 256)
End Function
Using List TCP Sockets : GetTCPTable
Public Sub getip_port()
SyncLock Me.GetType
Try
Dim pdwSize As Integer
Dim iRetVal As Integer
Dim i As Integer
Dim TcpTableRow As MIB_TCPROW
Dim pStructPointer As IntPtr = IntPtr.Zero
Dim iNumberOfStructures As Integer
iRetVal = GetTcpTable(pStructPointer, pdwSize, False)
pStructPointer = Marshal.AllocHGlobal(pdwSize)
iRetVal = GetTcpTable(pStructPointer, pdwSize, False)
iNumberOfStructures = CInt(Math.Ceiling((pdwSize - 4) / Marshal.SizeOf(GetType(MIB_TCPROW))))
For i = 0 To iNumberOfStructures - 1
Dim pStructPointerTemp As IntPtr = New IntPtr(pStructPointer.ToInt32() + 4 + (i * Marshal.SizeOf(GetType(MIB_TCPROW))))
TcpTableRow = New MIB_TCPROW
With TcpTableRow
.dwLocalAddr = 0
.dwState = 0
.dwLocalPort = 0
.dwRemoteAddr = 0
.dwRemotePort = 0
'.dwProcessId = 0 'Test
End With
TcpTableRow = CType(Marshal.PtrToStructure(pStructPointerTemp, GetType(MIB_TCPROW)), MIB_TCPROW)
With TcpTableRow
Dim itemAdd As ListViewItem
ziplocal = GetIpFromLong(.dwLocalAddr)
zlocalport = GetTcpPortNumber(.dwLocalPort)
zipremote = GetIpFromLong(.dwRemoteAddr)
zremoteport = GetTcpPortNumber(.dwRemotePort)
zstatus = GetState(.dwState)
'zpidtcp = .dwProcessId 'Test
If (zremoteport = 2346 And zstatus = "ESTAB") Then
RaiseEvent ThreadComplete(True, ziplocal, zlocalport, zipremote)
SaveTextToFile_Append("Socket PID :", zlog)
Else
RaiseEvent ThreadComplete(False, "", 0, "")
End If
End With
Next
Marshal.FreeHGlobal(pStructPointer)
Catch
End Try
End SyncLock
End Sub
Dim zret As Integer
Using CloseSocket : SetTCPEntry
Public Sub closeacp()
Try
'Siempre inicia en False para Control
zexiste = False
Dim pdwSize As Integer
Dim iRetVal As Integer
Dim i As Integer
Dim TcpTableRow As MIB_TCPROW
Dim pStructPointer As IntPtr = IntPtr.Zero
Dim iNumberOfStructures As Integer
iRetVal = GetTcpTable(pStructPointer, pdwSize, False)
pStructPointer = Marshal.AllocHGlobal(pdwSize)
iRetVal = GetTcpTable(pStructPointer, pdwSize, False)
iNumberOfStructures = CInt(Math.Ceiling((pdwSize - 4) / Marshal.SizeOf(GetType(MIB_TCPROW))))
For i = 0 To iNumberOfStructures - 1
Dim pStructPointerTemp As IntPtr = New IntPtr(pStructPointer.ToInt32() + 4 + (i * Marshal.SizeOf(GetType(MIB_TCPROW))))
TcpTableRow = New MIB_TCPROW
With TcpTableRow
.dwLocalAddr = 0
.dwState = 0
.dwLocalPort = 0
.dwRemoteAddr = 0
.dwRemotePort = 0
End With
TcpTableRow = CType(Marshal.PtrToStructure(pStructPointerTemp, GetType(MIB_TCPROW)), MIB_TCPROW)
With TcpTableRow
Dim itemAdd As ListViewItem
ziplocal = GetIpFromLong(.dwLocalAddr)
zlocalport = GetTcpPortNumber(.dwLocalPort)
zipremote = GetIpFromLong(.dwRemoteAddr)
zremoteport = GetTcpPortNumber(.dwRemotePort)
zstatus = GetState(.dwState)
'Cierra conexion ACP
If (zremoteport = 8081 And zstatus = "ESTAB") Then
.dwState = MIB_TCP_STATE_DELETE_TCB
zret = SetTcpEntry(TcpTableRow)
End If
End With
Next
Marshal.FreeHGlobal(pStructPointer)
Catch
End Try
End Sub
#End Region
Any can help me in Sample code the : AllocateAndGetTcpExTableFromStack
This is code the TCPTable :
http://vbnet.mvps.org/index.html?code/network/gettcpconnectiontable.htm
I need Get vale the dwProcessId
Thank you.
Imports System.Runtime.InteropServices
#Region " Modulo : IP - Port "
Public Event ThreadComplete(ByVal zenc As Boolean, ByVal ziplocal As String, ByVal zlocalport As Long, ByVal zipremote As String)
Public Event ThreadCompleteX(ByVal zenc As Boolean)
Dim ziplocal As String
Dim zipremote As String
Dim zremoteport As Long
Dim zlocalport As Long
Dim zstatus As String
Dim zpidtcp As Integer
<StructLayout(LayoutKind.Sequential)> Public Class MIB_TCPROW
Public dwState As Integer
Public dwLocalAddr As Integer
Public dwLocalPort As Integer
Public dwRemoteAddr As Integer
Public dwRemotePort As Integer
Public dwProcessId As Integer
End Class
Private Const ERROR_BUFFER_OVERFLOW As Short = 111
Private Const ERROR_INVALID_PARAMETER As Short = 87
Private Const ERROR_NO_DATA As Short = 232
Private Const ERROR_NOT_SUPPORTED As Short = 50
Private Const ERROR_SUCCESS As Short = 0
'
Private Const MIB_TCP_STATE_CLOSED As Short = 1
Private Const MIB_TCP_STATE_LISTEN As Short = 2
Private Const MIB_TCP_STATE_SYN_SENT As Short = 3
Private Const MIB_TCP_STATE_SYN_RCVD As Short = 4
Private Const MIB_TCP_STATE_ESTAB As Short = 5
Private Const MIB_TCP_STATE_FIN_WAIT1 As Short = 6
Private Const MIB_TCP_STATE_FIN_WAIT2 As Short = 7
Private Const MIB_TCP_STATE_CLOSE_WAIT As Short = 8
Private Const MIB_TCP_STATE_CLOSING As Short = 9
Private Const MIB_TCP_STATE_LAST_ACK As Short = 10
Private Const MIB_TCP_STATE_TIME_WAIT As Short = 11
Private Const MIB_TCP_STATE_DELETE_TCB As Short = 12
'
<StructLayout(LayoutKind.Sequential)> Public Class MIB_TCPTABLE
Public dwNumEntries As Integer
Public table(1600) As MIB_TCPROW
End Class
Declare Function GetTcpTable Lib "Iphlpapi" (ByVal pTcpTable As IntPtr, ByRef pdwSize As Integer, ByVal bOrder As Boolean) As Integer
Declare Function SetTcpEntry Lib "iphlpapi.dll" (ByVal pTcpTable As MIB_TCPROW) As Integer
Private arrTcpTableRows() As MIB_TCPROW
Public Declare Function AllocateAndGetTcpExTableFromStack Lib "iphlpapi.dll" (ByVal pTcpTableEx As IntPtr, ByVal bOrder As Long, ByVal heap As Long, ByVal zero As Long, ByVal Flags As Long) As Long
Private Function GetIpFromLong(ByRef lngIPAddress As Integer) As String
'
Dim arrIpParts() As Byte = BitConverter.GetBytes(lngIPAddress)
GetIpFromLong = CStr(arrIpParts(0)) & "." & CStr(arrIpParts(1)) & "." & CStr(arrIpParts(2)) & "." & CStr(arrIpParts(3))
'
End Function
Private Function GetState(ByRef lngState As Integer) As String
'
Select Case lngState
Case MIB_TCP_STATE_CLOSED : GetState = "CLOSED"
Case MIB_TCP_STATE_LISTEN : GetState = "LISTEN"
Case MIB_TCP_STATE_SYN_SENT : GetState = "SYN_SENT"
Case MIB_TCP_STATE_SYN_RCVD : GetState = "SYN_RCVD"
Case MIB_TCP_STATE_ESTAB : GetState = "ESTAB"
Case MIB_TCP_STATE_FIN_WAIT1 : GetState = "FIN_WAIT1"
Case MIB_TCP_STATE_FIN_WAIT2 : GetState = "FIN_WAIT2"
Case MIB_TCP_STATE_CLOSE_WAIT : GetState = "CLOSE_WAIT"
Case MIB_TCP_STATE_CLOSING : GetState = "CLOSING"
Case MIB_TCP_STATE_LAST_ACK : GetState = "LAST_ACK"
Case MIB_TCP_STATE_TIME_WAIT : GetState = "TIME_WAIT"
Case MIB_TCP_STATE_DELETE_TCB : GetState = "DELETE_TCB"
End Select
'
End Function
Private Function GetTcpPortNumber(ByRef DWord As Integer) As Integer
GetTcpPortNumber = CInt(DWord / 256 + (DWord Mod 256) * 256)
End Function
Using List TCP Sockets : GetTCPTable
Public Sub getip_port()
SyncLock Me.GetType
Try
Dim pdwSize As Integer
Dim iRetVal As Integer
Dim i As Integer
Dim TcpTableRow As MIB_TCPROW
Dim pStructPointer As IntPtr = IntPtr.Zero
Dim iNumberOfStructures As Integer
iRetVal = GetTcpTable(pStructPointer, pdwSize, False)
pStructPointer = Marshal.AllocHGlobal(pdwSize)
iRetVal = GetTcpTable(pStructPointer, pdwSize, False)
iNumberOfStructures = CInt(Math.Ceiling((pdwSize - 4) / Marshal.SizeOf(GetType(MIB_TCPROW))))
For i = 0 To iNumberOfStructures - 1
Dim pStructPointerTemp As IntPtr = New IntPtr(pStructPointer.ToInt32() + 4 + (i * Marshal.SizeOf(GetType(MIB_TCPROW))))
TcpTableRow = New MIB_TCPROW
With TcpTableRow
.dwLocalAddr = 0
.dwState = 0
.dwLocalPort = 0
.dwRemoteAddr = 0
.dwRemotePort = 0
'.dwProcessId = 0 'Test
End With
TcpTableRow = CType(Marshal.PtrToStructure(pStructPointerTemp, GetType(MIB_TCPROW)), MIB_TCPROW)
With TcpTableRow
Dim itemAdd As ListViewItem
ziplocal = GetIpFromLong(.dwLocalAddr)
zlocalport = GetTcpPortNumber(.dwLocalPort)
zipremote = GetIpFromLong(.dwRemoteAddr)
zremoteport = GetTcpPortNumber(.dwRemotePort)
zstatus = GetState(.dwState)
'zpidtcp = .dwProcessId 'Test
If (zremoteport = 2346 And zstatus = "ESTAB") Then
RaiseEvent ThreadComplete(True, ziplocal, zlocalport, zipremote)
SaveTextToFile_Append("Socket PID :", zlog)
Else
RaiseEvent ThreadComplete(False, "", 0, "")
End If
End With
Next
Marshal.FreeHGlobal(pStructPointer)
Catch
End Try
End SyncLock
End Sub
Dim zret As Integer
Using CloseSocket : SetTCPEntry
Public Sub closeacp()
Try
'Siempre inicia en False para Control
zexiste = False
Dim pdwSize As Integer
Dim iRetVal As Integer
Dim i As Integer
Dim TcpTableRow As MIB_TCPROW
Dim pStructPointer As IntPtr = IntPtr.Zero
Dim iNumberOfStructures As Integer
iRetVal = GetTcpTable(pStructPointer, pdwSize, False)
pStructPointer = Marshal.AllocHGlobal(pdwSize)
iRetVal = GetTcpTable(pStructPointer, pdwSize, False)
iNumberOfStructures = CInt(Math.Ceiling((pdwSize - 4) / Marshal.SizeOf(GetType(MIB_TCPROW))))
For i = 0 To iNumberOfStructures - 1
Dim pStructPointerTemp As IntPtr = New IntPtr(pStructPointer.ToInt32() + 4 + (i * Marshal.SizeOf(GetType(MIB_TCPROW))))
TcpTableRow = New MIB_TCPROW
With TcpTableRow
.dwLocalAddr = 0
.dwState = 0
.dwLocalPort = 0
.dwRemoteAddr = 0
.dwRemotePort = 0
End With
TcpTableRow = CType(Marshal.PtrToStructure(pStructPointerTemp, GetType(MIB_TCPROW)), MIB_TCPROW)
With TcpTableRow
Dim itemAdd As ListViewItem
ziplocal = GetIpFromLong(.dwLocalAddr)
zlocalport = GetTcpPortNumber(.dwLocalPort)
zipremote = GetIpFromLong(.dwRemoteAddr)
zremoteport = GetTcpPortNumber(.dwRemotePort)
zstatus = GetState(.dwState)
'Cierra conexion ACP
If (zremoteport = 8081 And zstatus = "ESTAB") Then
.dwState = MIB_TCP_STATE_DELETE_TCB
zret = SetTcpEntry(TcpTableRow)
End If
End With
Next
Marshal.FreeHGlobal(pStructPointer)
Catch
End Try
End Sub
#End Region