VB Code:
  1. 'in a .bas module
  2.  
  3. Option Explicit
  4. Private Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Long, ByVal dwLogonProvider As Long, phToken As Long) As Long
  5. Private Declare Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal hToken As Long) As Long
  6. Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long
  7. Private Const LOGON32_PROVIDER_DEFAULT    As Long = 0&
  8. Private Const LOGON32_PROVIDER_WINNT35    As Long = 1&
  9. Private Const LOGON32_LOGON_INTERACTIVE   As Long = 2&
  10. Private Const LOGON32_LOGON_NETWORK       As Long = 3&
  11. Private Const LOGON32_LOGON_BATCH         As Long = 4&
  12. Private Const LOGON32_LOGON_SERVICE       As Long = 5&
  13.  
  14.  
  15. Public Function doLogon(ByVal strAdminUser As String, ByVal strAdminPassword As String, ByVal strAdminDomain As String) As Boolean
  16. On Error GoTo DamnErr
  17.     Dim lngTokenHandle As Long
  18.     Dim lngLogonType As Long
  19.     Dim lngLogonProvider As Long
  20.     Dim blnResult As Boolean
  21.     lngLogonType = LOGON32_LOGON_INTERACTIVE
  22.     lngLogonProvider = LOGON32_PROVIDER_DEFAULT
  23.     blnResult = RevertToSelf()
  24.     blnResult = LogonUser(strAdminUser, strAdminDomain, strAdminPassword, _
  25.                                          lngLogonType, lngLogonProvider, _
  26.                                          lngTokenHandle)
  27.     blnResult = ImpersonateLoggedOnUser(lngTokenHandle)
  28.     doLogon = blnResult
  29.     Exit Function
  30. DamnErr:
  31.    Dim sERRORtxt As String
  32.    sERRORtxt = "Error Number: " & Err.Number & vbCrLf & _
  33.                "Description: " & Err.Description & vbCrLf & _
  34.                "Source: " & Err.Source & vbCrLf & _
  35.                "Function: doLogon" & vbCrLf & _
  36.                "Date: " & Now() & vbCrLf & _
  37.                "Input:" & vbCrLf & _
  38.                "  strAdminUser=" & strAdminUser & vbCrLf & _
  39.                "  strAdminPassword=" & strAdminPassword & vbCrLf & _
  40.                "  strAdminDomain=" & strAdminDomain
  41.    App.LogEvent sERRORtxt, vbLogEventTypeError
  42.    Err.Clear
  43. End Function
  44.  
  45. Public Function doLogoff() As Boolean
  46. On Error GoTo DamnErr
  47.     doLogoff = RevertToSelf()
  48.     Exit Function
  49. DamnErr:
  50. Dim sERRORtxt As String
  51.    sERRORtxt = "Error Number: " & Err.Number & vbCrLf & _
  52.                "Description: " & Err.Description & vbCrLf & _
  53.                "Source: " & Err.Source & vbCrLf & _
  54.                "Function: doLogoff" & vbCrLf & _
  55.                "Date: " & Now()
  56.    App.LogEvent sERRORtxt, vbLogEventTypeError
  57.    Err.Clear
  58. End Function



When you call it:

VB Code:
  1. doLogon "username", "password", "domain-if-needed"
  2. 'code here
  3. 'after tasks completed, log off from the pc
  4. doLogoff