Click here to Skip to main content
15,885,868 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have this C# code to use GetExtendedTcpTable function and Get Extended Tcp table, but during compile it stop in line :

int NumEntries= (int)Marshal.ReadIntPtr(lpTable);


with :Arithmetic OverFlow Exception erroe.

but it runs on 32 bit operation system without any error.

now my Question is that, whats the righe code for using GetExtendedTcpTable function on 64 bit OS? what change should I apply on my current code?

My complete code:
(GetTcpExTable region) :

public enum TCP_TABLE_CLASS
   {
       TCP_TABLE_BASIC_LISTENER,
       TCP_TABLE_BASIC_CONNECTIONS,
       TCP_TABLE_BASIC_ALL,
       TCP_TABLE_OWNER_PID_LISTENER,
       TCP_TABLE_OWNER_PID_CONNECTIONS,
       TCP_TABLE_OWNER_PID_ALL,
       TCP_TABLE_OWNER_MODULE_LISTENER,
       TCP_TABLE_OWNER_MODULE_CONNECTIONS,
       TCP_TABLE_OWNER_MODULE_ALL,
   }

   [DllImport("iphlpapi.dll",SetLastError=true)]
   private extern static
       UInt32 AllocateAndGetTcpExTableFromStack(
       ref IntPtr pTcpTable,
       bool bOrder,
       IntPtr heap,
       UInt32 zero,
       UInt32 flags
       );

   [DllImport("iphlpapi.dll",SetLastError=true)]
   public static extern UInt32 GetExtendedTcpTable(
       IntPtr pTcpTable,
       ref Int32 dwSize,
       bool bOrder,
       UInt32 ulAfInetVersion,
       TCP_TABLE_CLASS TableClass,
       UInt32 Reserved);


   public static CMIB_TCPEXTABLE GetTcpExTable(bool bIpV6)
   {
       UInt32 error_code=0;
       return iphelper.GetTcpExTable(true,ref error_code,bIpV6);
   }
   public static CMIB_TCPEXTABLE GetTcpExTable(bool b_verbose,ref UInt32 error_code,bool bIpV6)
   {
       System.OperatingSystem osInfo = System.Environment.OSVersion;

       else
       {

           int real_buffer_size=0;
           // allocate a dump memory space in order to retrieve nb of connexion
           int BufferSize = 300*CMIB_TCPEXROW.size_ex+4;//NumEntries*CMIB_TCPEXROW.size_ex+4
           IntPtr lpTable = Marshal.AllocHGlobal(BufferSize);
           //getting infos
           if (osInfo.Version.Major>=6)
           {
               real_buffer_size = BufferSize;
               error_code= GetExtendedTcpTable(lpTable,ref real_buffer_size,true,2,TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL,0);
           }
           else
               error_code= AllocateAndGetTcpExTableFromStack(ref lpTable, true, GetProcessHeap(),0,2);
           if (error_code!=0)
           {
               if (b_verbose)
               {
                   string str_msg=Tools.API.API_error.GetAPIErrorMessageDescription(error_code);
                   System.Windows.Forms.MessageBox.Show( str_msg,"Error",
                       System.Windows.Forms.MessageBoxButtons.OK,
                       System.Windows.Forms.MessageBoxIcon.Error);
               }
               return null;
           }
           //get the number of entries in the table
           int NumEntries= (int)Marshal.ReadIntPtr(lpTable);
           real_buffer_size=NumEntries*CMIB_TCPEXROW.size_ex+4;

           // check if memory was enougth (needed buffer size: NumEntries*MIB_UDPEXROW.size_ex +4 (for dwNumEntries))
           if (BufferSize<real_buffer_size)
           {
               // free the buffer
               Marshal.FreeHGlobal(lpTable);

               // get the needed buffer size: NumEntries*MIB_TCPEXROW.size_ex +4 (for dwNumEntries)
               BufferSize = real_buffer_size;
               // Allocate memory
               lpTable = Marshal.AllocHGlobal(BufferSize);
               if (osInfo.Version.Major>=6)
               {
                   real_buffer_size = BufferSize;
                   error_code= GetExtendedTcpTable(lpTable,ref real_buffer_size,true,2,TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL,0);
               }
               else
                   error_code=AllocateAndGetTcpExTableFromStack(ref lpTable,true,GetProcessHeap(),0,2);
               if (error_code!=0)
               {
                   if (b_verbose)
                   {
                       string str_msg=Tools.API.API_error.GetAPIErrorMessageDescription(error_code);
                       System.Windows.Forms.MessageBox.Show( str_msg,"Error",
                           System.Windows.Forms.MessageBoxButtons.OK,
                           System.Windows.Forms.MessageBoxIcon.Error);
                   }
                   return null;
               }
           }
           BufferSize=real_buffer_size;
           byte[] array=new byte[BufferSize];
           Marshal.Copy(lpTable,array,0,BufferSize);
           // free the buffer
           Marshal.FreeHGlobal(lpTable);
           CMIB_TCPEXTABLE mtet = new CMIB_TCPEXTABLE();
           mtet.decode(array);
           return mtet;
       }
   }









Your help will be appreciated
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900