tags:

views:

590

answers:

4

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.

A: 

Just a guess: Maybe the "FullName" field in the address book is empty and that's why the pointer is invalid?

hard to tell, because your code doesn't indicate which COM-interfaces you're using.

Stefan
A: 

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)"

)

Alex Stankiewicz
A: 

Does this make any difference?

ATLTRACE(_T("\tContact name: %s\n"), static_cast<LPCTSTR>(contactitem->GetFullName()));
Magnus Skog
A: 

In my example you format NULL value to a proper text value.

If the question is about the difference between FullName(as a property) and GetFullName() (as a method) then the answer is no. Property and method should give the same result. Sometimes property can be mapped to different methods then setXXX and getXXX. It can be achieved by using some specific syntax in IDL (and in reality in TLB after compilation of IDL to TLB). If property FullName is not mapped to method GetFullName then you will achieve different result.

So please examine file *.tlh after importing some type library to your project...

Alex Stankiewicz