Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


RouteAdd UDF

Keywords:    routeadd udf  iphlpapi.DLL

Here is a routine that does the equivalent of a "Route Add" command, in Winbatch.

It uses the "iphlpapi.DLL". The documentation says this DLL is available in Win98 and higher - but I found it on my win95 machine (don't know how it got there) and it worked fine. I have also tested it on NT4 and Win2K.

No Guarantees. Use at your own risk. Etc. Etc.

Here it is...

;---------------------------------------------------------------
;
;  Create a static route.  (Same as ROUTE ADD command.)
;
;---------------------------------------------------------------
#DefineFunction uRouteAdd(Address, Mask, Router)
  hDLL = DllLoad(StrCat(DirWindows(1),"\iphlpapi.dll"))
  ;
  ;  Read the current table, in order to get the interface indes
  ;
  MibSize = BinaryAlloc(4)
  MIB = BinaryAlloc(2048)
  BinaryPoke4(MibSize,0,2048)
  X = DllCall(hDLL,long:"GetIpForwardTable",lpbinary:MIB,lpbinary:MibSize,long:0)
  Required = BinaryPeek4(MibSize,0)
  if Required > 2048
      BinaryFree(MIB)
      Required = Required
      MIB = BinaryAlloc(Required)
      BinaryPoke4(MibSize,0,Required)
      X = DllCall(hDLL,long:"GetIpForwardTable",lpbinary:MIB,lpbinary:MibSize,long:0)
  endif
  BinaryFree(MibSize)
  Interface = BinaryPeek4(Mib,20)
  ;
  ;  Create a MIB table for the new route 
  ;
  X = 0
  For J = 1 to 4
    Byte = int(ItemExtract(J,Address,"."))   ; IP address
    BinaryPoke(MIB,X,Byte)               
    X = X + 1
  Next
  For J = 1 to 4
    Byte =  int(ItemExtract(J,Mask,"."))  ; Mask
    BinaryPoke(MIB,X,Byte)
    X = X + 1
  Next
  BinaryPoke4(MIB,X,0)                    ; Forward policy
  X = X + 4
  For J = 1 to 4
    Byte = int(ItemExtract(J,Router,".")) ; Next hop
    BinaryPoke(MIB,X,Byte)
    X = X + 1
  Next
  BinaryPoke4(MIB,X,Interface)            ; Index of interface
  X = X + 4
  BinaryPoke4(MIB,X,4)                    ; Forward type
  X = X + 4                               ; 4 = not final destination
  BinaryPoke4(MIB,X,3)                    ; Protocol that generated route
  X = X + 4                               
  BinaryPoke4(MIB,X,0)                    ; Forward Age
  X = X + 4
  BinaryPoke4(MIB,X,0)                    ; System number of next hop
  X = X + 4
  BinaryPoke4(MIB,X,1)                    ; Metric 1
  X = X + 4
  BinaryPoke4(MIB,X,-1)                   ; Metric 2
  X = X + 4
  BinaryPoke4(MIB,X,-1)                   ; Metric 3
  X = X + 4
  BinaryPoke4(MIB,X,-1)                   ; Metric 4
  X = X + 4
  BinaryPoke4(MIB,X,-1)                   ; Metric 5
  ;
  ;   Create the route
  ;
  X = DllCall(hDLL,long:"CreateIpForwardEntry",lpbinary:MIB)
  BinaryFree(MIB)
  DllFree(hDLL)
  return X
#EndFunction

;********************************************************************
;   TEST THE FUNCTION
;********************************************************************
x = uRouteAdd("10.11.12.13","255.255.255.255","192.168.169.170")
if X == 0
    Message("","Route was added.")
else
    Message("",StrCat("Route Add Failed... Error # ",X))
endif


Article ID:   W15315