Public Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal sBuffer As String, lSize As Long) As Long Function NameOfPC(MachineName As String) As Long Dim NameSize As Long Dim X As Long MachineName = Space$(255) NameSize = Len(MachineName) X = GetComputerName(MachineName, NameSize) End Function
这里给你一个API例子吧,希望能解决你的问题 '===================================API声明======================= Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long) Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long Public Const MAX_IP = 255 Public Type IPINFO dwAddr As Long dwIndex As Long dwMask As Long dwBCastAddr As Long dwReasmSize As Long unused1 As Integer unused2 As Integer End Type Public Type MIB_IPADDRTABLE dEntrys As Long mIPInfo(MAX_IP) As IPINFO End Type '=================================API声明结束======================'******************************************************************* '** ** '** 6、取得本机IP地址 ** '** ** '******************************************************************* '1、函数功能:取得本机IP地址 '2、参数解释:无 '3、返 回 值:String型 '4、调用示例:call qh_LocalGetIP Public Function qh_LocalGetIP() As String Dim myByte(3) As Byte Dim cnt As Long Dim Ret As Long, Tel As Long Dim bBytes() As Byte Dim StrIP As String Dim Listing As MIB_IPADDRTABLE On Error GoTo ErrS GetIpAddrTable ByVal 0&, Ret, True If Ret <= 0 Then Exit Function ReDim bBytes(0 To Ret - 1) As Byte GetIpAddrTable bBytes(0), Ret, False CopyMemory Listing.dEntrys, bBytes(0), 4 CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel)) CopyMemory myByte(0), Listing.mIPInfo(Tel).dwAddr, 4 For cnt = 0 To 3 StrIP = StrIP & CStr(myByte(cnt)) & "." Next cnt qh_LocalGetIP = Left$(StrIP, Len(StrIP) - 1) Exit Function ErrS: End Function'******************************************************************* '** ** '** 7、取得本机计算机名 ** '** ** '******************************************************************* '1、函数功能:取得本机IP地址 '2、参数解释:无 '3、返 回 值:String型 '4、调用示例:call qh_LocalGetName Public Function qh_LocalGetName() As String Dim s_ComputerName As String Dim s_Len As Long s_Len = 255 s_ComputerName = Space(255) GetComputerName s_ComputerName, s_Len qh_LocalGetName = s_ComputerName End Function'******************************************************************* '** ** '** 8、取得当前计算机登陆用户名 ** '** ** '******************************************************************* '1、函数功能:取得本机IP地址 '2、参数解释:无 '3、返 回 值:String型 '4、调用示例:call qh_LocalGetUser() Public Function qh_LocalGetUser() As String Dim s_strS$, s_lonS&, s_lonI& s_lonS& = 199 s_strS$ = String$(200, 0) s_lonI& = GetUserName(s_strS$, s_lonS) qh_LocalGetUser = Trim(s_strS$) End Function
1、 Option Explicit Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _ (ByVal lpBuffer As String, nSize As Long) As LongPublic Function GetComputerNameA() As String Dim sBuffer As String * 255 If GetComputerName(sBuffer, 255&) <> 0 Then GetComputerNameA = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1) Else GetComputerNameA = "(未知)" End If End FunctionPrivate Sub Command1_Click() MsgBox GetComputerNameA End Sub2、用winsock可以找到: Winsock1.LocalIP^_^
倒,w3k写得还真复杂,没有这么复杂啦 用一个winsock就轻松搞定: Option Explicit Dim fileName As StringPrivate Sub Command1_Click() Dim strString As String, FileNo Dim sVar
If Winsock1.LocalPort = 0 Then strString = "连接埠编号尚未设定!" Else strString = Winsock1.LocalPort End If
Text1.Text = "您的主机名称为:" & Winsock1.LocalHostName & vbCrLf Text1.Text = Text1.Text & "IP 地址为:" & Winsock1.LocalIP & vbCrLf Text1.Text = Text1.Text & "连接埠为:" & strString & vbCrLf Text1.Text = Text1.Text & "通讯协议为:" & IIf(Winsock1.Protocol = 0, "TCP 协定", "UDP 协定") FileNo = FreeFile() Open fileName For Input As #FileNo While Not EOF(FileNo) Line Input #FileNo, sVar Text1.Text = Text1.Text & sVar & vbCrLf Wend Close #FileNoEnd SubPrivate Sub Command2_Click() Unload Me End SubPrivate Sub Form_Load()
fileName = "c:\IPDetail.txt"
Shell "command.com /c ipconfig.exe > " & fileName DoEvents Do While Trim(Dir(fileName)) = "" DoEvents LoopEnd Sub ---得到用户名字 Option Explicit Dim strUsername As String Dim length As Long Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As LongPrivate Sub Form_Load() length = 80 strUsername = String(length, Chr(0)) GetUserName strUsername, length strUsername = Left(strUsername, InStr(strUsername, Chr(0)) - 1) MsgBox strUsername End Sub
"GetComputerNameA" (ByVal sBuffer As String, lSize As Long) As Long
Function NameOfPC(MachineName As String) As Long
Dim NameSize As Long
Dim X As Long
MachineName = Space$(255)
NameSize = Len(MachineName)
X = GetComputerName(MachineName, NameSize)
End Function
'===================================API声明=======================
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
Public Const MAX_IP = 255
Public Type IPINFO
dwAddr As Long
dwIndex As Long
dwMask As Long
dwBCastAddr As Long
dwReasmSize As Long
unused1 As Integer
unused2 As Integer
End Type
Public Type MIB_IPADDRTABLE
dEntrys As Long
mIPInfo(MAX_IP) As IPINFO
End Type
'=================================API声明结束======================'*******************************************************************
'** **
'** 6、取得本机IP地址 **
'** **
'*******************************************************************
'1、函数功能:取得本机IP地址
'2、参数解释:无
'3、返 回 值:String型
'4、调用示例:call qh_LocalGetIP
Public Function qh_LocalGetIP() As String
Dim myByte(3) As Byte
Dim cnt As Long
Dim Ret As Long, Tel As Long
Dim bBytes() As Byte
Dim StrIP As String
Dim Listing As MIB_IPADDRTABLE
On Error GoTo ErrS
GetIpAddrTable ByVal 0&, Ret, True
If Ret <= 0 Then Exit Function
ReDim bBytes(0 To Ret - 1) As Byte
GetIpAddrTable bBytes(0), Ret, False
CopyMemory Listing.dEntrys, bBytes(0), 4
CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
CopyMemory myByte(0), Listing.mIPInfo(Tel).dwAddr, 4
For cnt = 0 To 3
StrIP = StrIP & CStr(myByte(cnt)) & "."
Next cnt
qh_LocalGetIP = Left$(StrIP, Len(StrIP) - 1)
Exit Function
ErrS:
End Function'*******************************************************************
'** **
'** 7、取得本机计算机名 **
'** **
'*******************************************************************
'1、函数功能:取得本机IP地址
'2、参数解释:无
'3、返 回 值:String型
'4、调用示例:call qh_LocalGetName
Public Function qh_LocalGetName() As String
Dim s_ComputerName As String
Dim s_Len As Long s_Len = 255
s_ComputerName = Space(255)
GetComputerName s_ComputerName, s_Len
qh_LocalGetName = s_ComputerName
End Function'*******************************************************************
'** **
'** 8、取得当前计算机登陆用户名 **
'** **
'*******************************************************************
'1、函数功能:取得本机IP地址
'2、参数解释:无
'3、返 回 值:String型
'4、调用示例:call qh_LocalGetUser()
Public Function qh_LocalGetUser() As String
Dim s_strS$, s_lonS&, s_lonI& s_lonS& = 199
s_strS$ = String$(200, 0)
s_lonI& = GetUserName(s_strS$, s_lonS)
qh_LocalGetUser = Trim(s_strS$)
End Function
Option Explicit
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
(ByVal lpBuffer As String, nSize As Long) As LongPublic Function GetComputerNameA() As String
Dim sBuffer As String * 255
If GetComputerName(sBuffer, 255&) <> 0 Then
GetComputerNameA = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
Else
GetComputerNameA = "(未知)"
End If
End FunctionPrivate Sub Command1_Click()
MsgBox GetComputerNameA
End Sub2、用winsock可以找到:
Winsock1.LocalIP^_^
用一个winsock就轻松搞定:
Option Explicit
Dim fileName As StringPrivate Sub Command1_Click()
Dim strString As String, FileNo
Dim sVar
If Winsock1.LocalPort = 0 Then
strString = "连接埠编号尚未设定!"
Else
strString = Winsock1.LocalPort
End If
Text1.Text = "您的主机名称为:" & Winsock1.LocalHostName & vbCrLf
Text1.Text = Text1.Text & "IP 地址为:" & Winsock1.LocalIP & vbCrLf
Text1.Text = Text1.Text & "连接埠为:" & strString & vbCrLf
Text1.Text = Text1.Text & "通讯协议为:" & IIf(Winsock1.Protocol = 0, "TCP 协定", "UDP 协定") FileNo = FreeFile()
Open fileName For Input As #FileNo
While Not EOF(FileNo)
Line Input #FileNo, sVar
Text1.Text = Text1.Text & sVar & vbCrLf
Wend
Close #FileNoEnd SubPrivate Sub Command2_Click()
Unload Me
End SubPrivate Sub Form_Load()
fileName = "c:\IPDetail.txt"
Shell "command.com /c ipconfig.exe > " & fileName
DoEvents Do While Trim(Dir(fileName)) = ""
DoEvents
LoopEnd Sub
---得到用户名字
Option Explicit
Dim strUsername As String
Dim length As Long
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As LongPrivate Sub Form_Load()
length = 80
strUsername = String(length, Chr(0))
GetUserName strUsername, length
strUsername = Left(strUsername, InStr(strUsername, Chr(0)) - 1)
MsgBox strUsername
End Sub
用一个winsock就轻松搞定:=========================================================================
W3K也是好心帮我答问题!
论坛是交流学习的开发者天地,杜绝不文明的言论(要以斑竹为榜样,引用daisy8675(莫依)的"倒")
我看下次真没有^^[0]^^
W3K API编程(莫依说你的好复杂哦HOHO)
daisy8675(莫依) 控件编程(简单实用,看来想必是位美女)
huangjianyou(小健) API编程
libiyang(爱情抗体) API编程
谢谢各位的帮助(特别感谢W3K|daisy8675(莫依) (的热心帮助)