WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, see the following Microsoft Web site:
For additional information about the support options available from Microsoft, visit the following Microsoft Web site:
To work around this problem, split your code into two separate programs.
For example, the first program should do the impersonation by using LogonUser, and then start the second program as a separate process by using CreateProcessAsUser as in the following sample Visual Basic code:
'<-- Launcher program
Const LOGON32_LOGON_INTERACTIVE = 2
Const LOGON32_PROVIDER_DEFAULT = 0
Const CREATE_DEFAULT_ERROR_MODE = &H4000000
Private Type STARTUPINFO
cb As Long
lpReserved As Long ' !!! must be Long for Unicode string
lpDesktop As Long ' !!! must be Long for Unicode string
lpTitle As Long ' !!! must be Long for Unicode string
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Declare Function CreateProcessAsUser Lib "advapi32.dll" _
Alias "CreateProcessAsUserA" _
(ByVal hToken As Long, _
ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
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
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Sub Command1_Click()
Dim hToken As Long
Dim ulResult As Long
Dim startup As STARTUPINFO
Dim process As PROCESS_INFORMATION
ulResult = LogonUser("ImpersonatedUsed", "ImpersonatedDomain", _
"ImpersonatedUserPassword", LOGON32_LOGON_INTERACTIVE, _
LOGON32_PROVIDER_DEFAULT, hToken)
If ulResult = 0 Then
MsgBox "Error in LogonUser: " & Err.LastDllError
Exit Sub
End If
startup.cb = Len(startup)
'TODO: Replace 'mail.exe' with the name of the program you wish to start
ulResult = CreateProcessAsUser(hToken, 0&, "mail.exe", 0&, 0&, _
False, CREATE_DEFAULT_ERROR_MODE, 0&, "path", startup, process)
If ulResult = 0 Then
MsgBox "Error in CreateProcessAsUser: " & Err.LastDllError
Exit Sub
End If
CloseHandle hToken
End Sub
'<-- End of first program
'<-- Start of second process
'<-- Mail.exe project:
Private Sub Command1_Click()
On Error GoTo ErrHandler
Dim oSession 'As MAPI.Session
Set oSession = CreateObject("MAPI.Session")
oSession.Logon , , , , , True, "ServerName" & vbLf & "MailboxName"
Dim oFolder 'As MAPI.Folder
Set oFolder = oSession.GetDefaultFolder(1) 'CdoDefaultFolderInbox
Set oMessages = oFolder.Messages
Set oMessage = oMessages.GetFirst
MsgBox oMessage.Subject
Set oMessage = Nothing
MsgBox "1"
Set oMessages = Nothing
MsgBox "2"
Set oFolder = Nothing
MsgBox "3"
oSession.Logoff '
MsgBox "4"
Set oSession = Nothing
MsgBox "Done"
Exit Sub
ErrHandler:
MsgBox Err.Number & " --> " & Err.Description
Set oMessage = Nothing
Set oMessages = Nothing
Set oFolder = Nothing
oSession.Logoff
Set oSession = Nothing
End Sub