从 c++ 通过 activex 读取 Outlook 通讯簿会随机返回未知错误

Reading outlook address book via activex from c++ returns unknown error at random

本文关键字:随机 返回 错误 未知 通过 c++ activex 读取 Outlook      更新时间:2023-10-16

我目前正在编写一个需要导入整个Outlook AddressBook的程序。该程序是用C++编写的(用gcc(MinGW)编译),我使用ActiveX连接到Outlook。

这是我的工作:

  1. 创建 Outlook.Application 的实例
  2. 获取当前会话
  3. 获取地址列表
  4. 对于地址列表中的每个地址列表:获取地址条目
  5. 对于 AddressEntry 中的每个 AddressEntry:确定它是 ContactItem、ExchangeUser 还是其他内容
  6. 如果是联系人项:读取以下属性:名字、姓氏、邮寄地址街道、邮寄地址邮政信箱、邮寄地址邮政编码、邮寄地址城市、公司名称、部门、职务、主要电话号码、其他电话号码、商务传真号码和电子邮件 1地址
  7. 如果是 ExchangeUser:读取以下属性:名字、姓氏、街道地址、邮政编码、城市、公司名称、部门、职务、商业电话号码、手机号码、地址
  8. 如果它是其他类型的地址条目,请忽略它

只要地址簿中只有几个联系人,这就像一个魅力。但现在我正在一家更大的公司中测试它,地址簿中有大约 500 个联系人(包括 ContactItems 和 ExchangeUsers),分布在 22 个不同的地址列表中。

在读取最大的地址列表(包含 180 个地址条目,所有 ExchangeUsers)期间,Outlook 的 ActiveX 组件突然返回一个未知错误(返回值 IDispatch->Invoke 0xFFFFFFFF)。它通常介于第 105 个和第 115 个 AddressEntry 之间,并且通常在读取 MobilePhoneNumber 属性期间(但如果我将其注释掉,它只会在不同的调用中失败。

知道我可能做错了什么吗?我的印象是这与大量的连续通话有关。也许每次打电话后我都应该进行某种清理?

无论如何任何帮助将不胜感激!

问候低密度字节

编辑:代码(为简洁起见,省略头文件)

1. 应用类

Contact * MSOutlookContactSource::ToContact(SP<ContactUser> contactUser) const
{
  Contact * c = new Contact;
  c->setFirstName(contactUser->GetFirstName())
    .setLastName(contactUser->GetLastName())
    .setStreet(contactUser->GetMailingAddressStreet())
    .setStreetNr(contactUser->GetMailingAddressPostOfficeBox())
    .setZip(contactUser->GetMailingAddressPostalCode())
    .setCity(contactUser->GetMailingAddressCity())
    .setCompany(contactUser->GetCompanyName())
    .setDepartment(contactUser->GetDepartment())
    .setFunction(contactUser->GetJobTitle())
    .setTel1(contactUser->GetPrimaryTelephoneNumber())
    .setTel2(contactUser->GetOtherTelephoneNumber())
    .setFax(contactUser->GetBusinessFaxNumber())
    .setEmail(contactUser->GetEmail1Address());
  return c;
}
//----------------------------------------------------------------------
Contact * MSOutlookContactSource::ToContact(SP<ExchangeUser> exchangeUser) const
{
  Contact * c = new Contact;
  c->setFirstName(exchangeUser->GetFirstName())
    .setLastName(exchangeUser->GetLastName())
    .setStreet(exchangeUser->GetStreetAddress())
    .setZip(exchangeUser->GetPostalCode())
    .setCity(exchangeUser->GetCity())
    .setCompany(exchangeUser->GetCompanyName())
    .setDepartment(exchangeUser->GetDepartment())
    .setFunction(exchangeUser->GetJobTitle())
    .setTel1(exchangeUser->GetBusinessTelephoneNumber())
    .setTel2(exchangeUser->GetMobileTelephoneNumber())
    .setEmail(exchangeUser->GetAddress());
  return c;
}
//----------------------------------------------------------------------
vector<Contact *> MSOutlookContactSource::GetAllContacts() const
{
  LOG << "GetAllContacts" << endl;
  ActiveX::Initialize();
  vector<Contact *> retval;
  SP<Application> outlookApplication(Application::Create());
  SP<Namespace> session(outlookApplication->GetSession());
  SP<AddressLists> addressLists(session->GetAddressLists());
  long numAddressLists = addressLists->GetCount();
  LOG << "Found " << numAddressLists << " addressLists" << endl;
  for (long idxAddressLists = 1; idxAddressLists <= numAddressLists; ++idxAddressLists)
    {
      LOG << "Fetching addressList " << idxAddressLists << endl;
      SP<AddressList> addressList(addressLists->Item(idxAddressLists));
      SP<AddressEntries> addressEntries(addressList->GetAddressEntries());
      long numAddressEntries = addressEntries->GetCount();
      LOG << "Found " << numAddressEntries << " addressEntries" << endl;
      for (long idxAddressEntries = 1; idxAddressEntries <= numAddressEntries; ++idxAddressEntries)
        {
          LOG << "Fetching addressEntry " << idxAddressEntries << endl;
          SP<AddressEntry> addressEntry(addressEntries->Item(idxAddressEntries));
          SP<ContactUser> contactUser(addressEntry->GetContact());
          if (contactUser->IsNull())
            {
              SP<ExchangeUser> exchangeUser(addressEntry->GetExchangeUser());
              if (!exchangeUser->IsNull())
                {
                  LOG << "It's an ExchangeUser" << endl;
                  retval.push_back(ToContact(exchangeUser));
                }
              else
                LOG << "I don't know what it is => skipping" << endl;
            }
          else
            {
              LOG << "It's a ContactUser" << endl;
              retval.push_back(ToContact(contactUser));
            }
        }
    }
  ActiveX::Uninitialize();
  unsigned num_found = retval.size();
  LOG << "Found " << num_found << " contacts" << endl;
  return retval;
}
//----------------------------------------------------------------------

2. 域对象(示例)。其他域对象的实现方式类似

ExchangeUser::ExchangeUser() : 
  ActiveXProxy(NULL)
{
}
//----------------------------------------------------------------------
ExchangeUser::ExchangeUser(IDispatch * parent) : 
  ActiveXProxy(parent)
{
}
//----------------------------------------------------------------------
ExchangeUser::~ExchangeUser()
{
}
//----------------------------------------------------------------------
ExchangeUser::ExchangeUser(const ExchangeUser & to_copy) : 
  ActiveXProxy(to_copy)
{
}
//----------------------------------------------------------------------
ExchangeUser & ExchangeUser::operator=(const ExchangeUser & to_copy)
{
  if (&to_copy != this)
    {
      *((ActiveXProxy *)this) = to_copy;
    }
  return *this;
}
//----------------------------------------------------------------------
bool ExchangeUser::IsNull()
{
  return _parent == NULL;
}
//----------------------------------------------------------------------
string ExchangeUser::GetFirstName()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"FirstName", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetLastName()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"LastName", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetStreetAddress()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"StreetAddress", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetPostalCode()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"PostalCode", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetCity()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"City", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetCompanyName()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"CompanyName", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetDepartment()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"Department", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetJobTitle()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"JobTitle", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetBusinessTelephoneNumber()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"BusinessTelephoneNumber", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetMobileTelephoneNumber()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"MobileTelephoneNumber", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------
string ExchangeUser::GetAddress()
{
  wstring wstr(ActiveX::GetProperty(_parent, L"Address", 0).bstrVal);
  string str(wstr.size(), ' ');
  copy(wstr.begin(), wstr.end(), str.begin());
  return str;
}
//----------------------------------------------------------------------

3. ActiveXProxy 基类(_parent是一个 IDispatch *)

ActiveXProxy::ActiveXProxy() : 
  _parent(NULL)
{
}
//----------------------------------------------------------------------
ActiveXProxy::ActiveXProxy(IDispatch * parent) : 
  _parent(parent)
{
}
//----------------------------------------------------------------------
ActiveXProxy::~ActiveXProxy()
{
  if (_parent != NULL)
    {
      _parent->Release();
      _parent = NULL;
    }
}
//----------------------------------------------------------------------
ActiveXProxy::ActiveXProxy(const ActiveXProxy & to_copy) : 
  _parent(to_copy._parent)
{
}
//----------------------------------------------------------------------
ActiveXProxy & ActiveXProxy::operator=(const ActiveXProxy & to_copy)
{
  if (&to_copy != this)
    {
      _parent = to_copy._parent;
    }
  return *this;
}
//----------------------------------------------------------------------

4. ActiveX 实用程序类

map<HRESULT, string> ActiveX::_errorTranslations;
unsigned ActiveX::_numInits = 0;
//----------------------------------------------------------------------
void ActiveX::Initialize()
{
  if (_numInits == 0)
    {
      CoInitialize(NULL);      
      _errorTranslations[DISP_E_BADPARAMCOUNT] = "DISP_E_BADPARAMCOUNT";
      _errorTranslations[DISP_E_BADVARTYPE] = "DISP_E_BADVARTYPE";
      _errorTranslations[DISP_E_EXCEPTION] = "DISP_E_EXCEPTION";
      _errorTranslations[DISP_E_MEMBERNOTFOUND] = "DISP_E_MEMBERNOTFOUND";
      _errorTranslations[DISP_E_NONAMEDARGS] = "DISP_E_NONAMEDARGS";
      _errorTranslations[DISP_E_OVERFLOW] = "DISP_E_OVERFLOW";
      _errorTranslations[DISP_E_PARAMNOTFOUND] = "DISP_E_PARAMNOTFOUND";
      _errorTranslations[DISP_E_TYPEMISMATCH] = "DISP_E_TYPEMISMATCH";
      _errorTranslations[DISP_E_UNKNOWNINTERFACE] = "DISP_E_UNKNOWNINTERFACE";
      _errorTranslations[DISP_E_UNKNOWNLCID ] = "DISP_E_UNKNOWNLCID ";
      _errorTranslations[DISP_E_PARAMNOTOPTIONAL] = "DISP_E_PARAMNOTOPTIONAL";
    }
  _numInits++;
}
//----------------------------------------------------------------------
void ActiveX::Uninitialize()
{
  if (_numInits > 0)
    {
      _numInits--;
      if (_numInits == 0)
        CoUninitialize();
    }
}
//----------------------------------------------------------------------
VARIANT ActiveX::GetProperty(IDispatch * from, LPOLESTR olePropertyName, int cArgs...)
{
  va_list marker;
  va_start(marker, cArgs);
  VARIANT *pArgs = new VARIANT[cArgs+1];
  char name[256];
  WideCharToMultiByte(CP_ACP, 0, olePropertyName, -1, name, 256, NULL, NULL);
  string propertyName(name);
  DISPID dispId;
  HRESULT hr = from->GetIDsOfNames(IID_NULL, &olePropertyName, 1, LOCALE_USER_DEFAULT, &dispId);
  if (SUCCEEDED(hr))
    {
      // Extract arguments...
      for(int i=0; i<cArgs; i++) 
        pArgs[i] = va_arg(marker, VARIANT);
      // Build DISPPARAMS
      DISPPARAMS dispParams = { NULL, NULL, 0, 0 };
      dispParams.cArgs = cArgs;
      dispParams.rgvarg = pArgs;
      EXCEPINFO excepInfo;
      VARIANT vProperty;
      hr = from->Invoke(dispId, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &vProperty, &excepInfo, NULL);
      if (SUCCEEDED(hr))
        {
          va_end(marker);
          delete [] pArgs;
          return vProperty;
        }
      else
        {
          va_end(marker);
          delete [] pArgs;
          stringstream errorMessage;
          errorMessage << "Failed to Invoke property-get on " << propertyName << " (" << hr << " - " << TranslateError(hr);
          if (hr == DISP_E_EXCEPTION)
            errorMessage << " exception: " << excepInfo.wCode << " - " << excepInfo.bstrDescription;
          errorMessage << " )";
          throw ActiveXException(errorMessage.str());
        }
    }
  else
    {
      va_end(marker);
      delete [] pArgs;
      throw ActiveXException(string("Failed to get DISPID of property ") + propertyName);
    }
}
//----------------------------------------------------------------------
VARIANT ActiveX::CallMethod(IDispatch * on, LPOLESTR oleMethodName, int cArgs...)
{
  va_list marker;
  va_start(marker, cArgs);
  VARIANT *pArgs = new VARIANT[cArgs+1];
  char name[256];
  WideCharToMultiByte(CP_ACP, 0, oleMethodName, -1, name, 256, NULL, NULL);
  string methodName(name);
  DISPID dispId;
  HRESULT hr = on->GetIDsOfNames(IID_NULL, &oleMethodName, 1, LOCALE_USER_DEFAULT, &dispId);
  if (SUCCEEDED(hr))
    {
      // Extract arguments...
      for(int i=0; i<cArgs; i++) 
        pArgs[i] = va_arg(marker, VARIANT);
      // Build DISPPARAMS
      DISPPARAMS dp = { NULL, NULL, 0, 0 };
      dp.cArgs = cArgs;
      dp.rgvarg = pArgs;
      // Make the call!
      EXCEPINFO excepInfo;
      VARIANT result;
      hr = on->Invoke(dispId, IID_NULL, LOCALE_SYSTEM_DEFAULT, 
                      DISPATCH_METHOD, &dp, &result, &excepInfo, NULL);
      if(SUCCEEDED(hr))
        return result;
      else
        {
          va_end(marker);
          delete [] pArgs;
          stringstream errorMessage;
          errorMessage << "Failed to call method " << methodName << " (" << hr << " - " << TranslateError(hr);
          if (hr == DISP_E_EXCEPTION)
            errorMessage << " exception: " << excepInfo.wCode << " - " << excepInfo.bstrDescription;
          errorMessage << " )";
          throw ActiveXException(errorMessage.str());
        }
    }
  else
    {
      va_end(marker);
      delete [] pArgs;
      throw ActiveXException(string("Failed to get DISPID of method ") + methodName);
    }
}
//----------------------------------------------------------------------
string ActiveX::TranslateError(HRESULT error)
{
  return _errorTranslations[error];
}
//----------------------------------------------------------------------

ActiveX 是一个纯静态类

编辑2:看起来这不是一个与C++相关的问题。我想确保释放资源不是问题所在。所以我在 VBS 中复制了我的代码(射击我):

Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists("out.log") Then
   objFSO.CreateTextFile("out.log")
End If
Set objLog = objFSO.OpenTextFile("out.log", 2, True)
Set objOutlook = CreateObject("Outlook.Application")
Set objSession = objOutlook.Session
Set objAddressLists = objSession.AddressLists
objLog.WriteLine("--- Found " + CStr(objAddressLists.Count) + " AddressLists")
For i = 1 To objAddressLists.Count
    objLog.WriteLine("--- AddressList " + CStr(i))
    Set objAddressList = objAddressLists.Item(i)
    objLog.WriteLine("+++ AddressListName = " + objAddressList.Name)
    Set objAddressEntries = objAddressList.AddressEntries
    objLog.WriteLine("--- AddressList has " + CStr(objAddressEntries.Count) + " AddressEntries")
    For j = 1 To objAddressEntries.Count
      objLog.WriteLine("--- AddressEntry " + CStr(i) + "." + CStr(j))
      Set objAddressEntry = objAddressEntries.Item(j)    
      If objAddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
        Set objExchangeUser = objAddressEntry.GetExchangeUser()
        objLog.WriteLine("Exchangeuser: " + _
                         objExchangeUser.FirstName + "|" + _
                         objExchangeUser.LastName + "|" + _
                         objExchangeUser.StreetAddress + "|" + _
                         objExchangeUser.PostalCode + "|" + _
                         objExchangeUser.City + "|" + _
                         objExchangeUser.CompanyName + "|" + _
                         objExchangeUser.Department + "|" + _
                         objExchangeUser.JobTitle + "|" + _
                         objExchangeUser.BusinessTelephoneNumber + "|" + _
                         objExchangeUser.MobileTelephoneNumber + "|" + _
                         objExchangeUser.Address)
        Set objExchangeUser = Nothing
      ElseIf objAddressEntry.AddressEntryUserType = olOutlookContactAddressEntry Then
        Set objContact = objAddressEntry.GetContact()
        objLog.WriteLine("Contactuser: " + _
                         objContact.FirstName + "|" + _
                         objContact.LastName + "|" + _
                         objContact.MailingAddressStreet + "|" + _
                         objContact.MailingAddressPostOfficeBox + "|" + _
                         objContact.MailingAddressPostalCode + "|" + _
                         objContact.MailingAddressCity + "|" + _
                         objContact.CompanyName + "|" + _
                         objContact.Department + "|" + _
                         objContact.JobTitle + "|" + _
                         objContact.PrimaryTelephoneNumber + "|" + _
                         objContact.OtherTelephoneNumber + "|" + _
                         objContact.BusinessFaxNumber + "|" + _
                         objContact.Email1Address)
        Set objContact = Nothing
      End If
      Set objAddressEntry = Nothing
    Next
    Set objAddressEntries = Nothing
    Set objAddressList = Nothing
Next
objTextFile.Close
MsgBox "Done"

你猜怎么着:我也收到了错误!!只有VBS似乎给了我一些额外的信息:有时我会得到:

错误:服务器引发异常代码:80010105来源:(空)

有时我会得到:

错误:远程过程调用失败代码: 800706BE来源:(空)

另外:错误后,Outlook 完全崩溃:-|

帮助!

您需要让位于调度程序,以便宏不会终止。有几种方法可以解决这个问题,但我发现将DoEventsSleep()结合起来似乎可以解决这个问题。

这可能需要根据您的环境设置和性能需求进行一些调整。

屈服钩

For j = 1 To objAddressEntries.Count
    ' allow other events to trigger
    For intWait = 1 To 15000
        DoEvents
    Next  
    ' Yield to other events      
    If (j Mod 50 = 0) Then
        Sleep (5000)
    End If
    ' process address entry
    '...
Next

睡眠依赖(位于模块子组件的顶部

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

错误是RPC_S_CALL_FAILED和RPC_E_SERVERFAULT。这表示您打开的对象过多并且 RPC 通道用完(限制由 Exchange 服务器施加)。一旦使用完所有 Outlook 对象,请立即释放它们,并避免使用多个点表示法(这会创建无法显式引用和释放的隐式变量)。