Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COM Error 0x80004003 (Invalid Pointer) access MS Outlook contacts

Tags:

c++

com

atl

outlook

I am some ATL code that uses smart COM pointers to iterate through MS Outlook contacts, and on some PC's I am getting a COM error 0x80004003 ('Invalid Pointer') for each contact. The same code works fine on other PCs. The code looks like this:

_ApplicationPtr ptr;
ptr.CreateInstance(CLSID_Application);

_NameSpacePtr ns = ptr->GetNamespace(_T("MAPI"));
MAPIFolderPtr folder = ns->GetDefaultFolder(olFolderContacts);

_ItemsPtr items = folder->Items;
const long count = items->GetCount();

for (long i = 1; i <= count; i++)
{
    try
    {
        _ContactItemPtr contactitem = items->Item(i);
        // The following line throws a 0x80004003 exception on some machines
        ATLTRACE(_T("\tContact name: %s\n"), static_cast<LPCTSTR>(contactitem->FullName));
    }
    catch (const _com_error& e)
    {
        ATLTRACE(_T("%s\n"), e.ErrorMessage());
    }
}

I wonder if any other applications/add-ins could be causing this? Any help would be welcome.

like image 392
Rob Avatar asked Dec 02 '25 05:12

Rob


1 Answers

FullName is a property and you do the GET operation (it's probably something like this in IDL: get_FullName([out,retval] BSTR *o_sResult)). Such operation works ok with null values.

My assumption is that contactItem smart pointer points to any valid COM object. In such case the formatting operation done by ATLTRACE can cause the problem. Internally it behaves probably like standard sprintf("",args...) function.

To avoid such problems just do something like below:

ATLTRACE(_T("\tContact name: %s\n"),
_bstr_t(contactitem->FullName)?static_cast<LPCTSTR>(contactitem->FullName):"(Empty)")
like image 83
Aleksander Stankiewicz Avatar answered Dec 03 '25 21:12

Aleksander Stankiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!