如何获得当前计算机的名称,又如何修改呢,要时能获得修改计算机名称前的名称就更好了!

sosolong 2003-02-28 05:01:38
(1)如何获得当前计算机的名称

(2)又如何修改呢

(3)能否获得修改计算机名称前的计算机名称呢?
...全文
108 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sbj325 2003-03-01
  • 打赏
  • 举报
回复
这么复杂?
还是UP一下吧!
Matthewzhong 2003-03-01
  • 打赏
  • 举报
回复
up
northwolves 2003-02-28
  • 打赏
  • 举报
回复

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function MachineName() As String
Dim sBuffer As String * 255
MachineName = IIf(GetComputerName(sBuffer, 255&) <> 0, Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1), "(未知)")
End Function
Private Sub Form_Load()
MsgBox MachineName()
End Sub
fbmsf 2003-02-28
  • 打赏
  • 举报
回复
BOOL GetComputerName(

LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);


BOOL SetComputerName(

LPCTSTR lpComputerName // address of new computer name
);
kmcyz 2003-02-28
  • 打赏
  • 举报
回复
(1)用WINSOCK控件
sock.localname
ganzhiruogy 2003-02-28
  • 打赏
  • 举报
回复
(1)如何获得当前计算机的名称
'Example Name: Enumerating the User List and Obtaining User Info

'------------------------------------------------------------------------------
'
' Form Code
'
'------------------------------------------------------------------------------

Option Explicit

'Windows type used to call the Net API
Private Type USER_INFO_10
usr10_name As Long
usr10_comment As Long
usr10_usr_comment As Long
usr10_full_name As Long
End Type

'private type to hold the actual strings displayed
Private Type USER_INFO
name As String
full_name As String
comment As String
usr_comment As String
End Type

Private Const ERROR_SUCCESS As Long = 0&
Private Const MAX_COMPUTERNAME As Long = 15
Private Const MAX_USERNAME As Long = 256
Private Const FILTER_NORMAL_ACCOUNT As Long = &H2

Private Declare Function NetUserGetInfo Lib "netapi32" _
(lpServer As Byte, _
username As Byte, _
ByVal level As Long, _
lpBuffer As Long) As Long

Private Declare Function NetUserEnum Lib "netapi32" _
(servername As Byte, _
ByVal level As Long, _
ByVal filter As Long, _
buff As Long, _
ByVal buffsize As Long, _
entriesread As Long, _
totalentries As Long, _
resumehandle As Long) As Long

Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal Buffer As Long) As Long

Private Declare Function GetUserName Lib "advapi32" _
Alias "GetUserNameA" _
(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 Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(xDest As Any, _
xSource As Any, _
ByVal nBytes As Long)

Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long

Private Declare Function StrLen Lib "kernel32" _
Alias "lstrlenW" _
(ByVal lpString As Long) As Long


Private Sub Form_Load()

Dim tmp As String
Dim bServername() As Byte

tmp = rgbGetComputerName()

'assure the server string is properly formatted
If Len(tmp) Then

If InStr(tmp, "\\") Then
bServername = tmp & Chr$(0)
Else: bServername = "\\" & tmp & Chr$(0)
End If

End If

Text1.Text = tmp

Call GetUserEnumInfo(bServername())

End Sub


Private Function GetUserEnumInfo(bServername() As Byte)

Dim users() As Long
Dim buff As Long
Dim buffsize As Long
Dim entriesread As Long
Dim totalentries As Long
Dim cnt As Integer

buffsize = 255

If NetUserEnum(bServername(0), 0, _
FILTER_NORMAL_ACCOUNT, _
buff, buffsize, _
entriesread, _
totalentries, 0&) = ERROR_SUCCESS Then

ReDim users(0 To entriesread - 1) As Long
CopyMemory users(0), ByVal buff, entriesread * 4

For cnt = 0 To entriesread - 1
List1.AddItem GetPointerToByteStringW(users(cnt))
Next cnt

NetApiBufferFree buff

End If

End Function


Private Function rgbGetComputerName() As String

'returns the name of the computer
Dim tmp As String

tmp = Space$(MAX_COMPUTERNAME + 1)

If GetComputerName(tmp, Len(tmp)) <> 0 Then
rgbGetComputerName = TrimNull(tmp)
End If

End Function


Private Function TrimNull(item As String)

Dim pos As Integer

pos = InStr(item, Chr$(0))

If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function


Private Function GetUserNetworkInfo(bServername() As Byte, bUsername() As Byte) As USER_INFO

Dim usrapi As USER_INFO_10
Dim buff As Long

If NetUserGetInfo(bServername(0), bUsername(0), 10, buff) = ERROR_SUCCESS Then

'copy the data from buff into the
'API user_10 structure
CopyMemory usrapi, ByVal buff, Len(usrapi)

'extract each member and return
'as members of the UDT
GetUserNetworkInfo.name = GetPointerToByteStringW(usrapi.usr10_name)
GetUserNetworkInfo.full_name = GetPointerToByteStringW(usrapi.usr10_full_name)
GetUserNetworkInfo.comment = GetPointerToByteStringW(usrapi.usr10_comment)
GetUserNetworkInfo.usr_comment = GetPointerToByteStringW(usrapi.usr10_usr_comment)

NetApiBufferFree buff

End If

End Function


Private Function GetPointerToByteStringW(lpString As Long) As String

Dim buff() As Byte
Dim nSize As Long

If lpString Then

'its Unicode, so mult. by 2
nSize = lstrlenW(lpString) * 2

If nSize Then
ReDim buff(0 To (nSize - 1)) As Byte
CopyMemory buff(0), ByVal lpString, nSize
GetPointerToByteStringW = buff
End If

End If

End Function


Private Sub List1_Click()

Dim usr As USER_INFO
Dim bUsername() As Byte
Dim bServername() As Byte
Dim tmp As String

'This assures that both the server
'and user params have data
If Len(Text1.Text) And List1.ListIndex > -1 Then

bUsername = List1.List(List1.ListIndex) & Chr$(0)

'This demo uses the current machine as the
'server param, which works on NT4 and Win2000.
'If connected to a PDC or BDC, pass that
'name as the server, instead of the return
'value from GetComputerName().
tmp = Text1.Text

'assure the server string is properly formatted
If Len(tmp) Then

If InStr(tmp, "\\") Then
bServername = tmp & Chr$(0)
Else: bServername = "\\" & tmp & Chr$(0)
End If

End If

usr = GetUserNetworkInfo(bServername(), bUsername())

Text3.Text = usr.name

'The call may or may not return the
'full name, comment or usr_comment
'members, depending on the user's
'listing in User Manager.
Text4.Text = usr.full_name
Text5.Text = usr.comment
Text6.Text = usr.usr_comment

End If

End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧