Private Const MAX_COMPUTERNAME_LENGTH As Long = 31 Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long Private Sub Form_Load() Dim dwLen As Long Dim strString As String dwLen = MAX_COMPUTERNAME_LENGTH + 1 strString = String(dwLen, "X") GetComputerName strString, dwLen strString = Left(strString, dwLen) MsgBox strString End Sub
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long Public Function ComputerName() As String Dim cn As String Dim ls As Long Dim res As Long
cn = String(1024, 0) ls = 1024 res = GetComputerName(cn, ls)
If res <> 0 Then ComputerName = Mid(cn, 1, InStr(cn, Chr(0)) - 1) Else ComputerName = "" End If End Function程序中要使用时只要直接 call 即可.例: MsgBox "ComputerName=" & ComputerName
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Dim dwLen As Long
Dim strString As String
dwLen = MAX_COMPUTERNAME_LENGTH + 1
strString = String(dwLen, "X")
GetComputerName strString, dwLen
strString = Left(strString, dwLen)
MsgBox strString
End Sub
Public Function ComputerName() As String
Dim cn As String
Dim ls As Long
Dim res As Long
cn = String(1024, 0)
ls = 1024
res = GetComputerName(cn, ls)
If res <> 0 Then
ComputerName = Mid(cn, 1, InStr(cn, Chr(0)) - 1)
Else
ComputerName = ""
End If
End Function程序中要使用时只要直接 call 即可.例: MsgBox "ComputerName=" & ComputerName