目的:获取 Workstation 配置信息 和当前用户.
To a form add a command button (Command1), list box (List1), and two labels (Label1, Label2). A third set of labels in a control array can be used for the list item captions. Add the following to the form:
==================================================================
Option Explicit
Private Const NERR_SUCCESS As Long = 0&
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const ERROR_MORE_DATA As Long = 234&
Private Const LB_SETTABSTOPS As Long = &H192
'for use on Win NT/2000 only
Private Type WKSTA_INFO_102
wki102_platform_id As Long
wki102_computername As Long
wki102_langroup As Long
wki102_ver_major As Long
wki102_ver_minor As Long
wki102_lanroot As Long
wki102_logged_on_users As Long
End Type
Private Type WKSTA_USER_INFO_0
wkui0_username As Long
End Type
Private Declare Function NetWkstaGetInfo Lib "netapi32" _
(ByVal servername As Long, _
ByVal level As Long, _
bufptr As Long) As Long
Private Declare Function NetWkstaUserEnum Lib "netapi32" _
(ByVal servername As Long, _
ByVal level As Long, _
bufptr As Long, _
ByVal prefmaxlen As Long, _
entriesread As Long, _
totalentries As Long, _
resume_handle As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal Buffer As Long) As Long
'common
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pTo As Any, _
uFrom As Any, _
ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
If success = NERR_SUCCESS And _
success <> ERROR_MORE_DATA Then
nStructSize = LenB(ws102)
'cast data into a WKSTA_INFO_102 type
'and add the data to a list, calling
'the GetWorkstationUserName function
'to retrieve the logon username for
'the dwServer passed
CopyMemory ws102, ByVal bufptr, nStructSize
Private Function GetPointerToByteStringW(ByVal dwData As Long) As String
Dim tmp() As Byte
Dim tmplen As Long
If dwData <> 0 Then
tmplen = lstrlenW(dwData) * 2
If tmplen <> 0 Then
ReDim tmp(0 To (tmplen - 1)) As Byte
CopyMemory tmp(0), ByVal dwData, tmplen
GetPointerToByteStringW = tmp
End If
End If
End Function
Private Function GetWorkstationUserName(ByVal dwWorkstation As Long) As String
Dim bufptr As Long
Dim dwEntriesread As Long
Dim dwTotalentries As Long
Dim dwResumehandle As Long
Dim success As Long
Dim nStructSize As Long
Dim wui0 As WKSTA_USER_INFO_0
If success = NERR_SUCCESS And _
success <> ERROR_MORE_DATA Then
nStructSize = LenB(wui0)
If dwEntriesread > 0 Then
'cast data into WKSTA_USER_INFO_0 since
'we only need the user name. Although this
'API enumerates and returns information
'about all users currently logged on to
'the workstation, including interactive,
'service and batch logons, chances are
'that the first user enumerated was the
'user who logged on the session so we
'exit after the first user info is returned.
'
'If this presumption is incorrect, please
'let me know via the VBnet Comments link.
CopyMemory wui0, ByVal bufptr, nStructSize
如果您用的是Windows 2000的机器,您还可以试一下以下的简单WMI代码:
For Each computer In GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_ComputerSystem")
MsgBox computer.domain
Next