模拟代码中的用户可能会使 Outlook 在停止响应或导致方法失败的错误消息

文章翻译 文章翻译
文章编号: 821470 - 查看本文应用于的产品
展开全部 | 关闭全部

症状

您在运行代码的模拟来访问邮箱的用户时 Microsoft Office Outlook 2007,Outlook 2003 或 Outlook 2002 可能会停止响应或者您可能会收到与以下类似的错误消息:
方法 ~ 的对象 ~ 失败

原因

如果模拟用户时,会创建新线程,则可能会出现此问题。MAPI 和协作数据对象 (CDO) 创建新线程作为一个正常的函数,这些新的线程不会继承被模拟的用户令牌的完全访问权限。

注意 模拟与 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 标准版
  • Microsoft Office Outlook 2003
  • Microsoft Office Outlook 2007
关键字:?
kbmt kberrmsg kbbug kbprb KB821470 KbMtzh
机器翻译
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。
点击这里察看该文章的英文版: 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