C++, ATL: Access of folder content by index fails

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a very simple COM-add-in where I try to access a folder.
But the direct access by index fails:

CComPtr<Outlook::_NameSpace> NameSpace;
m_spApp->GetNamespace(L"MAPI", &NameSpace);

CComPtr<Outlook::MAPIFolder> InBoxFolder;
CComPtr<Outlook::MAPIFolder> RootFolder;
NameSpace->GetDefaultFolder(Outlook::olFolderInbox, &InBoxFolder);
InBoxFolder->get_Parent((IDispatch **)&RootFolder);
CComPtr<Outlook::_Folders> Folders;
RootFolder->get_Folders(&Folders);

Outlook::MAPIFolder * pFolder = NULL;
HRESULT hr = Folders->GetFirst(&pFolder);
ATLASSERT(hr == S_OK);
hr = Folders->Item(CComVariant(0), &pFolder);
ATLASSERT(hr == S_OK);

The second ATLASSERT() fails (out of bounds) whereas the first succeeds! Why?
 
=?Utf-8?B?Um9u?= said:
Got it. The Item property works not zero based. My god.

This is because people using the OOM are expected to be using VB, which
is 1-based. You should also be careful about assuming that 'true' = 1, and
you'll have to do some casting between string types to get things in/out
of BSTR.

See http://support.microsoft.com/kb/260626/en-us for some sample code,
though they're using #import to get the TLB info so the class names are a
bit different.

-- dan
 
Back
Top