Click here to Skip to main content

keshtkar2 asked:

Open original thread
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.
Tags: C#, ASP.NET, Sockets

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid