我想请教各位,在vb中如何得到主机名。我用API函数getComputerName为什么不好用。可不可以麻烦哪位有知道的帮帮忙,给一段详细的代码。谢谢!!!

解决方案 »

  1.   

    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
      

  2.   

    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