Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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:

C#
#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
Updated 14-Jan-13 3:58am
v2

1 solution

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[^]
 
Share this answer
 
v2
Comments
keshtkar2 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
Top Experts
Last 24hrsThis month


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