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;
int BufferSize = 300*CMIB_TCPEXROW.size_ex+4;
IntPtr 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;
}
int NumEntries= (int)Marshal.ReadIntPtr(lpTable);
real_buffer_size=NumEntries*CMIB_TCPEXROW.size_ex+4;
if (BufferSize<real_buffer_size)
{
Marshal.FreeHGlobal(lpTable);
BufferSize = real_buffer_size;
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);
Marshal.FreeHGlobal(lpTable);
CMIB_TCPEXTABLE mtet = new CMIB_TCPEXTABLE();
mtet.decode(array);
return mtet;
}
}
Your help will be appreciated