- Tham gia
- 30/5/06
- Bài viết
- 1,798
- Được thích
- 4,706
- Giới tính
- Nam
Chào các bạn,
Trong một số trường hợp lập trình VBA trong Excel bạn lại cần biết đến các thông tin của Network. Tôi xin giới thiệu với các bạn file tôi tìm trên trang web
http://www.appspro.com/
Đầu tiên bạn đưa đoạn code này vào module
Trong một số trường hợp lập trình VBA trong Excel bạn lại cần biết đến các thông tin của Network. Tôi xin giới thiệu với các bạn file tôi tìm trên trang web
http://www.appspro.com/
Đầu tiên bạn đưa đoạn code này vào module
Mã:
Option Explicit
''' **************************************************************************
''' Module constant declarations follow.
''' **************************************************************************
''' API constants.
Private Const NERR_SUCCESS As Long = 0&
Private Const GET_USER_INFO_1 As Long = 1&
Private Const VER_PLATFORM_WIN32_NT As Long = 2&
Private Const MAX_PREFERRED_LENGTH As Long = &H4000& ''' Tells the NetUserGetLocalGroups API to allocate as much memory as required for the return result.
''' **************************************************************************
''' Module type declarations follow.
''' **************************************************************************
''' This struct is loaded by the GetVersionExA API.
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
''' This is the struct returned by the NetWkstaUserGetInfo API.
Private Type WKSTA_USER_INFO_1
wkui1_username As Long
wkui1_logon_domain As Long
wkui1_oth_domains As Long
wkui1_logon_server As Long
End Type
''' This is a VB-friendly version of the WKSTA_USER_INFO_1 that we'll load.
Private Type VB_WKSTA_USER_INFO_1
UserName As String
LogonDomain As String
OtherDomains As String
LogonServer As String
End Type
''' **************************************************************************
''' Module DLL declarations follow.
''' **************************************************************************
Private Declare Function GetVersionExA Lib "kernel32" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function NetWkstaUserGetInfo Lib "netapi32" (ByVal reserved As Any, ByVal Level As Long, ByRef lpBuffer As Any) As Long
Private Declare Function NetUserGetGroups Lib "netapi32" (ByRef lpServer As Any, ByRef UserName As Byte, ByVal Level As Long, ByRef lpBuffer As Long, ByVal PrefMaxLen As Long, ByRef lpEntriesRead As Long, ByRef lpTotalEntries As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal lpBuffer As Long) As Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (ByRef pTo As Any, ByRef uFrom As Any, ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Public Sub ReturnUserInfo()
Dim lIndex As Long
Dim lOffset As Long
Dim aszGroups() As String
Dim uUserInfo As VB_WKSTA_USER_INFO_1
Sheet1.Range("Clear").ClearContents
If bGetUserInfo(uUserInfo) Then
If bGetGlobalGroups(uUserInfo.UserName, uUserInfo.LogonServer, aszGroups()) Then
Sheet1.Range("Username").Value = uUserInfo.UserName
Sheet1.Range("Server").Value = uUserInfo.LogonServer
Sheet1.Range("Domain").Value = uUserInfo.LogonDomain
For lIndex = LBound(aszGroups) To UBound(aszGroups)
Sheet1.Range("Groups").Offset(lOffset, 0).Value = aszGroups(lIndex)
lOffset = lOffset + 1
Next lIndex
End If
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Comments: Get information about the currently logged on user.
'''
''' Arguments: uUserInfo [out] A variable of type VB_WKSTA_USER_INFO_1
''' that will be loaded by this procedure.
'''
''' Returns: Boolean True on success, False on Error.
'''
''' Date Developer Action
''' --------------------------------------------------------------------------
''' 05/15/05 Rob Bovey Created
'''
Private Function bGetUserInfo(ByRef uUserInfo As VB_WKSTA_USER_INFO_1) As Boolean
Dim lpBuffer As Long
Dim lReturn As Long
Dim uVerInfo As OSVERSIONINFO
Dim uAPIUserInfo As WKSTA_USER_INFO_1
On Error GoTo ErrorHandler
''' First make sure we're on Windows NT/2000.
uVerInfo.dwOSVersionInfoSize = Len(uVerInfo)
GetVersionExA uVerInfo
If uVerInfo.dwPlatformId <> VER_PLATFORM_WIN32_NT Then Err.Raise 9999, , "This application can only be run on Windows NT/2000/2003."
''' Get the information about the logged on user.
lReturn = NetWkstaUserGetInfo(0&, GET_USER_INFO_1, lpBuffer)
If lReturn = NERR_SUCCESS Then
If lpBuffer <> 0 Then
''' Retrieve the struct from the pointer returned by the API call.
CopyMem uAPIUserInfo, ByVal lpBuffer, Len(uAPIUserInfo)
''' Transfer the data into our VB-friendly structure.
uUserInfo.UserName = szPointerToStringW(uAPIUserInfo.wkui1_username)
uUserInfo.LogonDomain = szPointerToStringW(uAPIUserInfo.wkui1_logon_domain)
uUserInfo.OtherDomains = szPointerToStringW(uAPIUserInfo.wkui1_oth_domains)
uUserInfo.LogonServer = szPointerToStringW(uAPIUserInfo.wkui1_logon_server)
''' Clean up.
NetApiBufferFree lpBuffer
End If
End If
bGetUserInfo = True
Exit Function
ErrorHandler:
MsgBox Err.Description, vbCritical
bGetUserInfo = False
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Comments: Returns the list of global groups on the specified server that
''' the specified user belongs to.
'''
''' Arguments: szUserName [in] The username to return the group list for.
''' szServer [in] The server where the global groups are
''' defined.
''' aszGroups() [out] An array of global group names that the
''' specified user belongs to.
'''
''' Returns: Boolean True on success, False on Error.
'''
''' Date Developer Action
''' --------------------------------------------------------------------------
''' 05/15/05 Rob Bovey Created
'''
Private Function bGetGlobalGroups(ByRef szUserName As String, ByRef szServer As String, ByRef aszGroups() As String) As Boolean
Dim abytUserName() As Byte
Dim abytServer() As Byte
Dim lpBuffer As Long
Dim lNumRead As Long
Dim lTotal As Long
Dim lReturn As Long
Dim lIndex As Long
Dim alGroups() As Long
On Error GoTo ErrorHandler
abytUserName = szUserName & vbNullChar
If InStr(szServer, "\\") = 1 Then
abytServer = szServer & vbNullChar
Else
abytServer = "\\" & szServer & vbNullChar
End If
lReturn = NetUserGetGroups(abytServer(0), abytUserName(0), 0&, lpBuffer, MAX_PREFERRED_LENGTH, lNumRead, lTotal)
If lReturn = NERR_SUCCESS Then
If lpBuffer <> 0 Then
ReDim alGroups(0 To lNumRead - 1)
ReDim aszGroups(0 To lNumRead - 1)
''' Load the list of pointers to the group names returned by the API call.
CopyMem alGroups(0), ByVal lpBuffer, lNumRead * 4 ''' lNumRead * sizeof(long*)
''' Convert the pointers to VB strings.
For lIndex = 0 To lNumRead - 1
aszGroups(lIndex) = szPointerToStringW(alGroups(lIndex))
Next lIndex
''' We must free the memory buffer that was allocated by the call to NetUserGetLocalGroups.
NetApiBufferFree lpBuffer
End If
End If
bGetGlobalGroups = True
Exit Function
ErrorHandler:
MsgBox Err.Description, vbCritical
bGetGlobalGroups = False
End Function
''' Returns a VB string from a pointer to a Unicode string.
Private Function szPointerToStringW(ByRef lpStringW As Long) As String
Dim bytBuffer() As Byte
Dim lNumBytes As Long
If lpStringW <> 0 Then
''' The lstrlenW API returns the number of Unicode characters located at lpStringW.
''' We multiply by 2 because we need the number of bytes.
lNumBytes = lstrlenW(lpStringW) * 2
If lNumBytes > 0 Then
''' Allocate enough memory to hold the string.
ReDim bytBuffer(0 To (lNumBytes - 1)) As Byte
''' Beginning at the memory location specified by lpStringW,
''' copy the specified number of bytes into our buffer.
CopyMem bytBuffer(0), ByVal lpStringW, lNumBytes
''' VB automatically performs a Byte array to String conversion.
szPointerToStringW = bytBuffer
End If
End If
End Function
''' Returns a Long value from the specified memory location.
Private Function lPointerToDWord(ByRef lpDWord As Long) As Long
Dim lReturn As Long
If lpDWord <> 0 Then
CopyMem lReturn, ByVal lpDWord, 4
lPointerToDWord = lReturn
End If
End Function
Lần chỉnh sửa cuối: