Api getnetworkparams

tof72 Messages postés 33 Date d'inscription mardi 8 juin 2004 Statut Membre Dernière intervention 18 juillet 2005 - 19 oct. 2004 à 12:32
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 - 19 oct. 2004 à 18:06
Bonjour à tous

Une personne pourrait m'expliquer comment utiliser l'api getnetworkparams pour pouvoir récuperer le masque de sous réseau de mon poste.

Merci

1 réponse

Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
19 oct. 2004 à 18:06
1. Démarrez un nouveau projet EXE standard dans Visual Basic. Form1 est créé par défaut.
2. Dans le menu Projet, cliquez sur Supprimer Form1.
3. Dans le menu Projet, cliquez sur Ajouter un module. Module1 est créé par défaut.
4. Collez le code suivant dans la section Déclarations générales de Module1 :

Public Const MAX_HOSTNAME_LEN = 132
Public Const MAX_DOMAIN_NAME_LEN = 132
Public Const MAX_SCOPE_ID_LEN = 260
Public Const MAX_ADAPTER_NAME_LENGTH = 260
Public Const MAX_ADAPTER_ADDRESS_LENGTH = 8
Public Const MAX_ADAPTER_DESCRIPTION_LENGTH = 132
Public Const ERROR_BUFFER_OVERFLOW = 111
Public Const MIB_IF_TYPE_ETHERNET = 6
Public Const MIB_IF_TYPE_TOKENRING = 9
Public Const MIB_IF_TYPE_FDDI = 15
Public Const MIB_IF_TYPE_PPP = 23
Public Const MIB_IF_TYPE_LOOPBACK = 24
Public Const MIB_IF_TYPE_SLIP = 28

Type IP_ADDR_STRING
            Next As Long
            IpAddress As String * 16
            IpMask As String * 16
            Context As Long
End Type

Type IP_ADAPTER_INFO
            Next As Long
            ComboIndex As Long
            AdapterName As String * MAX_ADAPTER_NAME_LENGTH
            Description As String * MAX_ADAPTER_DESCRIPTION_LENGTH
            AddressLength As Long
            Address(MAX_ADAPTER_ADDRESS_LENGTH - 1) As Byte
            Index As Long
            Type As Long
            DhcpEnabled As Long
            CurrentIpAddress As Long
            IpAddressList As IP_ADDR_STRING
            GatewayList As IP_ADDR_STRING
            DhcpServer As IP_ADDR_STRING
            HaveWins As Byte
            PrimaryWinsServer As IP_ADDR_STRING
            SecondaryWinsServer As IP_ADDR_STRING
            LeaseObtained As Long
            LeaseExpires As Long
End Type

Type FIXED_INFO
            HostName As String * MAX_HOSTNAME_LEN
            DomainName As String * MAX_DOMAIN_NAME_LEN
            CurrentDnsServer As Long
            DnsServerList As IP_ADDR_STRING
            NodeType As Long
            ScopeId  As String * MAX_SCOPE_ID_LEN
            EnableRouting As Long
            EnableProxy As Long
            EnableDns As Long
End Type

Public Declare Function GetNetworkParams Lib "IPHlpApi.dll" _
       (FixedInfo As Any, pOutBufLen As Long) As Long
Public Declare Function GetAdaptersInfo Lib "IPHlpApi.dll" _
       (IpAdapterInfo As Any, pOutBufLen As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
       (Destination As Any, Source As Any, ByVal Length As Long)

Sub main()
    Dim error As Long
    Dim FixedInfoSize As Long
    Dim AdapterInfoSize As Long
    Dim i As Integer
    Dim PhysicalAddress  As String
    Dim NewTime As Date
    Dim AdapterInfo As IP_ADAPTER_INFO
    Dim AddrStr As IP_ADDR_STRING
    Dim FixedInfo As FIXED_INFO
    Dim Buffer As IP_ADDR_STRING
    Dim pAddrStr As Long
    Dim pAdapt As Long
    Dim Buffer2 As IP_ADAPTER_INFO
    Dim FixedInfoBuffer() As Byte
    Dim AdapterInfoBuffer() As Byte

    ' Obtenir les principales informations de configuration IP de cet ordinateur
    ' à l'aide d'une structure FIXED_INFO.
    FixedInfoSize = 0
    error = GetNetworkParams(ByVal 0&, FixedInfoSize)
    If error <> 0 Then
        If error <> ERROR_BUFFER_OVERFLOW Then
           MsgBox "Échec du dimensionnement de GetNetworkParams avec l'erreur " & error
           Exit Sub
        End If
    End If
    ReDim FixedInfoBuffer(FixedInfoSize - 1)

    error = GetNetworkParams(FixedInfoBuffer(0), FixedInfoSize)
    If error = 0 Then
            CopyMemory FixedInfo, FixedInfoBuffer(0), FixedInfoSize
            MsgBox "Nom de l'hôte :  " & FixedInfo.HostName
            MsgBox "Serveurs DNS :  " & FixedInfo.DnsServerList.IpAddress
            pAddrStr = FixedInfo.DnsServerList.Next
            Do While pAddrStr <> 0
                  CopyMemory Buffer, ByVal pAddrStr, LenB(Buffer)
                  MsgBox "Serveurs DNS :  " & Buffer.IpAddress
                  pAddrStr = Buffer.Next
            Loop

            Select Case FixedInfo.NodeType
                       Case 1
                                  MsgBox "Type de noeud : Diffusion"
                       Case 2
                                  MsgBox "Type de noeud : Homologue à homologue"
                       Case 4
                                  MsgBox "Type de noeud : Mixte"
                       Case 8
                                  MsgBox "Type de noeud : Hybride"
                       Case Else
                                  MsgBox "Type de noeud inconnu"
            End Select

            MsgBox "Identificateur d'étendue NetBIOS :  " & FixedInfo.ScopeId
            If FixedInfo.EnableRouting Then
                       MsgBox "Routage IP activé "
            Else
                       MsgBox "Routage IP désactivé"
            End If
            If FixedInfo.EnableProxy Then
                       MsgBox "Proxy WINS activé "
            Else
                       MsgBox "Proxy WINS désactivé "
            End If
            If FixedInfo.EnableDns Then
                      MsgBox "La résolution NetBIOS utilise DNS "
            Else
                      MsgBox "La résolution NetBIOS n'utilise pas DNS  "
            End If

    Else
            MsgBox "Échec de GetNetworkParams avec l'erreur " & error
            Exit Sub
    End If

    ' Énumérer toutes les informations spécifiques à la carte à l'aide de la structure
    ' IP_ADAPTER_INFO.
    ' Remarque :  IP_ADAPTER_INFO contient une liste liée d'entrées relatives à la carte.

    AdapterInfoSize = 0
    error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)
    If error <> 0 Then
        If error <> ERROR_BUFFER_OVERFLOW Then
           MsgBox "Échec du dimensionnement de GetAdaptersInfo avec l'erreur " & error
           Exit Sub
        End If
    End If

    ReDim AdapterInfoBuffer(AdapterInfoSize - 1)
    ' Obtenir les informations relatives à la carte effectivement installée
    error = GetAdaptersInfo(AdapterInfoBuffer(0), AdapterInfoSize)
    If error <> 0 Then
       MsgBox "Échec de GetAdaptersInfo avec l'erreur " & error
       Exit Sub
    End If

    ' Allouer de la mémoire
     CopyMemory AdapterInfo, AdapterInfoBuffer(0), AdapterInfoSize
    pAdapt = AdapterInfo.Next
    Do
     CopyMemory Buffer2, AdapterInfo, AdapterInfoSize
       Select Case Buffer2.Type
              Case MIB_IF_TYPE_ETHERNET
                   MsgBox "Nom de la carte : Carte Ethernet "
              Case MIB_IF_TYPE_TOKENRING
                   MsgBox "Nom de la carte : Carte Token Ring "
              Case MIB_IF_TYPE_FDDI
                   MsgBox "Nom de la carte : Carte FDDI "
              Case MIB_IF_TYPE_PPP
                   MsgBox "Nom de la carte : Carte PPP "
              Case MIB_IF_TYPE_LOOPBACK
                   MsgBox "Nom de la carte : Carte de bouclage "
              Case MIB_IF_TYPE_SLIP
                   MsgBox "Nom de la carte : Carte SLIP "
              Case Else
                   MsgBox "Nom de la carte : Autre carte "
       End Select
       MsgBox "Description de la carte : " & Buffer2.Description
       PhysicalAddress = ""
       For i = 0 To Buffer2.AddressLength - 1
           PhysicalAddress = PhysicalAddress & Hex(Buffer2.Address(i))
           If i < Buffer2.AddressLength - 1 Then
              PhysicalAddress = PhysicalAddress & "-"
           End If
       Next
       MsgBox "Adresse physique : " & PhysicalAddress

       If Buffer2.DhcpEnabled Then
          MsgBox "DHCP activé "
       Else
          MsgBox "DHCP désactivé "
       End If
       MsgBox "Adresse IP : " & Buffer2.IpAddressList.IpAddress
       MsgBox "Masque de sous-réseau : " & Buffer2.IpAddressList.IpMask

       pAddrStr = Buffer2.IpAddressList.Next
       Do While pAddrStr <> 0
          CopyMemory Buffer, Buffer2.IpAddressList, LenB(Buffer)
          MsgBox "Adresse IP : " & Buffer.IpAddress
          MsgBox "Masque de sous-réseau : " & Buffer.IpMask
          pAddrStr = Buffer.Next
          If pAddrStr <> 0 Then
             CopyMemory Buffer2.IpAddressList, ByVal pAddrStr, _
                        LenB(Buffer2.IpAddressList)
          End If
       Loop

       MsgBox "Passerelle par défaut : " & Buffer2.GatewayList.IpAddress
       pAddrStr = Buffer2.GatewayList.Next
       Do While pAddrStr <> 0
          CopyMemory Buffer, Buffer2.GatewayList, LenB(Buffer)
          MsgBox "Adresse IP : " & Buffer.IpAddress
          pAddrStr = Buffer.Next
          If pAddrStr <> 0 Then
             CopyMemory Buffer2.GatewayList, ByVal pAddrStr, _
                        LenB(Buffer2.GatewayList)
          End If
       Loop

       MsgBox "Serveur DHCP : " & Buffer2.DhcpServer.IpAddress
       MsgBox "Serveur WINS principal : " & _
              Buffer2.PrimaryWinsServer.IpAddress
       MsgBox "Serveur WINS secondaire : " & _
              Buffer2.SecondaryWinsServer.IpAddress
       ' Afficher l'heure.
       NewTime = DateAdd("s", Buffer2.LeaseObtained, #1/1/1970#)
       MsgBox "Bail obtenu : " & _
              CStr(Format(NewTime, "dddd, mmm d hh:mm:ss yyyy"))

       NewTime = DateAdd("s", Buffer2.LeaseExpires, #1/1/1970#)
       MsgBox "Expiration du bail :  " & _
              CStr(Format(NewTime, "dddd, mmm d hh:mm:ss yyyy"))
       pAdapt = Buffer2.Next
       If pAdapt <> 0 Then
           CopyMemory AdapterInfo, ByVal pAdapt, AdapterInfoSize
        End If
      Loop Until pAdapt = 0
      End Sub


5. Appuyez sur la touche F5 pour exécuter le projet, cliquez sur OK à chaque fois qu'un message s'affiche, puis notez les résultats.

Lors de l'exécution de cet exemple de code, tel qu'il est compilé, EXE renvoie le message d'erreur suivant à la fin de la procédure :

Erreur d'exécution 10 : Ce tableau est fixe ou temporairement verrouillé.

L'exécution de ce code dans IDE génère une erreur de page non valide à la fin de l'exécution de VB6.EXE.

références: support.microsoft.com article 223025
0
Rejoignez-nous