all ipOption Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ?1996-2003 VBnet, Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 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 = 0Private Type IP_ADDRESS_STRING IpAddr(0 To 15) As Byte End TypePrivate Type IP_MASK_STRING IpMask(0 To 15) As Byte End TypePrivate Type IP_ADDR_STRING dwNext As Long IpAddress As IP_ADDRESS_STRING IpMask As IP_MASK_STRING dwContext As Long End TypePrivate Type IP_ADAPTER_INFO dwNext As Long ComboIndex As Long 'reserved sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte dwAddressLength As Long sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte dwIndex As Long uType As Long uDhcpEnabled As Long CurrentIpAddress As Long IpAddressList As IP_ADDR_STRING GatewayList As IP_ADDR_STRING DhcpServer As IP_ADDR_STRING bHaveWins As Long PrimaryWinsServer As IP_ADDR_STRING SecondaryWinsServer As IP_ADDR_STRING LeaseObtained As Long LeaseExpires As Long End TypePrivate 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) Private Sub Command1_Click() 'pass a character to be used as the 'delimiter in the list of returned addresses. Text1.Text = LocalIPAddresses("+") End Sub Public Function LocalIPAddresses(ByVal sDelim As String) As String
'api vars Dim cbRequired As Long Dim buff() As Byte Dim Adapter As IP_ADAPTER_INFO Dim AdapterStr As IP_ADDR_STRING
'working vars Dim ptr1 As Long Dim sIPAddr As String Dim sAllAddr As String
Call GetAdaptersInfo(ByVal 0&, cbRequired) If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte
If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
'get a pointer to the data stored in buff() ptr1 = VarPtr(buff(0))
'ptr1 is 0 when no more adapters Do While (ptr1 <> 0)
'copy the data from the pointer to the 'first adapter into the IP_ADAPTER_INFO type CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
With Adapter
'the DHCP IP address is in the 'IpAddress.IpAddr member sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode)) sAllAddr = sAllAddr & sIPAddr & "+"
'more? ptr1 = .dwNext
End With 'With Adapter
Loop 'Do While (ptr1 <> 0) End If 'If GetAdaptersInfo End If 'If cbRequired > 0 'remove the last comma If Len(sAllAddr) > 0 Then sAllAddr = Left$(sAllAddr, Len(sAllAddr) - 1) End If
'return any string found LocalIPAddresses = sAllAddr
End Function Private Function TrimNull(item As String) Dim pos As Integer
'double check that there is a chr$(0) in the string pos = InStr(item, Chr$(0)) If pos Then TrimNull = Left$(item, pos - 1) Else: TrimNull = item End If
End Function '--end block--'
Private Declare Function GetComputerNameA Lib "kernel32" _ (ByVal lpBuffer As String, nSize As Long) As Long' ' Returns the computer's name ' Public Function GetComputerName() As String Dim UserName As String * 255 Call GetComputerNameA(UserName, 255) GetComputerName = Left$(UserName, InStr(UserName, Chr$(0)) - 1) End Function
http://www.cbinews.com/start/index.jsp
拖一个winsock控件 Private Sub Command1_Click() MsgBox Winsock1.LocalHostName & Winsock1.LocalIP End Sub
lxcc的最简单,直接调用winsock的LocalHostName,LocalIP属性,远端的主机名和IP地址可以用RemoteHost,RemoteHostIP属性获得。API函数GetComputerName有一个相对应的函数SetComputerName可以设置计算机名称。 Private Declare Function GetComputerName Lib "kernel32" Alias _ "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long Private Declare Function SetComputerName Lib "kernel32" Alias _ "SetComputerNameA" (ByVal lpComputerName As String) As Long Private Sub Command1_Click() Dim Name1 As String * 255 Dim name As String Dim i As Integer
i = GetComputerName(Name1, 255)
If i <> 0 Then name = Name1 Else name = "unknown" End If
Text1 = name
End SubPrivate Sub Command2_Click() Dim i As Integer Dim name As String
name = Text1.Text
i = SetComputerName(name) If i = 0 Then MsgBox "fail!", vbOKOnly Else MsgBox "success!", vbOKOnly End If
为API函数:
e.x.:
nSize = 80
cpname = String(nSize, Chr(0)) 'Chr(0)·µ»Ø¿Õ "" ,ex.Chr(65)·µ»Ø A
GetComputerName cpname, nSize 'µÃµ½name·ÅÔÚCpNameÖУ¬Îª80×Ö·û³¤
cpname = Left(cpname, InStr(cpname, Chr(0)) - 1) 'ɾȥCpNameÖеĺóÃæµÄ¿Õ¸ñ£¬ÓÉ80¼õΪʵ¼Ê×Ö·ûÊý
结果在cpname中
GetComputerName
为API函数:
e.x.:
nSize = 80
cpname = String(nSize, Chr(0))
GetComputerName cpname, nSize
cpname = Left(cpname, InStr(cpname, Chr(0)) - 1)
结果在cpname中
查一下WInAPI帮助
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ?1996-2003 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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 = 0Private Type IP_ADDRESS_STRING
IpAddr(0 To 15) As Byte
End TypePrivate Type IP_MASK_STRING
IpMask(0 To 15) As Byte
End TypePrivate Type IP_ADDR_STRING
dwNext As Long
IpAddress As IP_ADDRESS_STRING
IpMask As IP_MASK_STRING
dwContext As Long
End TypePrivate Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long 'reserved
sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
dwAddressLength As Long
sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
dwIndex As Long
uType As Long
uDhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
bHaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End TypePrivate 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)
Private Sub Command1_Click() 'pass a character to be used as the
'delimiter in the list of returned addresses.
Text1.Text = LocalIPAddresses("+")
End Sub
Public Function LocalIPAddresses(ByVal sDelim As String) As String
'api vars
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING
'working vars
Dim ptr1 As Long
Dim sIPAddr As String
Dim sAllAddr As String
Call GetAdaptersInfo(ByVal 0&, cbRequired) If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte
If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
'get a pointer to the data stored in buff()
ptr1 = VarPtr(buff(0))
'ptr1 is 0 when no more adapters
Do While (ptr1 <> 0)
'copy the data from the pointer to the
'first adapter into the IP_ADAPTER_INFO type
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
With Adapter
'the DHCP IP address is in the
'IpAddress.IpAddr member
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))
sAllAddr = sAllAddr & sIPAddr & "+"
'more?
ptr1 = .dwNext
End With 'With Adapter
Loop 'Do While (ptr1 <> 0) End If 'If GetAdaptersInfo
End If 'If cbRequired > 0 'remove the last comma
If Len(sAllAddr) > 0 Then
sAllAddr = Left$(sAllAddr, Len(sAllAddr) - 1)
End If
'return any string found
LocalIPAddresses = sAllAddr
End Function
Private Function TrimNull(item As String) Dim pos As Integer
'double check that there is a chr$(0) in the string
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If
End Function
'--end block--'
Private Declare Function GetComputerNameA Lib "kernel32" _
(ByVal lpBuffer As String, nSize As Long) As Long'
' Returns the computer's name
'
Public Function GetComputerName() As String
Dim UserName As String * 255 Call GetComputerNameA(UserName, 255)
GetComputerName = Left$(UserName, InStr(UserName, Chr$(0)) - 1)
End Function
Private Sub Command1_Click()
MsgBox Winsock1.LocalHostName & Winsock1.LocalIP
End Sub
Private Declare Function GetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function SetComputerName Lib "kernel32" Alias _
"SetComputerNameA" (ByVal lpComputerName As String) As Long
Private Sub Command1_Click()
Dim Name1 As String * 255
Dim name As String
Dim i As Integer
i = GetComputerName(Name1, 255)
If i <> 0 Then
name = Name1
Else
name = "unknown"
End If
Text1 = name
End SubPrivate Sub Command2_Click()
Dim i As Integer
Dim name As String
name = Text1.Text
i = SetComputerName(name)
If i = 0 Then
MsgBox "fail!", vbOKOnly
Else
MsgBox "success!", vbOKOnly
End If
End Sub