GetComputerNameEx
The GetComputerNameEx function retrieves a NetBIOS or DNS name associated with the local computer. The names are established at system startup, when the system reads them from the registry. GetComputerNameEx can retrieve the NetBIOS name retrieved by the GetComputerName function. In addition, GetComputerNameEx can retrieve fully qualified, host, and domain DNS names.If the local computer is a node in a cluster, GetComputerNameEx can return either the name of the cluster or the name of the local computer. BOOL GetComputerNameEx(
  COMPUTER_NAME_FORMAT NameType,  // name type
  LPTSTR lpBuffer,                // name buffer
  LPDWORD lpnSize                 // size of name buffer
);
Parameters
NameType 
[in] A value from the COMPUTER_NAME_FORMAT enumeration type that specifies the type of name to retrieve. 
This parameter can be one of the following values from the COMPUTER_NAME_FORMAT enumeration type that specifies the type of name to retrieve. Value Description 
ComputerNameNetBIOS The NetBIOS name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the NetBIOS name of the cluster.  
ComputerNameDnsHostname The DNS host name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS host name of the cluster.  
ComputerNameDnsDomain The name of the DNS domain assigned to the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS domain name of the cluster.  
ComputerNameDnsFullyQualified The fully qualified DNS name that uniquely identifies the local computer. This name is a combination of the DNS host name and the DNS domain name, using the form HostName.DomainName. If the local computer is a node in a cluster, lpBuffer receives the fully qualified DNS name of the cluster.  
ComputerNamePhysicalNetBIOS The NetBIOS name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the NetBIOS name of the local computer, not the name of the cluster.  
ComputerNamePhysicalDnsHostname The DNS host name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS host name of the local computer, not the name of the cluster.  
ComputerNamePhysicalDnsDomain The name of the DNS domain assigned to the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS domain name of the local computer, not the name of the cluster.  
ComputerNamePhysicalDnsFullyQualified The fully qualified DNS name that uniquely identifies the computer. If the local computer is a node in a cluster, lpBuffer receives the fully qualified DNS name of the local computer, not the name of the cluster. 
The fully qualified DNS name is a combination of the DNS host name and the DNS domain name, using the form HostName.DomainName. 
 lpBuffer 
[out] Pointer to a buffer that receives a null-terminated string containing the computer name. 
The length of the name may be greater than MAX_COMPUTERNAME_LENGTH characters because DNS allows longer names. lpnSize 
[in/out] On input, specifies the size, in TCHARs, of the buffer. On output, receives the number of TCHARs copied to the destination buffer, including the terminating null character. 
If the buffer is too small, the function fails and GetLastError returns ERROR_MORE_DATA. Return Value
If the function succeeds, the return value is a nonzero value. If the function fails, the return value is zero. To get extended error information, call GetLastError. 

解决方案 »

  1.   

    Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long  '取计算机名称
    '********************************取得计算机名称
        Dim Compu As String * 128
        Dim CompuName As String
        Dim TempLong As Long
        Dim X As Integer
        TempLong = GetComputerName(Compu, 128)
        X = InStr(Compu, Chr(0))
        CompuName = Trim(Left(Compu, X - 1))  'CompuName为机器名称
        
      

  2.   

    Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (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
    Private Declare Function GetComputerNameW Lib "kernel32" (lpBuffer As Any, nSize As Long) As Long
    GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long【说明】  取得这台计算机的名称 【返回值】  Long,TRUE(非零)表示成功,否则返回零。会设置GetLastError 
      

  3.   

    取得本地机名
    Public Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Sub Main()
      Dim Buffer As String
      Dim lSize As Long
      Dim PcName As String
        lSize = 255
        Buffer = Space(255)
        GetComputerName Buffer, lSize
        PcName = Trim(Buffer)
      End Sub