Impersonation of user in code may cause Outlook to stop responding or cause "Method Failed" error message

Article translations Article translations
Article ID: 821470 - View products that this article applies to.
This article has been archived. It is offered "as is" and will no longer be updated.
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all


When you run code that impersonates a user to access a mailbox, Microsoft Office Outlook 2007, Outlook 2003, or Outlook 2002 may stop responding or you may receive an error message that is similar to the following:
Method '~' of object '~' failed


This problem may occur if new threads are created when you impersonate a user. MAPI and Collaboration Data Objects (CDO) create new threads as a normal function, and these new threads do not inherit full access to the impersonated users token.

Note Impersonation with MAPI is not a supported behavior.


Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

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


   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


   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
   MsgBox Err.Number & "   -->   " & Err.Description
   Set oMessage = Nothing
   Set oMessages = Nothing
   Set oFolder = Nothing
   Set oSession = Nothing

End Sub


Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.


Article ID: 821470 - Last Review: January 11, 2015 - Revision: 3.0
Applies to
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Office Outlook 2003
  • Microsoft Office Outlook 2007
kbnosurvey kbarchive kberrmsg kbbug kbprb KB821470

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from