Announcement

Collapse
No announcement yet.

How to make IP static programatically?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dave Biggs
    replied
    Some info here on netsh usage: http://www.petri.co.il/configure_tcp_ip_from_cmd.htm

    Some test code using it via Shell as Shawn suggested..
    Code:
    #DIM ALL
    #Compile exe
    #Include "WIN32API.INC"
     
    Function PBMain()
     STATIC hConsole AS LONG, cWritten AS LONG
     LOCAL sOut, CommandLine AS STRING 
      AllocConsole
      hConsole = GetStdHandle(%STD_OUTPUT_HANDLE)
     
        SetConsoleTitle "Netsh IP Test" 
     
    ' Read current IP settings -
        sOut = "Getting Current IP settings:" + $CRLF 
        WriteFile hConsole, BYVAL  STRPTR(sOut), LEN(sOut), cWritten, BYVAL 0&
     
        CommandLine = "netsh interface ip show config"
        Shell (Environ$("comspec") & " /C " & CommandLine, 0)
     
    ' Set Static IP address -
        sOut = "Set Static IP (can take a few seconds!):" + $CRLF
        WriteFile hConsole, BYVAL  STRPTR(sOut), LEN(sOut), cWritten, BYVAL 0&
     
        CommandLine = "netsh interface ip set address "+$Dq+"Local Area Connection"+$Dq+ _
                      " static 192.168.0.100 255.255.255.0 192.168.0.1 1"
        Shell (Environ$("comspec") & " /C " & CommandLine, 0)
     
    ' Confirm Settings -
        sOut = "New IP settings:" + $CRLF 
        WriteFile hConsole, BYVAL  STRPTR(sOut), LEN(sOut), cWritten, BYVAL 0&
     
        CommandLine = "netsh interface ip show config"
        Shell (Environ$("comspec") & " /C " & CommandLine, 0)
     
     
    ' Change to DHCP -
        sOut = "Change to DHCP:" + $CRLF 
        WriteFile hConsole, BYVAL  STRPTR(sOut), LEN(sOut), cWritten, BYVAL 0&
     
        CommandLine = "netsh interface ip set address "+$Dq+"Local Area Connection"+$Dq+" dhcp"
        Shell (Environ$("comspec") & " /C " & CommandLine, 0)
     
    ' Confirm Settings -
        sOut = "Wait 10 seconds for New IP settings:" + $CRLF      ' may be longer??
        WriteFile hConsole, BYVAL  STRPTR(sOut), LEN(sOut), cWritten, BYVAL 0&
      Sleep 10000
        CommandLine = "netsh interface ip show config"
        Shell (Environ$("comspec") & " /K " & CommandLine, 0)
     
      'MsgBox "Close"  'instead of '/K' in last Shell command
     
      FreeConsole
     
    End Function 
    '------------------/
    And here's some for AddIPAddress that I played with a while back..
    Code:
    #Dim All
    #Compile Exe
    #INCLUDE "WIN32API.INC"
    #Include "IPHLPAPI.INC"
     
    DECLARE FUNCTION inet_addr LIB "wsock32.dll" ALIAS "inet_addr" (cp AS ASCIIZ) AS DWORD        ' in WSock32.inc
    ' Internet Protocol dotted address to proper address for the IN_ADDR structure
    DECLARE FUNCTION inet_ntoa LIB "wsock32.dll" ALIAS "inet_ntoa" (byval inn AS DWORD) AS DWORD  ' returns ASCIIZ PTR
     
    Function ErrorMsg (ByVal ErrorCode As Dword) As String
     Local szMsg As Asciiz * 255
      FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0, ErrorCode, 0, szMsg, SIZEOF(szMsg), ByVal 0
     Function = Str$(ErrorCode) +" "+ szMsg
    End Function 
    '------------------/ErrorMsg
     
    Function PbMain()
     Local i, Res   As Long
     Local iSize    As Long
     Local pIPADD   As MIB_IPADDRTABLE Ptr        ' contains table of MIB_IPADDRROW structures
     Local sBuffer, Msg  As String
     Local IpAddr, IpMask, IfIndex As Dword 
     Local Address  As Dword
     Static NTEContext, NTEInstance As Dword 
     Static pszIP, pszMask As Asciiz Ptr
     
       'First call gets the required size for the table, ie the iSize result is set
       If GetIpAddrTable (ByVal 0, iSize, 0) = %ERROR_INSUFFICIENT_BUFFER Then
          sBuffer = Space$(iSize)
          'Second call gets tha actual data - current IP info.
          If GetIpAddrTable (ByVal StrPtr(sBuffer), iSize, 0) = %NO_ERROR Then
            pIPADD = StrPtr(sBuffer)
            'Pointer to a buffer that receives the interface–to–IP address mapping table as a MIB_IPADDRTABLE structure. 
            For i = 0 To @pIPADD.dwNumEntries - 1 ' loop through number of IP address entries in the table
              IpAddr = @pIPADD.table(i).dwAddr
              IpMask = @pIPADD.table(i).dwMask
              IfIndex = @pIPADD.table(i).dwIndex
              pszIP = inet_ntoa (IpAddr)          : Msg =       "ADDRESS : " + @pszIP + $CRLF
              pszMask = inet_ntoa (IpMask)        : Msg = Msg + "SUBNET  : " + @pszMask + $CRLF
              MsgBox Msg ,,"TCP/IP Interface INDEX " + Str$(IfIndex)
            Next
     
            '// Add new IP
            Address = inet_addr("192.168.0.27")
            IPMask  = inet_addr("255.255.255.0")
            IfIndex = @pIPADD.table(0).dwIndex    ' Primary Interface?
            MsgBox "Entry; "+Str$(IfIndex)
            ' Variables where handles to the added IP will be returned
            NTEContext = 0 : NTEInstance = 0
     
            Res = AddIPAddress(Address, IPMask, IfIndex, NTEContext, NTEInstance)
            If Res = %NO_ERROR Then 
              MsgBox "IP address added."
            Else 
              MsgBox ErrorMsg (Res), ,"Error Adding IP"
            End If 
     
            '// Delete the IP we just added using the NTEContext created by AddIpAddress
            Res = DeleteIPAddress(NTEContext)
            If Res = %NO_ERROR Then
              MsgBox "Address Deleted."
            Else 
              MsgBox ErrorMsg (Res), ,"Error Deleting IP"
            End If
     
          Else
            MsgBox "Error retrieving IP address table.",,"Error"
          End If
       Else
          MsgBox "Error getting the size needed to retrieve the IP address table.",,"Error"
       End If
    End Function 
    '------------------/

    Leave a comment:


  • Knuth Konrad
    replied
    and need a shutdown after ...
    No, at least not on workstations.

    Servers might need a restart, because certain services (AD, DNS, DHCP etc.) are bound to the old IP.

    Leave a comment:


  • Hubert Brandel
    replied
    Hi,

    and last but not least, in Win2000ff you need to have admin rights to change it and need a shutdown after ...

    Leave a comment:


  • Rod Macia
    replied
    Maybe this can be of help

    The Win32_NetworkAdapterConfiguration WMI class represents the attributes and behaviors of a network adapter. This class includes extra properties and methods that support the management of the TCP/IP and Internetwork Packet Exchange (IPX) protocols that are independent from the network adapter.

    http://msdn.microsoft.com/en-us/library/aa394217.aspx


    Rod

    Leave a comment:


  • Shawn Anderson
    replied
    You can use NETSH commands from a command line. I know that isn't exactly a programmatic solution, but you could shell out to run it.

    Leave a comment:


  • Ronald Robinson
    replied
    Elias,
    You may want to look at Internet Protocol Helper (IP Helper) in the MS SDK

    The AddIPAddress function adds the specified IP address to the specified adapter.

    Leave a comment:


  • Chris Holbrook
    replied
    Elias, you need to get around or disable your DHCP server - here is a link: http://corz.org/comms/hardware/route...ip.address.php

    Leave a comment:


  • Elias Montoya
    replied
    Im sorry Chris, i now think i wasnt clear enough.

    Yes, I was talking about Local network IP.

    And i still dont know how.

    Thanx.

    Leave a comment:


  • Chris Holbrook
    replied
    Originally posted by Elias Montoya View Post
    I know how to do it manually in XP, but i was wondering if i could automate the process from within my app. Any ideas?
    I thought that your ISP did that for you. You can spoof it using DynDNS, for example. Or do you mean local network IP address?

    Leave a comment:


  • Elias Montoya
    started a topic How to make IP static programatically?

    How to make IP static programatically?

    I know how to do it manually in XP, but i was wondering if i could automate the process from within my app. Any ideas?

    Ill keep looking meanwhile.

    Thanx in advance.
Working...
X