public static string dump_MIB_TCPROW(MIB_TCPROW mib) { string s = "n/a"; s = dumpEndPoint(mib.Local) + " " + dumpEndPoint(mib.Remote) + " " + mib.StrgState; return(s); }
//The function that fills the MIB_TCPROW array with connectioninfos private static MIB_TCPROW[] getTcpTable() { IntPtr buffer = IntPtr.Zero; bool allocated = false; try { int iBytes = 0; GetTcpTable(IntPtr.Zero, ref iBytes, false); //Getting size of return data buffer = Marshal.AllocCoTaskMem(iBytes); //allocating the datasize allocated = true; GetTcpTable(buffer, ref iBytes, false); //Run it again to fill the memory with the data int structCount = Marshal.ReadInt32(buffer); // Get the number of structures IntPtr buffSubPointer = buffer; //Making a pointer that will point into the buffer buffSubPointer = (IntPtr)((int)buffer + 4); //Move to the first data (ignoring dwNumEntries from the original MIB_TCPTABLE struct) MIB_TCPROW[] tcpRows = new MIB_TCPROW[structCount]; //Declaring the array //Get the struct size MIB_TCPROW tmp = new MIB_TCPROW(); int sizeOfTCPROW = Marshal.SizeOf(tmp); //Fill the array 1 by 1 for (int i = 0; i < structCount; i++) { tcpRows[i] = (MIB_TCPROW)Marshal.PtrToStructure(buffSubPointer, typeof(MIB_TCPROW)); //copy struct data buffSubPointer = (IntPtr)((int)buffSubPointer + sizeOfTCPROW); //move to next structdata } return tcpRows; } catch (Exception ex) { throw new Exception("getTcpTable failed! [" + ex.GetType().ToString() + "," + ex.Message + "]"); } finally { if (allocated) Marshal.FreeCoTaskMem(buffer); //Free the allocated memory } }
public static string dump_MIB_TCPROW(MIB_TCPROW mib) { string s = "n/a"; s = dumpEndPoint(mib.Local) + " " + dumpEndPoint(mib.Remote) + " " + mib.StrgState; return s; }