Adding route entry fails with different gateways

Posted on 2007-07-30
Last Modified: 2013-11-20
I am using Windows IP helper functions to add an entry to the routing table which directs all requests to the 82.x.x.x through a VPN connection which has an interface IP 192.168.x.x. The VPN connection is started through a call to RasDial().

When I use my local network's router IP the gateway, which is also the default gateway, the CreateIpForwardEntry() successfully adds the new route to the route table. But I want to make the gateway for that connection same as the 192.168.x.x, but when I try that, the CreateIpForwardEntry() fails with error ERROR_INVALID_PARAMETER.

I tried with different metrics for the connection, but none of them work. Here's the code segment I am using.

MIB_IPFORWARDROW  routeEntry;      //      route entry data
      memset(&routeEntry, 0, sizeof(MIB_IPFORWARDROW));


      CString s;
      DWORD dwIndex=0,dwMask=0, dwVPNIP;;
      in_addr in;
      routeEntry.dwForwardIfIndex = dwIndex;
    routeEntry.dwForwardMetric1 = 20;      //      hard coded ************

    // some default values
    routeEntry.dwForwardProto =MIB_IPPROTO_LOCAL;// MIB_IPPROTO_LOCAL;
    routeEntry.dwForwardMetric2 = 1;
    routeEntry.dwForwardMetric3 = 1;
    routeEntry.dwForwardMetric4 = 1;
      DWORD dwStatus;

      if ((dwStatus=CreateIpForwardEntry(&routeEntry))!= NO_ERROR)
                  MessageBox("Invalid param");
            s.Format("Adding entry failed Status %d",dwStatus);

What might have cause this problem?

Question by:shaf81
    LVL 22

    Expert Comment

    >> routeEntry.dwForwardProto =MIB_IPPROTO_LOCAL;// MIB_IPPROTO_LOCAL;

    Is that correct ? it should not be MIB_IPPROTO_NETMGMT  ?? ie. 3 ??

    LVL 22

    Expert Comment

    The error usually means that one of the members of the MIB_IPFORWARDROW structure is invalid. You can try to use route.exe and execute "route add" command to add the entry. Thus, you can test whether the parameters are
    correct or not.


    LVL 3

    Author Comment

    I've used routeEntry.dwForwardProto=MIB_IPPROTO_NETMGMT initially, but it failed ,too. "route add" successfully adds the route. Confused?!
    LVL 22

    Expert Comment

    changin values of dwForwardMetric2 ,dwForwardMetric3 ,dwForwardMetric4
    to ' -1' ?

    LVL 3

    Author Comment

    Sorry Mahesh, I tried initially with those values set to -1 but failed and then only tried with 1 which also fails.
    LVL 3

    Author Comment

    Any experts out there who can help me?

    On the command prompt, when I use:

    Route ADD 82.x.x.x MASK 192.168.x.x  IF 0x4003 - it works

    Also this too works:

    Route ADD 82.x.x.x MASK IF 0x4003

    But with CreateIpForwardEntry() only gateway work, and not the VPN gateway. What am I doing wrong?

    Please help.
    LVL 3

    Author Comment

    Thanks experts,
    I've resolved the problem myself !!!!!!  routeEntry,ForwardType must be set to 4 (The next hop is not the final destination (remote route)) to make this happen. When I memset(), this value becomes 0 which falls to none of the allowed values in the documentation.
    LVL 1

    Accepted Solution

    Closed, 500 points refunded.
    Community Support Moderator

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    VPN Client  - Windows 10 11 106
    Multicast MAC Addresses in VMware vSwitches 3 47
    FortiGate problem 8 37
    Mac OS 10.12 + VPN 17 105
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    Let’s list some of the technologies that enable smooth teleworking. 
    After creating this article (, I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now