IpReleaseAddress and IPRenewAddress

Everything else that doesn't fall into one of the other PB categories.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

IpReleaseAddress and IPRenewAddress

Post by SFSxOI »

Has anyone ever gotten the API IpReleaseAddress and IPRenewAddress functions to work in PureBasic, and if so how did you do it?

http://msdn2.microsoft.com/en-us/library/aa366056.aspx
and
http://msdn2.microsoft.com/en-us/librar ... S.85).aspx
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

here :

Code: Select all

; ABBKlaus Mon May 17, 2004 20:16 
; http://msdn2.microsoft.com/en-us/library/aa365917.aspx 
; with the help of IPHlpAPI.inc from the Powerbasic include-file for the structures 
; Microsoft isn´t quite a help here :-) 
; modified by PSW Wed Sep 06, 2006 07:49 
; PB4.02 compatible since 29.05.2007 19:48 
; PB4.10 compatible on 23.12.2007 21:03 
; added more commands 31.12.2007 17:09

ImportC "msvcrt.lib" 
  asctime.l(a.l) 
  localtime.l(a.l) 
  strftime.l(a.l,b.l,c.p-ascii,d.l) 
EndImport 

Structure IP_ADDR_STRING 
  pNext.l 
  IpAddress.b[16] 
  IpMask.b[16] 
  Context.l 
EndStructure 

Structure TM 
  tm_sec.l 
  tm_min.l 
  tm_hour.l 
  tm_mday.l 
  tm_mon.l 
  tm_year.l 
  tm_wday.l 
  tm_yday.l 
  tm_isdst.l 
EndStructure 

#MAX_ADAPTER_NAME=128
#MAX_ADAPTER_NAME_LENGTH=256 
#MAX_ADAPTER_DESCRIPTION_LENGTH=128 
#MAX_ADAPTER_ADDRESS_LENGTH=8 

#MIB_IF_TYPE_OTHER     = 0 
#MIB_IF_TYPE_ETHERNET  = 1 
#MIB_IF_TYPE_TOKENRING = 2 
#MIB_IF_TYPE_FDDI      = 3 
#MIB_IF_TYPE_PPP       = 4 
#MIB_IF_TYPE_LOOPBACK  = 5 
#MIB_IF_TYPE_SLIP      = 6 

Structure IP_ADAPTER_INFO 
  pNext.l 
  ComboIndex.l 
  AdapterName.b[#MAX_ADAPTER_NAME_LENGTH+4] 
  Description.b[#MAX_ADAPTER_DESCRIPTION_LENGTH+4] 
  AddressLength.l 
  Address.b[#MAX_ADAPTER_ADDRESS_LENGTH] 
  Index.l 
  Type.l 
  DhcpEnabled.l 
  CurrentIpAddressPTR.l 
  IpAddressList.IP_ADDR_STRING 
  GatewayList.IP_ADDR_STRING 
  DhcpServer.IP_ADDR_STRING 
  HaveWins.l 
  PrimaryWinsServer.IP_ADDR_STRING 
  SecondaryWinsServer.IP_ADDR_STRING 
  LeaseObtained.l 
  LeaseExpires.l 
EndStructure 

Structure IP_ADAPTER_INDEX_MAP
  Index.l
  Name.w[#MAX_ADAPTER_NAME]
EndStructure

Structure IP_INTERFACE_INFO
  NumAdapters.l
  Adapter.IP_ADAPTER_INDEX_MAP[1]
EndStructure

Structure MyIP_ADAPTER_INFO 
  Index.l
  AdapterName.s 
  Description.s 
  MACAddress.s 
  IPAdress.s 
EndStructure 

Structure MyIP_INTERFACE_INFO
  Index.l
  Name.s
EndStructure

Procedure GetAdaptersInfo()
  Protected length.l=0,Result.l,*Buffer,*Buffer2,*ipinfo.IP_ADAPTER_INFO,*iplist.IP_ADDR_STRING
  Protected mac$,i.l,byte.b
  
  Result=GetAdaptersInfo_(0,@length) ; Get the length for Buffer 
  If Result=#ERROR_BUFFER_OVERFLOW And length 
    *Buffer=AllocateMemory(length) 
    If *Buffer And GetAdaptersInfo_(*Buffer,@length)=#ERROR_SUCCESS 
      *ipinfo.IP_ADAPTER_INFO=*Buffer 
      Global NewList MyIPAdapterList.MyIP_ADAPTER_INFO() ; declare list here 
      While *ipinfo 
        AddElement(MyIPAdapterList()) ; add one element 
        ;Debug "Index : "+Str(*ipinfo\Index) 
        MyIPAdapterList()\Index=*ipinfo\Index
        Select *ipinfo\Type 
          Case #MIB_IF_TYPE_OTHER 
            ;Debug "Type : #MIB_IF_TYPE_OTHER" 
          Case #MIB_IF_TYPE_ETHERNET 
            ;Debug "Type : #MIB_IF_TYPE_ETHERNET" 
          Case #MIB_IF_TYPE_TOKENRING 
            ;Debug "Type : #MIB_IF_TYPE_TOKENRING" 
          Case #MIB_IF_TYPE_FDDI 
            ;Debug "Type : #MIB_IF_TYPE_FDDI" 
          Case #MIB_IF_TYPE_PPP 
            ;Debug "Type : #MIB_IF_TYPE_PPP" 
          Case #MIB_IF_TYPE_LOOPBACK 
            ;Debug "Type : #MIB_IF_TYPE_LOOPBACK" 
          Case #MIB_IF_TYPE_SLIP 
            ;Debug "Type : #MIB_IF_TYPE_SLIP" 
          Default 
            ;Debug "Type : unknown" 
        EndSelect 
        ;Debug "Name : "+PeekS(@*ipinfo\AdapterName,-1,#PB_Ascii) 
        MyIPAdapterList()\AdapterName=PeekS(@*ipinfo\AdapterName,-1,#PB_Ascii) 
        MyIPAdapterList()\Description=PeekS(@*ipinfo\Description,-1,#PB_Ascii) 
        ;Debug "Desc : "+PeekS(@*ipinfo\Description,-1,#PB_Ascii) 
        ;IP-Adress 
        *iplist.IP_ADDR_STRING=*ipinfo\IpAddressList 
        While *iplist 
          ;Debug "IP-Adress : "+PeekS(@*iplist\IpAddress,-1,#PB_Ascii) 
          MyIPAdapterList()\IPAdress+PeekS(@*iplist\IpAddress,-1,#PB_Ascii)+Chr(13) 
          *iplist.IP_ADDR_STRING=*iplist\pNext 
        Wend 
        ;Gateway 
        *iplist.IP_ADDR_STRING=*ipinfo\GatewayList 
        While *iplist 
          ;Debug "Gateway : "+PeekS(@*iplist\IpAddress,-1,#PB_Ascii) 
          *iplist.IP_ADDR_STRING=*iplist\pNext 
        Wend 
        ;Wins 
        If *ipinfo\HaveWins 
          ;PrimaryWinsServer 
          *iplist.IP_ADDR_STRING=*ipinfo\PrimaryWinsServer 
          While *iplist 
            ;Debug "P-Wins : "+PeekS(@*iplist\IpAddress,-1,#PB_Ascii) 
            *iplist.IP_ADDR_STRING=*iplist\pNext 
          Wend 
          ;SecondaryWinsServer 
          *iplist.IP_ADDR_STRING=*ipinfo\SecondaryWinsServer 
          While *iplist 
            ;Debug "S-Wins : "+PeekS(@*iplist\IpAddress,-1,#PB_Ascii) 
            *iplist.IP_ADDR_STRING=*iplist\pNext 
          Wend 
        EndIf 
        ;DHCP 
        If *ipinfo\DhcpEnabled 
          ;DhcpServer 
          *iplist.IP_ADDR_STRING=*ipinfo\DhcpServer 
          While *iplist 
            ;Debug "DHCP Server Adress : " +PeekS(@*iplist\IpAddress,-1,#PB_Ascii) 
            *iplist.IP_ADDR_STRING=*iplist\pNext 
          Wend 
          ;LeaseObtained 
          *Buffer2=AllocateMemory(#MAXCHAR) 
          If *Buffer2 
            strftime(*Buffer2,#MAXCHAR,"%d.%m.%Y %H:%M:%S",localtime(@*ipinfo\LeaseObtained)) 
            ;Debug "Lease optained : "+PeekS(*Buffer2,-1,#PB_Ascii) 
            FreeMemory(*Buffer2) 
            *Buffer2=0
          EndIf 
          ;LeaseExpires 
          *Buffer2=AllocateMemory(#MAXCHAR) 
          If *Buffer2 
            strftime(*Buffer2,#MAXCHAR,"%d.%m.%Y %H:%M:%S",localtime(@*ipinfo\LeaseExpires)) 
            ;Debug "Lease expires : "+PeekS(*Buffer2,-1,#PB_Ascii) 
            FreeMemory(*Buffer2) 
            *Buffer2=0
          EndIf 
        Else 
          ;Debug "STATIC IP Configuration" 
        EndIf 
        ;MAC-Adress 
        If *ipinfo\AddressLength 
          mac$="" 
          For i=0 To *ipinfo\AddressLength-1 
            If i 
              mac$+":" 
            EndIf 
            byte.b=PeekB(@*ipinfo\Address+i) 
            If byte>=0 
              mac$+RSet(Hex(byte),2,"0") 
            Else 
              mac$+RSet(Hex(byte+256),2,"0") 
            EndIf 
          Next 
          ;Debug "MAC-Address "+mac$ 
          MyIPAdapterList()\MACAddress=mac$ 
        EndIf 
        *ipinfo.IP_ADAPTER_INFO=*ipinfo\pNext 
      Wend 
    EndIf
    If *Buffer 
      FreeMemory(*Buffer) 
      *Buffer=0
    EndIf 
  EndIf 
EndProcedure

Procedure GetInterfaceInfo()
  Protected *pInfo.IP_INTERFACE_INFO
  Protected ulOutBufLen=0
  Protected NumAdapters=0
  
  Global NewList MyIPInterfaceList.MyIP_INTERFACE_INFO()
  
  If GetInterfaceInfo_(0,@ulOutBufLen)=#ERROR_INSUFFICIENT_BUFFER
    *Buffer=AllocateMemory(ulOutBufLen)
    If *Buffer
      If GetInterfaceInfo_(*Buffer,@ulOutBufLen)=#NO_ERROR
        *pInfo=*Buffer
        If *pInfo\NumAdapters
          For i=0 To *pInfo\NumAdapters-1
            AddElement(MyIPInterfaceList())
            MyIPInterfaceList()\Index=*pInfo\Adapter[i]\Index
            MyIPInterfaceList()\Name=PeekS(@*pInfo\Adapter[i]\Name,#MAX_ADAPTER_NAME,#PB_Unicode)
            ;Debug *pInfo\Adapter[i]\Index
            ;Debug PeekS(@*pInfo\Adapter[i]\Name,#MAX_ADAPTER_NAME,#PB_Unicode)
          Next
        EndIf
      EndIf
      FreeMemory(*Buffer)
    EndIf
  EndIf
EndProcedure

Procedure IpReleaseAddress(Index,Adaptername$)
  Protected Adapter.IP_ADAPTER_INDEX_MAP,*Buffer
  Adapter\Index=Index
  *Buffer=AllocateMemory(Len(Adaptername$)*2+2)
  If *Buffer
    PokeS(*Buffer,Adaptername$,-1,#PB_Unicode) ; a zero will still be placed in memory after the poked string
    CopyMemory(*Buffer,@Adapter\Name,#MAX_ADAPTER_NAME) ; copy the buffer without the ending zero !
    FreeMemory(*Buffer)
    ProcedureReturn IpReleaseAddress_(@Adapter)
  EndIf
EndProcedure

Procedure IpRenewAddress(Index,Adaptername$)
  Protected Adapter.IP_ADAPTER_INDEX_MAP,*Buffer
  Adapter\Index=Index
  *Buffer=AllocateMemory(Len(Adaptername$)*2+2)
  If *Buffer
    PokeS(*Buffer,Adaptername$,-1,#PB_Unicode) ; a zero will still be placed in memory after the poked string
    CopyMemory(*Buffer,@Adapter\Name,#MAX_ADAPTER_NAME) ; copy the buffer without the ending zero !
    FreeMemory(*Buffer)
    ProcedureReturn IpRenewAddress_(@Adapter)
  EndIf
EndProcedure

GetAdaptersInfo()
ForEach MyIPAdapterList() 
  Debug "-------------------------------------------" 
  Debug MyIPAdapterList()\Index
  Debug MyIPAdapterList()\AdapterName 
  Debug MyIPAdapterList()\Description 
  Debug MyIPAdapterList()\IPAdress 
  Debug MyIPAdapterList()\MACAddress
Next 

GetInterfaceInfo()
ForEach MyIPInterfaceList()
  Debug "-------------------------------------------" 
  Debug MyIPInterfaceList()\Index
  Debug MyIPInterfaceList()\Name
  If IpReleaseAddress(MyIPInterfaceList()\Index,MyIPInterfaceList()\Name)<>#NO_ERROR
    Debug "IpReleaseAddress failed on adapter "+MyIPInterfaceList()\Name
  Else
    If IpRenewAddress(MyIPInterfaceList()\Index,MyIPInterfaceList()\Name)<>#NO_ERROR
      Debug "IpRenewAddress failed on adapter "+MyIPInterfaceList()\Name
    EndIf
  EndIf
Next
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

I was using basically the same as you...but it wasn't working. but......Ahhhh...now I see where I was going wrong, in this area:

Code: Select all

*Buffer=AllocateMemory(Len(Adaptername$)*2+2)
i wasn't doing anything like that, I was trying to do it something like this:

Code: Select all

*Buffer=AllocateMemory(Adaptername$)
Thanks ABBKlaus :)
Post Reply