这个例子是获取本机器的ip和机器名的例子,对你可能有些参考。Option ExplicitPrivate Sub cmdGetMachineID_Click()   
   Text1 = GetIPHostName()     
   Text2 = GetIPAddress()
End Sub
''''''modal 
Option Explicit
Public Const MAX_WSADescription = 256
Public Const MAX_WSASYSStatus = 128
Public Const ERROR_SUCCESS As Long = 0
Public Const WS_VERSION_REQD As Long = &H101
Public Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD \ &H100 And &HFF&
Public Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
Public Const MIN_SOCKETS_REQD As Long = 1
Public Const SOCKET_ERROR As Long = -1Public Type HOSTENT
   hName As Long
   hAliases As Long
hAddrType As Integer
hLen As Integer
hAddrList As Long
End TypePublic Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End TypePublic Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Public Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Public Declare Function gethostname Lib "WSOCK32.DLL" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
Public Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal szHost As String) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)Public Function GetIPAddress() As String
Dim sHostName As String * 256
Dim lpHost As Long
    Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim i As Integer
Dim sIPAddr As String
   
If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If
'Gethostname函数将本地主机的Name属性返回到内存中,主机名的
'类型取决于接口:可以是简单的主机名,也可以是完整的域名。
'在实际的应用中,如果没有可用的主机名,Gethostname函数将会返回
'一个能解析的地址。If gethostname(sHostName, 256) = SOCKET_ERROR Then
GetIPAddress = ""
MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & " has occurred. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
'Gethostname函数返回Hostent结构的地址.这个结构包括对主机名。
'参数的搜索是否成功的结果.程序不能试图清空这个结构,就是说只能
'有一个实例在程序中运行.所以应用程序必须在其他程序调用到这个
'结构之前将所有的信息得到.Gethostname函数不能解决IP地址的问题。sHostName = Trim$(sHostName)
lpHost = gethostbyname(sHostName)
If lpHost = 0 Then
GetIPAddress = ""
MsgBox "Windows Sockets are not responding. " & "Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If'要得到IP地址,我们必须得到主机和它成员的结构.
CopyMemory HOST, lpHost, Len(HOST)
CopyMemory dwIPAddr, HOST.hAddrList, 4
'建立接收数组ReDim tmpIPAddr(1 To HOST.hLen)
CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
For i = 1 To HOST.hLen
sIPAddr = sIPAddr & tmpIPAddr(i) & "."
Next
GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
SocketsCleanup
End FunctionPublic Function GetIPHostName() As String
Dim sHostName As String * 256
If Not SocketsInitialize() Then
GetIPHostName = ""
Exit Function
End If
If gethostname(sHostName, 256) = SOCKET_ERROR Then
GetIPHostName = ""
MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & " has occurred.  Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End IfGetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
SocketsCleanup
End FunctionPublic Function HiByte(ByVal wParam As Integer) As Byte
HiByte = (wParam And &HFF00&) \ (&H100)
End FunctionPublic Function LoByte(ByVal wParam As Integer) As Byte
LoByte = wParam And &HFF&
End FunctionPublic Sub SocketsCleanup()
If WSACleanup() <> ERROR_SUCCESS Then
MsgBox "Socket error occurred in Cleanup."
End If
End SubPublic Function SocketsInitialize() As Boolean
Dim WSAD As WSADATA
Dim sLoByte As String
Dim sHiByte As String
If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
MsgBox "The 32-bit Windows Socket is not responding."
SocketsInitialize = False
Exit Function
End IfIf WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
MsgBox "This application requires a minimum of " & CStr(MIN_SOCKETS_REQD) & " supported sockets."
SocketsInitialize = False
Exit Function
End IfIf LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And HiByte(WSAD.wVersion) < WS_VERSION_MINOR Then
sHiByte = CStr(HiByte(WSAD.wVersion))
sLoByte = CStr(LoByte(WSAD.wVersion))
MsgBox "Sockets version " & sLoByte & "." & sHiByte & " is not supported by 32-bit Windows Sockets."
SocketsInitialize = False
Exit Function
End If
SocketsInitialize = True
End Function

解决方案 »

  1.   

    太复杂了吧?
    以下可以得到本机计算机名Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Declare Function SetComputerName Lib "kernel32" Alias "SetComputerNameA" (ByVal lpComputerName As String) As LongPublic Function GetCName(CName As String) As Boolean
    Dim sName As String
    Dim lNLen As Long
    Dim lRes As Long
    Dim RV As Boolean
    lNLen = 256
    sName = Space(lNLen)
    lRes = GetComputerName(sName, lNLen)
    If lRes <> 0 Then
        CName = Left(sName, lNLen)
        RV = True
    Else
        RV = False
    End If
    GetCName = RV
    End Function以下得到本机IP
    Private Sub Command1_Click()
    If GetCName(CName) = False Then
        MsgBox "没有找到计算机名称!"
    End If
    CName = txtLc.Text
    If txtLc = "游客" Then
         Randomize
         CName = CName & Int((10000 * Rnd) + 1)
    End IffrmMain.Picture1.Visible = False
    frmMain.WindowState = 2
    'frmMain.Left = 0
        mstrUser(0).Address = "192.0.0.255"
        mstrUser(0).lcName = "所有人"
        Label3(0).Caption = mstrUser(cIntUser).lcName
        Text1.Text = mstrUser(0).lcName
    Dim strTemp As String
    strTemp = "#我来了!" & "##" & Winsock1(0).LocalIP & "###" & CName
    Winsock1(0).SendData strTempEnd Sub'Winsock1(0).LocalIP 即本机IP。
      

  2.   

    http://vbsite.my5599.com/source/showdoc.asp?detail_id=3111
    据说可以得到局域网中所有机器的IP地址。