取计算机名的代码,这两种方式哪种好呢?代码在下面,我主要想问,用api函数提到机器名称后,字符串的尾部都是空格。
用什么办法截去空格呢?下面两种方法哪种更科学呢?一种是查找0字符,一种是根据 nSize 参数, '通过查找0字符,来确定机器名的长度
getPcName = Left(s, InStr(1, s, Chr(0)) - 1)
'通过 nSize 参数,来确定机器名的长度
getPcName = Left(s, sz)
Option ExplicitPublic Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Const MAX_COMPUTERNAME_LENGTH = 15Public Function getPcName() As String
Dim s As String
'按机器名长度确定字符串
s = String$(MAX_COMPUTERNAME_LENGTH + 1, Chr(0))
Dim dl As Long '返回值
Dim sz As Long '长度
sz = MAX_COMPUTERNAME_LENGTH + 1
dl = GetComputerName(s, sz)
'通过查找0字符,来确定机器名的长度
getPcName = Left(s, InStr(1, s, Chr(0)) - 1)
'通过 nSize 参数,来确定机器名的长度
getPcName = Left(s, sz)
End Function
用什么办法截去空格呢?下面两种方法哪种更科学呢?一种是查找0字符,一种是根据 nSize 参数, '通过查找0字符,来确定机器名的长度
getPcName = Left(s, InStr(1, s, Chr(0)) - 1)
'通过 nSize 参数,来确定机器名的长度
getPcName = Left(s, sz)
Option ExplicitPublic Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Const MAX_COMPUTERNAME_LENGTH = 15Public Function getPcName() As String
Dim s As String
'按机器名长度确定字符串
s = String$(MAX_COMPUTERNAME_LENGTH + 1, Chr(0))
Dim dl As Long '返回值
Dim sz As Long '长度
sz = MAX_COMPUTERNAME_LENGTH + 1
dl = GetComputerName(s, sz)
'通过查找0字符,来确定机器名的长度
getPcName = Left(s, InStr(1, s, Chr(0)) - 1)
'通过 nSize 参数,来确定机器名的长度
getPcName = Left(s, sz)
End Function
The GetComputerName function retrieves the computer name of the current system. This name is established at system startup, when it is initialized from the registry. BOOL GetComputerName(
LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);
Parameters
lpBuffer
Pointer to a buffer that receives a null-terminated string containing the computer name. The buffer size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.
nSize
Pointer to a DWORD variable. On input, the variable specifies the size, in bytes or characters, of the buffer. On output, the variable returns the number of bytes or characters copied to the destination buffer, not including the terminating null character.
If the buffer is too small, the function fails, GetLastError returns ERROR_BUFFER_OVERRUN, and the variable returns the required buffer size including the terminating null character. Windows 95 and Windows 98: GetComputerName fails if the input size is less than MAX_COMPUTERNAME_LENGTH + 1. Return Values
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, callGetLastError. 综上所述,应通过查找0字符,来确定机器名的长度。
还有最好判断一下dl值(GetComputerName的返回值)
getpcname=Environ$("ComputerName")
end function
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Dim ComputerName As String
ComputerName = String(255, " ")
GetComputerName ComputerName, 255
ComputerName = Trim(ComputerName)
ComputerName = Left(ComputerName, Len(ComputerName) - 1)
MsgBox ComputerName
End Sub
部件添加winsock控件,再添加winsock1到窗体。Private Sub Form_Load()
MsgBox Winsock1.LocalHostName
End Sub