Avatar billede ow.ob Nybegynder
20. januar 2003 - 07:24 Der er 1 løsning

Hvordan finder jeg subnet masken på en ipadresse via API

Jeg har brug for at finde Subnetmasken via et API kald, er det muligt?
Jeg har hørt noget om det er inetmib1.dll som leverer dataene til Winipcfg og ipconfig via en række udokumenterede kald, men er der nogen der ved hvo'n den fungerer ?
Avatar billede michaelemanuel Nybegynder
20. januar 2003 - 15:24 #1
Private Const MAX_ADAPTER_NAME_LENGTH        As Long = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH  As Long = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH      As Long = 8
Private Const ERROR_SUCCESS  As Long = 0

Private Type IP_ADDRESS_STRING
    IpAddr(0 To 15)  As Byte
End Type

Private Type IP_MASK_STRING
    IpMask(0 To 15)  As Byte
End Type

Private Type IP_ADDR_STRING
    dwNext    As Long
    IpAddress  As IP_ADDRESS_STRING
    IpMask    As IP_MASK_STRING
    dwContext  As Long
End Type

Private Type IP_ADAPTER_INFO
  dwNext                As Long
  ComboIndex            As Long  'reserved
  sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3))        As Byte '"{F25AA292-EC20-4BAD-BEE9-DAF72C9557FC}"
  sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte '"Intel(R) PRO/100 VE Network Connection #2 - Packet Scheduler Miniport"
  dwAddressLength      As Long                                  '6 - Length of the hardware address for the adapter
  Address(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1))          As Byte '00,03,47,2C,B8,58 - if MAC Address is "00-03-47-2C-B8-58"
  dwIndex              As Long                                  '2 - Adapter index
  dwType                As Long                                  '6 = MIB_IF_TYPE_ETHERNET - One of the MIB_IF_TYPE_xxx Constants
  bDhcpEnabled          As Long                                  '0=No, 1=Yes
  CurrentIpAddress      As Long                                  '0 - Reserved
  IpAddressList        As IP_ADDR_STRING                        'Contains IP : 10.1.0.10
  GatewayList          As IP_ADDR_STRING                        'Contains IP : 10.1.200.1
  DhcpServer            As IP_ADDR_STRING                        'Contains IP : <None>
  bHaveWins            As Long                                  '0=No, 1=Yes
  PrimaryWinsServer    As IP_ADDR_STRING                        'Contains IP : 10.1.100.1
  SecondaryWinsServer  As IP_ADDR_STRING                        'Contains IP : 0.0.0.0
  LeaseObtained        As Long                                  '0 - Time when Lease is obtained
  LeaseExpires          As Long                                  '0 - Time when Lease will expire
End Type

Public Enum TAdapterType
  MIB_IF_TYPE_OTHER = 1
  MIB_IF_TYPE_ETHERNET = 6
  MIB_IF_TYPE_TOKENRING = 9
  MIB_IF_TYPE_FDDI = 15
  MIB_IF_TYPE_PPP = 23
  MIB_IF_TYPE_LOOPBACK = 24
  MIB_IF_TYPE_SLIP = 28
End Enum

Public Type TAdapterInfo
  Description          As String
  MacAdress            As String
  Type                  As TAdapterType
  IpAddress            As String
  SubnetMask            As String
  DefaultGatewayIP      As String
  HaveWins              As Boolean
  PrimaryWinsServerIP  As String
  SecondaryWinsServerIP As String
  DhcpEnabled          As Boolean
  DhcpServerIP          As String
End Type

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
  (pTcpTable As Any, _
  pdwSize As Long) As Long
 
Private Declare Sub CopyMemory Lib "kernel32" _
  Alias "RtlMoveMemory" _
  (dst As Any, _
  src As Any, _
  ByVal bcount As Long)

Public Sub GetIP_MACAddress(AdapterInfo() As TAdapterInfo)
  Dim cbRequired  As Long
  Dim Buffer()    As Byte
  Dim Adapter    As IP_ADAPTER_INFO
  Dim AdapterStr  As IP_ADDR_STRING
  Dim pAdapter    As Long
  Dim idxAdapter  As Integer
 
  '--- Call GetAdaptersInfo to recieve required buffer size ---
  Call GetAdaptersInfo(ByVal 0&, cbRequired)
  If cbRequired = 0 Then Exit Sub
 
  '--- Allocate buffer of required size, and call GetAdaptersInfo ---
  ReDim Buffer(0 To cbRequired - 1) As Byte
  If GetAdaptersInfo(Buffer(0), cbRequired) = ERROR_SUCCESS Then
    '--- Get a pointer to the first Adapter ---
    pAdapter = VarPtr(Buffer(0))
    Do
      '--- Copy the data into Adapter ---
      CopyMemory Adapter, ByVal pAdapter, LenB(Adapter)
      Dim sItem As String
      '--- Convert sDescription into String ---
      sItem = StrConv(Adapter.sDescription, vbUnicode)
      AdapterInfo(idxAdapter).Description = Left$(sItem, InStr(sItem, Chr$(0)) - 1)
      '--- Convert MacAddress into String ---
      AdapterInfo(idxAdapter).MacAdress = _
        Right("00" & Hex(Adapter.Address(0)), 2) & "-" & _
        Right("00" & Hex(Adapter.Address(1)), 2) & "-" & _
        Right("00" & Hex(Adapter.Address(2)), 2) & "-" & _
        Right("00" & Hex(Adapter.Address(3)), 2) & "-" & _
        Right("00" & Hex(Adapter.Address(4)), 2) & "-" & _
        Right("00" & Hex(Adapter.Address(5)), 2)
      '--- Read Type ---
      AdapterInfo(idxAdapter).Type = Adapter.dwType
      '--- Convert IpAddress into String ---
      sItem = StrConv(Adapter.IpAddressList.IpAddress.IpAddr, vbUnicode)
      AdapterInfo(idxAdapter).IpAddress = Left$(sItem, InStr(sItem, Chr$(0)) - 1)
      '--- Convert SubnetMask into String ---
      sItem = StrConv(Adapter.IpAddressList.IpMask.IpMask, vbUnicode)
      AdapterInfo(idxAdapter).SubnetMask = Left$(sItem, InStr(sItem, Chr$(0)) - 1)
      '--- Convert DefaultGatewayIP into String ---
      sItem = StrConv(Adapter.GatewayList.IpAddress.IpAddr, vbUnicode)
      AdapterInfo(idxAdapter).DefaultGatewayIP = Left$(sItem, InStr(sItem, Chr$(0)) - 1)
      '--- Convert HaveWins into Boolean ---
      AdapterInfo(idxAdapter).HaveWins = (Adapter.bHaveWins <> 0)
      '--- Convert PrimaryWinsServerIP into String ---
      sItem = StrConv(Adapter.PrimaryWinsServer.IpAddress.IpAddr, vbUnicode)
      AdapterInfo(idxAdapter).PrimaryWinsServerIP = Left$(sItem, InStr(sItem, Chr$(0)) - 1)
      '--- Convert SecondaryWinsServerIP into String ---
      sItem = StrConv(Adapter.SecondaryWinsServer.IpAddress.IpAddr, vbUnicode)
      AdapterInfo(idxAdapter).SecondaryWinsServerIP = Left$(sItem, InStr(sItem, Chr$(0)) - 1)
      '--- Convert DhcpEnabled into Boolean ---
      AdapterInfo(idxAdapter).DhcpEnabled = (Adapter.bDhcpEnabled <> 0)
      '--- Convert DefaultGatewayIP into String ---
      sItem = StrConv(Adapter.DhcpServer.IpAddress.IpAddr, vbUnicode)
      AdapterInfo(idxAdapter).DhcpServerIP = Left$(sItem, InStr(sItem, Chr$(0)) - 1)
      '--- Get next Adapter ---
      pAdapter = Adapter.dwNext
      idxAdapter = idxAdapter + 1
      If UBound(AdapterInfo) < idxAdapter Then Exit Do
    Loop While (pAdapter <> 0)
  End If
End Sub
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester