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
    'Create a buffer
    dwLen = MAX_COMPUTERNAME_LENGTH + 1
    strString = String(dwLen, "X")
    'Get the computer name
    GetComputerName strString, dwLen
    'get only the actual data
    strString = Left(strString, dwLen)
    'Show the computer name
    MsgBox strString
End Sub
得到本机机器名

解决方案 »

  1.   

    Option Explicit
    Private Const NCBASTAT = &H33
    Private Const NCBNAMSZ = 16
    Private Const HEAP_ZERO_MEMORY = &H8
    Private Const HEAP_GENERATE_EXCEPTIONS = &H4
    Private Const NCBRESET = &H32Private Type NCB
        ncb_command As Byte 'Integer
        ncb_retcode As Byte 'Integer
        ncb_lsn As Byte 'Integer
        ncb_num As Byte ' Integer
        ncb_buffer As Long 'String
        ncb_length As Integer
        ncb_callname As String * NCBNAMSZ
        ncb_name As String * NCBNAMSZ
        ncb_rto As Byte 'Integer
        ncb_sto As Byte ' Integer
        ncb_post As Long
        Ncb_Lana_num As Byte 'Integer
        ncb_cmd_cplt As Byte 'Integer
        ncb_reserve(9) As Byte ' Reserved, must be 0
        ncb_event As Long
    End TypePrivate Type ADAPTER_STATUS
        adapter_address(5) As Byte 'As String * 6
        rev_major As Byte 'Integer
        reserved0 As Byte 'Integer
        adapter_type As Byte 'Integer
        rev_minor As Byte 'Integer
        duration As Integer
        frmr_recv As Integer
        frmr_xmit As Integer
        iframe_recv_err As Integer
        xmit_aborts As Integer
        xmit_success As Long
        recv_success As Long
        iframe_xmit_err As Integer
        recv_buff_unavail As Integer
        t1_timeouts As Integer
        ti_timeouts As Integer
        Reserved1 As Long
        free_ncbs As Integer
        max_cfg_ncbs As Integer
        max_ncbs As Integer
        xmit_buf_unavail As Integer
        max_dgram_size As Integer
        pending_sess As Integer
        max_cfg_sess As Integer
        max_sess As Integer
        max_sess_pkt_size As Integer
        name_count As Integer
    End TypePrivate Type NAME_BUFFER
        name As String * NCBNAMSZ
        name_num As Integer
        name_flags As Integer
    End TypePrivate Type ASTAT
        adapt As ADAPTER_STATUS
        NameBuff(30) As NAME_BUFFER
    End TypePrivate Declare Function Netbios Lib "netapi32.dll" (pncb As NCB) As Byte
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
    Private Declare Function GetProcessHeap Lib "kernel32" () As Long
    Private Declare Function HeapAlloc Lib "kernel32" _
        (ByVal hHeap As Long, ByVal dwFlags As Long, _
        ByVal dwBytes As Long) As Long
    Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, _
        ByVal dwFlags As Long, lpMem As Any) As Long
        
    Public Function Address(Ncb_Lana_num As Integer, NetName As String) As String
        Dim myNcb As NCB
        Dim bRet As Byte
        myNcb.ncb_command = NCBRESET
        bRet = Netbios(myNcb)
        myNcb.ncb_command = NCBASTAT
        myNcb.Ncb_Lana_num = Ncb_Lana_num
        myNcb.ncb_callname = NetName
        
        Dim myASTAT As ASTAT, tempASTAT As ASTAT
        Dim pASTAT As Long
        Dim Ind%
        Dim AddressID$ '网卡号
        
        myNcb.ncb_length = Len(myASTAT)
        
        Debug.Print Err.LastDllError
        pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS _
            Or HEAP_ZERO_MEMORY, myNcb.ncb_length)
        If pASTAT = 0 Then
            Debug.Print "memory allcoation failed!"
            Exit Function
        End If
        myNcb.ncb_buffer = pASTAT
        bRet = Netbios(myNcb)
        Debug.Print Err.LastDllError
        CopyMemory myASTAT, myNcb.ncb_buffer, Len(myASTAT)
        
    '    MsgBox Hex(myASTAT.adapt.adapter_address(0)) & " " & _
            Hex(myASTAT.adapt.adapter_address(1)) _
            & " " & Hex(myASTAT.adapt.adapter_address(2)) & " " _
            & Hex(myASTAT.adapt.adapter_address(3)) _
            & " " & Hex(myASTAT.adapt.adapter_address(4)) & " " _
            & Hex(myASTAT.adapt.adapter_address(5))
        HeapFree GetProcessHeap(), 0, pASTAT
        
        AddressID = ""
        With myASTAT.adapt
            For Ind = 0 To 5
                AddressID = AddressID + Right("00" + Hex$(.adapter_address(Ind)), 2)
            Next Ind
        End With
        
        'List1.AddItem Address + "  " + Text1
        
        Address = AddressID
    End Function这是我写的一个网卡类
      

  2.   

    '*********************************************************
    '* 名称:ComputerName
    '* 功能:返回计算机名称
    '* 用法:
    '*********************************************************
    Public Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As LongPublic Function ComputerName() As String
        Dim l1 As String
        Dim l2 As Long
        Dim l3 As Long
        l2 = 255
        l1 = String$(l2, " ")
        l3 = GetComputerName(l1, l2)
        ComputerName = ""
        If l3 <> 0 Then
            ComputerName = Left(l1, l2)
        End If
    End Function
      

  3.   

    返回Ip
    Private Const WS_VERSION_REQD = &H101
    Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
    Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
    Private Const MIN_SOCKETS_REQD = 1
    Private Const SOCKET_ERROR = -1
    Private Const WSADescription_Len = 256
    Private Const WSASYS_Status_Len = 128Private Type HOSTENT
       hName As Long
       hAliases As Long
       hAddrType As Integer
       hLength As Integer
       hAddrList As Long
    End TypePrivate Type WSADATA
       wversion As Integer
       wHighVersion As Integer
       szDescription(0 To WSADescription_Len) As Byte
       szSystemStatus(0 To WSASYS_Status_Len) As Byte
       iMaxSockets As Integer
       iMaxUdpDg As Integer
       lpszVendorInfo As Long
    End TypePrivate Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
    Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, lpWSAData As WSADATA) As Long
    Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
    Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long
    Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)' #VBIDEUtils#************************************************************
    ' * Programmer Name  : Waty Thierry
    ' * Web Site         : www.geocities.com/ResearchTriangle/6311/
    ' * E-Mail           : [email protected]
    ' * Date             : 13/10/98
    ' * Time             : 10:24
    ' * Module Name      : IP_Module
    ' * Module Filename  : IP.bas
    ' **********************************************************************
    ' * Comments         :
    ' * Find IP address ginving the hostname
    ' **********************************************************************Function hibyte(ByVal wParam As Integer)
       
       hibyte = wParam \ &H100 And &HFF&
       
    End FunctionFunction lobyte(ByVal wParam As Integer)
       
       lobyte = wParam And &HFF&
       
    End FunctionSub SocketsInitialize()
       
       Dim WSAD As WSADATA
       Dim iReturn As Integer
       Dim sLowByte As String, sHighByte As String, sMsg As String
       
       iReturn = WSAStartup(WS_VERSION_REQD, WSAD)
       
       If iReturn <> 0 Then
          MsgBox "Winsock.dll is not responding."
          End
       End If
       
       If lobyte(WSAD.wversion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then
          sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
          sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
          sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
          sMsg = sMsg & " is not supported by winsock.dll "
          MsgBox sMsg
          End
       End If
       
       If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
          sMsg = "This application requires a minimum of "
          sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
          MsgBox sMsg
          End
       End If
       
    End SubSub SocketsCleanup()
       Dim lReturn As Long
       
       lReturn = WSACleanup()
       
       If lReturn <> 0 Then
          MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup "
          End
       End If
       
    End SubSub Form_Load()
       
       SocketsInitialize
       
    End SubPrivate Sub Form_Unload(Cancel As Integer)
       
       SocketsCleanup
       
    End SubPrivate Sub Command1_click()
       Dim hostent_addr As Long
       Dim host As HOSTENT
       Dim hostip_addr As Long
       Dim temp_ip_address() As Byte
       Dim i As Integer
       Dim ip_address As String   hostent_addr = gethostbyname("westwin")   If hostent_addr = 0 Then
          MsgBox "Can't resolve name."
          Exit Sub
       End If
       
       RtlMoveMemory host, hostent_addr, LenB(host)
       
       RtlMoveMemory hostip_addr, host.hAddrList, 4   ReDim temp_ip_address(1 To host.hLength)
       RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
       
       For i = 1 To host.hLength
          ip_address = ip_address & temp_ip_address(i) & "."
       Next
       ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
       
       MsgBox ip_address
    End Sub
      

  4.   

    Private Sub Form_Load()
    '********其实不用很麻烦的*********
    '*********AddList 为list 控件******
        Call GetIP
    End Sub
    Public Sub GetIP()
        Set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
                ("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
        
        For Each IPConfig In IPConfigSet
        
            If Not IsNull(IPConfig.IPAddress) Then
                For i = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
                    addlist.AddItem IPConfig.IPAddress(i)
                Next
            End If
        Next
    End Sub