List Info

Thread: C-Sharp (C#) Group: Help with c# in route api




C-Sharp (C#) Group: Help with c# in route api
user name
2006-08-21 04:32:23
Hi all,

Iam new to c# . The problem is that i want to add/del/edit
system
routes using c#.
I used the calls like GetIpForwardTable() to do the above
but it is not
working. Any sample on how to use route table manuplation
calls in c#
will be of great help. Its urgent please help me.

The code i tried that is not working is as follows:

public class RouteMgmt
    {
        [DllImport("Iphlpapi.dll")]
        [return: MarshalAs(UnmanagedType.U4)]
        public extern static int CreateIpForwardEntry(ref
MIB_IPFORWARDROW pRoute);
        [DllImport("Iphlpapi.dll")]
        [return: MarshalAs(UnmanagedType.U4)]
        public extern static int GetIpForwardTable(out
MIB_IPFORWARDTABLE pTable,out int size,bool order);


[StructLayout(LayoutKind.Sequential)]
        public struct MIB_IPFORWARDROW
        {
            public ulong dwForwardDest;
            public ulong dwForwardMask;
            public ulong dwForwardPolicy;
            public ulong dwForwardNextHop;
            public ulong dwForwardIfIndex;
            public ulong dwForwardType;
            public ulong dwForwardProto;
            public ulong dwForwardAge;
            public ulong dwForwardNextHopAS;
            public ulong dwForwardMetric1;
            public ulong dwForwardMetric2;
            public ulong dwForwardMetric3;
            public ulong dwForwardMetric4;
            public ulong dwForwardMetric5;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct MIB_IPFORWARDTABLE
        {
            public UInt32 dwNumEntries;
            //public MIB_IPFORWARDROW[] table;
            //public MIB_IPFORWARDROW* ptr;
            public char[] arr;
            //public UInt32 tables[];
        }

        public void AddRoute(string Dstipaddr,string
DstMsk,string
ttl,string metric,string gateway)
        {
            int ret;
            ulong newgw = 0x51615172;
            ulong newdest = 0x461615172;
            ulong newmsk = 0x0255255255;
            MIB_IPFORWARDROW routeadd = new
MIB_IPFORWARDROW();
            IPAddress TempIP = IPAddress.Parse(Dstipaddr);
            IPAddress TempMsk = IPAddress.Parse(DstMsk);
            IPAddress TempGw = IPAddress.Parse(gateway);
            //UInt32 TempDec = Convert.ToUInt32(Dstipaddr);
            //routeadd.dwForwardDest =
Convert.ToUInt32(TempIP.Address);
            //routeadd.dwForwardMask =
Convert.ToUInt32(TempMsk.Address);
            routeadd.dwForwardDest = newdest;
            routeadd.dwForwardMask = 24;
            //routeadd.dwForwardAge =
Convert.ToUInt32(IPAddress.HostToNetworkOrder(Convert.ToInt3
2(ttl)));
            //routeadd.dwForwardAge = Convert.ToUInt32(ttl);
            //routeadd.dwForwardMetric1 =
Convert.ToUInt32(metric);
            routeadd.dwForwardAge = 5000;
            routeadd.dwForwardMetric1 = 4;

            //routeadd.dwForwardMetric2 = -1;
            //routeadd.dwForwardMetric3 = -1;
            //routeadd.dwForwardMetric4 = -1;
            //routeadd.dwForwardMetric5 = -1;
            routeadd.dwForwardIfIndex = 0;
            //routeadd.dwForwardNextHop =
Convert.ToUInt32(TempGw.Address);
            routeadd.dwForwardNextHop = newgw;
            routeadd.dwForwardProto = 3;
            routeadd.dwForwardType = 4;

            if ((ret = CreateIpForwardEntry(ref routeadd))
!= 0)
            {


            }



        }
        public void DisplayRoute()
        {
            MIB_IPFORWARDTABLE routedisp = new
MIB_IPFORWARDTABLE();
            int dwSize = 0;
            bool order = true;
            int ret;
            int stusize;



            if ((ret = GetIpForwardTable(out routedisp,out
dwSize,order)) != 0)
            {
                //stusize =
(dwSize/Marshal.SizeOf(typeof(MIB_IPFORWARDROW)));
                //routedisp.table = new
MIB_IPFORWARDROW[stusize];
                routedisp.arr = new char[dwSize];
            }

            if ((ret = GetIpForwardTable(out routedisp, out
dwSize,
order)) != 0)
            {

            }

            MIB_IPFORWARDROW[] temp = new
MIB_IPFORWARDROW[dwSize];
            for (int i = 0; i <= dwSize; i++)
            {
                //temp[i] =
(MIB_IPFORWARDROW)Marshal.PtrToStructure(ref routedisp.arr,
typeof(MIB_IPFORWARDROW));
                //temp[i] = Marshal.
                //temp = routedisp.table[0];
            }

        }

    }


 I dont know what the problem is. Please help to correct the
above code
or give me a sample code for the same. Its urgent please
help.

Varun


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )