Microsoft KB Archive/821470

= Impersonation of user in code may cause Outlook to stop responding or cause &quot;Method Failed&quot; error message =

Article ID: 821470

Article Last Modified on 4/30/2007

-

APPLIES TO


 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Office Outlook 2003
 * Microsoft Office Outlook 2007

-



SYMPTOMS
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



CAUSE
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.



WORKAROUND
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 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 &quot;advapi32.dll&quot; _ Alias &quot;CreateProcessAsUserA&quot; _ (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 &quot;advapi32.dll&quot; Alias &quot;LogonUserA&quot; ( _     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 &quot;kernel32&quot; ( _     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(&quot;ImpersonatedUsed&quot;, &quot;ImpersonatedDomain&quot;, _        &quot;ImpersonatedUserPassword&quot;, LOGON32_LOGON_INTERACTIVE, _            LOGON32_PROVIDER_DEFAULT, hToken)

If ulResult = 0 Then MsgBox &quot;Error in LogonUser: &quot; & 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&, &quot;mail.exe&quot;, 0&, 0&, _        False, CREATE_DEFAULT_ERROR_MODE, 0&, &quot;path&quot;, startup, process)

If ulResult = 0 Then MsgBox &quot;Error in CreateProcessAsUser: &quot; & 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(&quot;MAPI.Session&quot;) oSession.Logon, , , , , True, &quot;ServerName&quot; & vbLf & &quot;MailboxName&quot;

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 &quot;1&quot; Set oMessages = Nothing

MsgBox &quot;2&quot; Set oFolder = Nothing

MsgBox &quot;3&quot; oSession.Logoff '

MsgBox &quot;4&quot; Set oSession = Nothing MsgBox &quot;Done&quot;

Exit Sub ErrHandler: MsgBox Err.Number & &quot;  -->   &quot; & Err.Description Set oMessage = Nothing Set oMessages = Nothing Set oFolder = Nothing oSession.Logoff Set oSession = Nothing

End Sub



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.

Additional query words: OFFQFE 8382

Keywords: kberrmsg kbbug kbprb KB821470

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.