^ This function is an advanced function that uses Application Programming Interface (API),which we 0 will review in Chapter 22,"Windows Application Programming Interface (API)."

This first section (Private declarations) must be at the top of the module.

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _ (ByVal lpName As String, ByVal lpUserName As String, _ lpnLength As Long) As Long Private Const NO_ERROR = 0 Private Const ERROR_NOT_CONNECTED = 2250& Private Const ERROR_MORE_DATA = 234 Private Const ERROR_NO_NETWORK = 1222& Private Const ERROR_EXTENDED_ERROR = 1208& Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&

The following section of code can be placed anywhere in the module as long as it is below the previous section.

Function WinUsername() As String 'variables

Dim strBuf As String, lngUser As Long, strUn As String 'clear buffer for user name from api func strBuf = Space$(255)

'use api func WNetGetUser to assign user value to lngUser

'will have lots of blank space lngUser = WNetGetUser("", strBuf, 255)

'if no error from function call

If lngUser = NO_ERROR Then

'clear out blank space in strBuf and assign val to function strUn = Left(strBuf, InStr(strBuf, vbNullChar) - 1) WinUsername = strUn


'error, give up

WinUsername = "Error :" & lngUser End If End Function

Function example:

Sub CheckUserRights() Dim UserName As String UserName = WinUsername Select Case UserName

Case "Administrator"

MsgBox "Full Rights" Case "Guest"

MsgBox "You cannot make changes"

Case Else

MsgBox "Limited Rights" End Select End Sub

0 0

Post a comment