public UdpConnectExInfo GetExUdpConnexions() { // the size of the MIB_EXTCPROW struct = 4*DWORD int rowsize = 12; int BufferSize = 100000; // allocate a dumb memory space in order to retrieve nb of connexion IntPtr lpTable = Marshal.AllocHGlobal(BufferSize); //getting infos int res = ConInfosHlpAPI32Wrapper.AllocateAndGetUdpExTableFromStack(ref lpTable, true, ConInfosHlpAPI32Wrapper.GetProcessHeap(), 0, 2); if (res != NO_ERROR) { Debug.WriteLine("Erreur : " + ConInfosHlpAPI32Wrapper.GetAPIErrorMessageDescription(res) + " " + res); return UdpExConnexion; // Error. You should handle it } int CurrentIndex = 0; //get the number of entries in the table int NumEntries = (int)Marshal.ReadIntPtr(lpTable); lpTable = IntPtr.Zero; // free allocated space in memory Marshal.FreeHGlobal(lpTable); /////////////////// // calculate the real buffer size nb of entrie * size of the struct for each entrie(24) + the dwNumEntries BufferSize = (NumEntries * rowsize) + 4; // make the struct to hold the resullts UdpExConnexion = new UdpConnectExInfo(); // Allocate memory lpTable = Marshal.AllocHGlobal(BufferSize); res = ConInfosHlpAPI32Wrapper.AllocateAndGetUdpExTableFromStack(ref lpTable, true, ConInfosHlpAPI32Wrapper.GetProcessHeap(), 0, 2); if (res != NO_ERROR) { Debug.WriteLine("Erreur : " + ConInfosHlpAPI32Wrapper.GetAPIErrorMessageDescription(res) + " " + res); return UdpExConnexion; // Error. You should handle it } // New pointer of iterating throught the data IntPtr current = lpTable; CurrentIndex = 0; // get the (again) the number of entries NumEntries = (int)Marshal.ReadIntPtr(current); UdpExConnexion.dwNumEntries = NumEntries; // Make the array of entries UdpExConnexion.table = new UdpConnectExTable[NumEntries]; // iterate the pointer of 4 (the size of the DWORD dwNumEntries) CurrentIndex += 4; current = (IntPtr)((int)current + CurrentIndex); // for each entries for (int i = 0; i < NumEntries; i++) { // get the local address of the connexion UInt32 localAddr = (UInt32)Marshal.ReadIntPtr(current); // iterate the pointer of 4 current = (IntPtr)((int)current + 4); // get the local port of the connexion UInt32 localPort = (UInt32)Marshal.ReadIntPtr(current); // iterate the pointer of 4 current = (IntPtr)((int)current + 4); // Store the local endpoint in the struct and convertthe port in decimal (ie convert_Port()) UdpExConnexion.table[i].Local = new IPEndPoint(localAddr, convert_Port(localPort)); // store the process ID UdpExConnexion.table[i].dwProcessId = (int)Marshal.ReadIntPtr(current); // Store and get the process name in the struct UdpExConnexion.table[i].ProcessName = this.get_process_name(UdpExConnexion.table[i].dwProcessId); current = (IntPtr)((int)current + 4); } // free the buffer Marshal.FreeHGlobal(lpTable); // re init the pointer current = IntPtr.Zero; return UdpExConnexion; }
public UdpConnectExInfo GetExUdpConnexions() { // the size of the MIB_EXTCPROW struct = 4*DWORD int rowsize = 12; int BufferSize = 100000; // allocate a dumb memory space in order to retrieve nb of connexion IntPtr lpTable = Marshal.AllocHGlobal(BufferSize); //getting infos int res = ConInfosHlpAPI32Wrapper.AllocateAndGetUdpExTableFromStack(ref lpTable, true, ConInfosHlpAPI32Wrapper.GetProcessHeap(), 0, 2); if (res != NO_ERROR) { Debug.WriteLine("Erreur : " + ConInfosHlpAPI32Wrapper.GetAPIErrorMessageDescription(res) + " " + res); return(UdpExConnexion); // Error. You should handle it } int CurrentIndex = 0; //get the number of entries in the table int NumEntries = (int)Marshal.ReadIntPtr(lpTable); lpTable = IntPtr.Zero; // free allocated space in memory Marshal.FreeHGlobal(lpTable); /////////////////// // calculate the real buffer size nb of entrie * size of the struct for each entrie(24) + the dwNumEntries BufferSize = (NumEntries * rowsize) + 4; // make the struct to hold the resullts UdpExConnexion = new UdpConnectExInfo(); // Allocate memory lpTable = Marshal.AllocHGlobal(BufferSize); res = ConInfosHlpAPI32Wrapper.AllocateAndGetUdpExTableFromStack(ref lpTable, true, ConInfosHlpAPI32Wrapper.GetProcessHeap(), 0, 2); if (res != NO_ERROR) { Debug.WriteLine("Erreur : " + ConInfosHlpAPI32Wrapper.GetAPIErrorMessageDescription(res) + " " + res); return(UdpExConnexion); // Error. You should handle it } // New pointer of iterating throught the data IntPtr current = lpTable; CurrentIndex = 0; // get the (again) the number of entries NumEntries = (int)Marshal.ReadIntPtr(current); UdpExConnexion.dwNumEntries = NumEntries; // Make the array of entries UdpExConnexion.table = new UdpConnectExTable[NumEntries]; // iterate the pointer of 4 (the size of the DWORD dwNumEntries) CurrentIndex += 4; current = (IntPtr)((int)current + CurrentIndex); // for each entries for (int i = 0; i < NumEntries; i++) { // get the local address of the connexion UInt32 localAddr = (UInt32)Marshal.ReadIntPtr(current); // iterate the pointer of 4 current = (IntPtr)((int)current + 4); // get the local port of the connexion UInt32 localPort = (UInt32)Marshal.ReadIntPtr(current); // iterate the pointer of 4 current = (IntPtr)((int)current + 4); // Store the local endpoint in the struct and convertthe port in decimal (ie convert_Port()) UdpExConnexion.table[i].Local = new IPEndPoint(localAddr, convert_Port(localPort)); // store the process ID UdpExConnexion.table[i].dwProcessId = (int)Marshal.ReadIntPtr(current); // Store and get the process name in the struct UdpExConnexion.table[i].ProcessName = this.get_process_name(UdpExConnexion.table[i].dwProcessId); current = (IntPtr)((int)current + 4); } // free the buffer Marshal.FreeHGlobal(lpTable); // re init the pointer current = IntPtr.Zero; return(UdpExConnexion); }