Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

Changing Route Table from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cyberco - 20 Nov 2006 20:57 GMT
I've posted this question in
'microsoft.public.dotnet.framework.compactframework' as well, but
despite the great help I still haven't solved the problem. So before
going the C++ route I would like to see if anybody in this group may
have the answer.

What I'm trying to do is change the route table of my Windows Mobile 5
Pocket PC (.Net CF) device (TyTN) using C#. This is probably similar as
doing it on standard CF. Using Iphlpapi.dll I can retrieve the route
table, but I am unable to change/add a route. I have specified the
following structs/pinvokes:

=========START===========
[StructLayout(LayoutKind.Sequential)]
public struct MIB_IPFORWARDROW {
   public UInt32 dwForwardDest;        //destination IP address.
   public UInt32 dwForwardMask;        //Subnet mask
   public UInt32 dwForwardPolicy;      //conditions for multi-path
route. Unused, specify 0.
   public UInt32 dwForwardNextHop;     //IP address of the next hop.
Own address?
   public UInt32 dwForwardIfIndex;     //index of interface
   public UInt32 dwForwardType;        //route type
   public UInt32 dwForwardProto;       //routing protocol.
   public UInt32 dwForwardAge;         //age of route.
   public UInt32 dwForwardNextHopAS;   //autonomous system number. 0
if not relevant
   public int    dwForwardMetric1;     //-1 if not used (goes for all
metrics)
   public int    dwForwardMetric2;
   public int    dwForwardMetric3;
   public int    dwForwardMetric4;
   public int    dwForwardMetric5;
}

[StructLayout(LayoutKind.Sequential)]
public struct MIB_IPFORWARDTABLE {
   public int dwNumEntries;            //number of route entries in
the table.
   public MIB_IPFORWARDROW[] table;
}

[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int CreateIpForwardEntry(ref MIB_IPFORWARDROW
pRoute);

[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int DeleteIpForwardEntry(ref MIB_IPFORWARDROW
pRoute);

[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int SetIpForwardEntry(ref MIB_IPFORWARDROW
pRoute);

[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int GetIpForwardTable(byte[] pIpForwardTable, out
int pdwSize, bool bOrder);

=========END===========

I invoke these methods as follows:

=========START===========
public int createIpForwardEntry(UInt32 destIPAddress, UInt32 destMask,
                                           UInt32 nextHopIPAddress,
UInt32 ifIndex) {
   MIB_IPFORWARDROW mifr = new MIB_IPFORWARDROW();
   mifr.dwForwardDest = destIPAddress;
   mifr.dwForwardMask = destMask;
   mifr.dwForwardPolicy = Convert.ToUInt32(0);
   mifr.dwForwardNextHop = nextHopIPAddress;
   mifr.dwForwardIfIndex = ifIndex; //?
   mifr.dwForwardType = Convert.ToUInt32(4);
   mifr.dwForwardProto = Convert.ToUInt32(3);
   mifr.dwForwardAge = Convert.ToUInt32(0);
   mifr.dwForwardNextHopAS = Convert.ToUInt32(0);
   mifr.dwForwardMetric1 = -1;
   mifr.dwForwardMetric2 = -1;
   mifr.dwForwardMetric3 = -1;
   mifr.dwForwardMetric4 = -1;
   mifr.dwForwardMetric5 = -1;
   return CreateIpForwardEntry(ref mifr);
}
=========END===========

All IP addresses are added as unsigned ints (double checked whether
they are correct. I even tried adding a row that I just successfully
retrieved (thereby skipping my own structure). If that gives me an
error code 87 (invalid param).

Is there anybody here that has successfully done this or has a few
relevant pointers? Preferably C# examples, but C++ examples are a step
in the right direction. Thanks!
Mubashir Khan - 21 Nov 2006 14:02 GMT
i would suggest u firstly use the get api and observe your kind of ip entry
for parameter values .... this would be helpful
> I've posted this question in
> 'microsoft.public.dotnet.framework.compactframework' as well, but
[quoted text clipped - 93 lines]
> relevant pointers? Preferably C# examples, but C++ examples are a step
> in the right direction. Thanks!
cyberco - 27 Nov 2006 22:58 GMT
I figured maybe the problem is that I should reserve some memory for
the new MIB_IPFORWARDROW I'm creating (as I do in the following code)?

> =========START===========
> public int createIpForwardEntry(UInt32 destIPAddress, UInt32 destMask,
[quoted text clipped - 17 lines]
>     return CreateIpForwardEntry(ref mifr);}
> =========END===========

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.