Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Sockets
I have a big code in c#and asp.net , it connects to IPHLPAPI.lib on windows 7 (64 bit) and return TCP and UDP connections informations in XMl,
You can see the pieces of code which stop with error in following:
 
 

    #region GetTcpExTable
        [DllImport("iphlpapi.dll",SetLastError=true)]
        private extern static 
            UInt32 AllocateAndGetTcpExTableFromStack(
            ref IntPtr pTcpTable,
            bool bOrder,
            IntPtr heap,
            UInt32 zero,
            UInt32 flags
            ); 
        public static CMIB_TCPEXTABLE GetTcpExTable()
        {
            UInt32 error_code=0;
            return iphelper.GetTcpExTable(true,ref error_code);
        }
        public static CMIB_TCPEXTABLE GetTcpExTable(bool b_verbose,ref UInt32 error_code)
        {
            // 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
            error_code= AllocateAndGetTcpExTableFromStack(ref lpTable, true, GetProcessHeap(),0,2);
            // AllocateAndGetTcpExTableFromStack
            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);
            int 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);
 
                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;
        }
    #endregion
 
        //MSDN:"Currently, the only state to which a TCP connection can be set is MIB_TCP_STATE_DELETE_TCB"
        //return 65 - User has no sufficient privilege to execute this API successfully 
        //return 87 - Specified port is not in state to be closed down.
        [DllImport("iphlpapi.dll",SetLastError=true)]
        public extern static 
            UInt32 SetTcpEntry(
            ref MIB_TCPROW pTcpRow
            );
 
The underlined code is stop with error:
"Unable to find an entry point named 'AllocateAndGetTcpExTableFromStack' in DLL 'iphlpapi.dll'."
 
I know that i should change the member "AllocateAndGetTcpExTableFromStack" to Newer member like "GetTcpExtendedTable" , but I do not know how to make these changes? what should i put as input/output parameters.. and what other changes I should do in other part of my code (other classes) to be worked correctly by "GetTcpExtendedTable" member?
( and I should make all these changes again for UDP part)
 
Your Help Will Be Appreciated.
Posted 14-Jan-13 3:56am
Edited 14-Jan-13 3:58am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Check this link for how to use GetExtendedTcpTable[^] API and related sample can be found here
Getting the active TCP/UDP connections using the GetExtendedTcpTable function[^]
  Permalink  
v2
Comments
Mohammad Amin Keshtkar at 15-Jan-13 23:49pm
   
I saw that post, it uses getExtendedTcpTable member, ( however this is not work in win7 64bit at all) but it does not how to move GetExtendedTcpTable from the existing code which uses AllocateAndGetTcpTable member.

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

  Print Answers RSS
0 OriginalGriff 540
1 Sergey Alexandrovich Kryukov 432
2 Yogesh Kumar Tyagi 384
3 Prakriti Goyal 280
4 Maciej Los 205
0 OriginalGriff 6,472
1 Sergey Alexandrovich Kryukov 5,404
2 Maciej Los 3,434
3 Peter Leow 3,259
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100