模擬程式碼中的使用者可能會導致 Outlook 停止回應,或可能會造成方法失敗 」 錯誤訊息

文章翻譯 文章翻譯
文章編號: 821470 - 檢視此文章適用的產品。
全部展開 | 全部摺疊

徵狀

您在執行程式碼會模擬使用者存取信箱時 Microsoft Office Outlook 2007,Outlook 2003 或 Outlook 2002 可能會停止回應,或者您可能會收到類似下列的錯誤訊息:
方法 '~ '的物件' ~' 失敗

發生的原因

如果模擬使用者時,會建立新的執行緒,可能就會發生這個問題。MAPI 及共同作業資料物件 (CDO) 建立新的執行緒為正常的函式,而且這些新的執行緒不會繼承模擬的使用者語彙基元 (Token) 的完整存取。

附註 模擬與 MAPI 不受支援的行為。

其他可行方案

Microsoft 僅,為了說明提供程式設計範例,不提供任何明示或默示的保證。這包括,但不限於適售性或適合某特定用途之默示擔保責任。本文假設您已熟悉使用我們所示範的程式設計語言以及建立和偵錯程序所使用的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能。不過,不會修改這些範例以提供附加功能或建構程序,以符合您特定需求。

如果要解決這個問題,分成兩個不同的程式中的程式碼。

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

狀況說明

Microsoft 已確認<適用於>一節所列之 Microsoft 產品確實有此問題。

屬性

文章編號: 821470 - 上次校閱: 2007年4月30日 - 版次: 2.1
這篇文章中的資訊適用於:
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Office Outlook 2003
  • Microsoft Office Outlook 2007
關鍵字:?
kbmt kberrmsg kbbug kbprb KB821470 KbMtzh
機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:821470
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。

提供意見

 

Contact us for more help

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