通过NetUserEnum枚举win2000用户
Public Declare Function NetApiBufferFree Lib "NETAPI32.DLL" (ByVal lpBuffer As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function NetUserEnum Lib "NETAPI32.DLL" (ByVal ServerName As String, ByVal Level As Long, ByVal lFilter As Long, Buffer As Long, ByVal PrefMaxLen As Long, EntriesRead As Long, TotalEntries As Long, ResumeHandle As Long) As Long
Public Const FILTER_NORMAL_ACCOUNT As Long = &H2&
Public Type USER_INFO_0
Name As Long
End Type
Public Declare Function lstrlenW Lib "kernel32" (lpString As Any) As Long
Public Declare Function WideCharToMultiByte Lib "kernel32" (ByVal codepage As Long, ByVal dwFlags As Long, lpWideCharStr As Any, ByVal cchWideChar As Long, lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
Public Const CP_ACP = 0
Public Sub EnumUser(ByVal Server As String)
'枚举指定服务器的用户,参数形式为"\服务器名",如果服务器为本机,Server=""
Dim lRetVal As Long
Dim UIF0 As USER_INFO_0
Dim lpBuf As Long
Dim dwEntriesRead As Long
Dim dwTotalEntries As Long
Dim lSize As Long
Dim lResHandle As Long
Dim lngCount As Long
Do
lRetVal = NetUserEnum(Server, 0, FILTER_NORMAL_ACCOUNT, lpBuf, lSize, dwEntriesRead, dwTotalEntries, lResHandle)
For lngCount = 1 To dwEntriesRead
CopyMemory UIF0, ByVal lpBuf + (lngCount - 1) * 4, Len(UIF0)
Debug.Print GetStrFromPtrW(UIF0.Name) '显示用户名称,你可以在此加入判断指定用户是否存在的代码。
Next
Loop Until dwEntriesRead = dwTotalEntries
NetApiBufferFree lpBuf
End Sub
Public Function GetStrFromPtrW(lpszW As Long) As String
Dim sRtn As String
sRtn = String(lstrlenW(ByVal lpszW) * 2, 0)
Call WideCharToMultiByte(CP_ACP, 0, ByVal lpszW, -1, ByVal sRtn, Len(sRtn), 0, 0)
GetStrFromPtrW = GetStrFromBufferA(sRtn)
End Function
Public Function GetStrFromBufferA(sz As String) As String
If InStr(sz, vbNullChar) Then
GetStrFromBufferA = Left(sz, InStr(sz, vbNullChar) - 1)
Else
GetStrFromBufferA = sz
End If
End Function
注:本代码只能在win2000/NT下运行,因为WinNet API不支持win98