Article ID: 192651
Article Last Modified on 3/11/2004
try {
_SessionPtr pSession("MAPI.Session");
pSession->Logon();
FolderPtr pFolder = pSession->Inbox;
MessagesPtr pMessages = pFolder->Messages;
MessagePtr pMessage = pMessages->GetFirst();
_bstr_t body;
while (pMessage != NULL) {
body = pMessage->Text;
_tprintf("%s\n", (LPCTSTR)body);
pMessage = pMessages->GetNext();
}
} catch (_com_error &e) {
dump_com_error(e);
}
The preceding code fails on the following line because you are now
reassigning the smart pointer without first releasing the underlying
object:
pMessage = pMessages->GetNext();For the preceding code to work, that line should be replaced by the following two lines of code:
pMessage = NULL; pMessage = pMessages->GetNext();It is also a good idea to set the pointers to NULL at the end of your code. This must be done if you are going to call the CoUninitialize function in the same scope as the smart pointers.
Keywords: kbmsg kbprb kbcode KB192651