11

(最初に開いたインターフェイスの) マシンの IP アドレスを取得する最も面倒でない (モジュールの組み込み、コードの長さなど) 方法は何ですか? MSINET を使用したソリューションをいくつか知っていますが、もっとうまくやれると信じています。返信しないでください

Function HomeIP() as Atring
HomeIP= "127.0.0.1"
End Function

それはそれほど面白くないからです...または正しくないからです。シナリオは、返信を作成しようとしているドキュメント ID 機能に関する質問です。

4

7 に答える 7

24

Technetからの適応例を次に示します。

Function GetIPAddress()
    Const strComputer As String = "."   ' Computer name. Dot means local computer
    Dim objWMIService, IPConfigSet, IPConfig, IPAddress, i
    Dim strIPAddress As String

    ' Connect to the WMI service
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    ' Get all TCP/IP-enabled network adapters
    Set IPConfigSet = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

    ' Get all IP addresses associated with these adapters
    For Each IPConfig In IPConfigSet
        IPAddress = IPConfig.IPAddress
        If Not IsNull(IPAddress) Then
            strIPAddress = strIPAddress & Join(IPAddress, ", ")
        End If
    Next

    GetIPAddress = strIPAddress
End Function

プロジェクトの参照に Microsoft WMI Scripting Library が含まれている必要があります。

于 2009-06-04T11:34:38.573 に答える
2

私が見つけたいくつかの例: -

http://www.everythingaccess.com/tutorials.asp?ID=Get-all-IP-Addresses-of-your-machine

http://puremis.net/excel/code/079.shtml

編集

これは、最初のリンクのコードをわずかに変更したものです

Option Explicit

' VBA MODULE: Get all IP Addresses of your machine
' (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
' Written 18/05/2005
'
' REQUIREMENTS: Windows 98 or above, Access 97 and above
'
' Please read the full tutorial here:
' http://www.everythingaccess.com/tutorials.asp?ID=Get-all-IP-Addresses-of-your-machine
'
' Please leave the copyright notices in place.
' Thank you.
'
'Option Compare Database

'A couple of API functions we need in order to query the IP addresses in this machine
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetIpAddrTable Lib "Iphlpapi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long

'The structures returned by the API call GetIpAddrTable...
Type IPINFO
    dwAddr As Long         ' IP address
    dwIndex As Long         ' interface index
    dwMask As Long         ' subnet mask
    dwBCastAddr As Long     ' broadcast address
    dwReasmSize As Long    ' assembly size
    Reserved1 As Integer
    Reserved2 As Integer
End Type

Public Function ConvertIPAddressToString(longAddr As Long) As String

    Dim IPBytes(3) As Byte
    Dim lngCount As Long

    'Converts a long IP Address to a string formatted 255.255.255.255
    'Note: Could use inet_ntoa instead

    CopyMemory IPBytes(0), longAddr, 4 ' IP Address is stored in four bytes (255.255.255.255)

    'Convert the 4 byte values to a formatted string
    While lngCount < 4

        ConvertIPAddressToString = ConvertIPAddressToString + _
                                    CStr(IPBytes(lngCount)) + _
                                    IIf(lngCount < 3, ".", "")

        lngCount = lngCount + 1

    Wend

End Function

Public Function GetFirstNonLocalIPAddress()

    Dim Ret As Long, Tel As Long
    Dim bytBuffer() As Byte
    Dim IPTableRow As IPINFO
    Dim lngCount As Long
    Dim lngBufferRequired As Long
    Dim lngStructSize As Long
    Dim lngNumIPAddresses As Long
    Dim strIPAddress As String

On Error GoTo ErrorHandler:

    Call GetIpAddrTable(ByVal 0&, lngBufferRequired, 1)

    If lngBufferRequired > 0 Then

        ReDim bytBuffer(0 To lngBufferRequired - 1) As Byte

        If GetIpAddrTable(bytBuffer(0), lngBufferRequired, 1) = 0 Then

            'We've successfully obtained the IP Address details...

            'How big is each structure row?...
            lngStructSize = LenB(IPTableRow)

            'First 4 bytes is a long indicating the number of entries in the table
            CopyMemory lngNumIPAddresses, bytBuffer(0), 4

            While lngCount < lngNumIPAddresses

                'bytBuffer contains the IPINFO structures (after initial 4 byte long)
                CopyMemory IPTableRow, _
                            bytBuffer(4 + (lngCount * lngStructSize)), _
                            lngStructSize

                strIPAddress = ConvertIPAddressToString(IPTableRow.dwAddr)

                If Not ((strIPAddress = "127.0.0.1")) Then

                    GetFirstNonLocalIPAddress = strIPAddress
                    Exit Function

                End If

                lngCount = lngCount + 1

            Wend

        End If

    End If

Exit Function

ErrorHandler:
    MsgBox "An error has occured in GetIPAddresses():" & vbCrLf & vbCrLf & _
            Err.Description & " (" & CStr(Err.Number) & ")"

End Function
于 2009-05-06T08:18:11.603 に答える
1

シェルコマンドipconfigを実行して、返された結果を解析できますか?

于 2009-05-06T08:12:33.147 に答える
1

ipconfig を使用する別の簡単な方法があります。 http://www.vbaexpress.com/kb/getarticle.php?kb_id=537

于 2009-05-06T15:49:52.703 に答える
0
Option Explicit
Sub Main()

Dim wsh As Object
Dim strIPOutputFile As String, strSingleLine As String, strIP As String, strToFind As String
Dim intSourceFile As Integer, intLocation As Integer

    Set wsh = CreateObject("WScript.Shell")

    strIPOutputFile = "C:\Users\MeMeMeMe\Desktop\txtfile.txt"

'Save ipconfig info to file
    wsh.Run "%comspec% /c ipconfig/all> """ & strIPOutputFile & """

'Close any open text files
   Close

'Get the number of the next free text file
   intSourceFile = FreeFile

   Open strIPOutputFile For Input As intSourceFile

    strToFind = "IPv4 Address. . . . . . . . . . . :" 'This will probably depend on your file
   Do Until EOF(intSourceFile)
        Input #intSourceFile, strSingleLine
      If InStr(1, strSingleLine, strToFind) > 0 Then
        Exit Do
      End If
    Loop

    intLocation = Len(strToFind)
   strIP = Trim(Mid(strSingleLine,1 + intLocation,Len(strSingleLine) - intLocation))

    intLocation = Len(strIP)
    While Not IsNumeric(Mid(strIP,intLocation,1))
       strIP = Left(strIP, Len(strIP) - 1)
       intLocation = Len(strIP)
    Wend

    Close

   MsgBox strIP

End Sub
于 2017-01-09T16:02:58.053 に答える
0

nbtstat -nとにかくXPで仕事をするかもしれません。他の Windows バージョンや他の言語でのローカライズについては不明です。部分的な出力例:

C:\Documents and Settings\colin>nbtstat -n

ローカル エリア接続: ノード IP アドレス: [192.168.1.100] スコープ ID: []

            NetBIOS Local Name Table

于 2009-05-07T22:45:31.357 に答える
0

Codeproject には、.net でこれを行う方法に関する優れた記事があります: http://www.codeproject.com/KB/cs/network.aspx

これからいつでもコンソール実行可能ファイルを作成し、VBA から呼び出すことができます。

RO

于 2009-05-06T08:31:19.420 に答える