Notes API Sample FILEATT not working

Hello,

I am trying to get the Lotus Notes sample fileatt to work properly (samples\misc\fileatt). This is a sample C application that comes with the NotesAPI.

This sample adds an attachment to a Note using 2 different methods. The first (‘easy method’) works great.

The second method (AttachFile2) creates an invalid attachment in notes (notes cannot find the file and the fileobject does not have valid information). Has anyone ever gotten this to work?

Function that creates an invalid attachment:

STATUS LNPUBLIC AttachFile2( /* using low-level FILEOBJECT routines */

DBHANDLE hDB,

NOTEHANDLE hNote)

{

char szAttachFileName[] = "attach2.txt"; /* UI-visible attachment name */

BYTE   *pObjectItemBuffer;

HANDLE hObjectItemBuffer;

DWORD  dwObjectID;

LAPI_FILE hAttachFile;

WORD *pw;

char *pchar;

DWORD dwItemSize;

FILEOBJECT objFile;

BLOCKID    bidFile;

int        i;

#if defined(UNIX)

struct  stat AttachStatBuf;

#else

struct  _stat AttachStatBuf;

#endif

TIMEDATE MyTimeDate;

STATUS sError;



hAttachFile = LAPI_OpenFile(szSourceFileName, READPERM);



if (hAttachFile == -1)

    return (ERR_NOEXIST);

#if defined(UNIX)

i = fstat(hAttachFile, &AttachStatBuf);

if (i == -1)

    return (ERR_FD);

objFile.FileSize = AttachStatBuf.st_size;

#else

i = _fstat(hAttachFile, &AttachStatBuf);

if (i == -1)

    return (ERR_FD);

objFile.FileSize = AttachStatBuf.st_size;

#endif

/*

  • Allocate memory for the object item.

*/

OSMemAlloc(0, objFile.FileSize, &hObjectItemBuffer);

pObjectItemBuffer = (BYTE *) OSLockObject(hObjectItemBuffer);



if (sError = NSFDbAllocObject(hDB,

                              objFile.FileSize,

                              NOTE_CLASS_DOCUMENT,

                              0,

                              &dwObjectID))



{

    return (ERR(sError));

}



objFile.Header.ObjectType = OBJECT_FILE;

objFile.Header.RRV = dwObjectID;

objFile.FileNameLength = strlen(szAttachFileName);

objFile.HostType = HOST_LOCAL;

objFile.CompressionType = COMPRESS_NONE;

objFile.FileAttributes = 0;

objFile.Flags = 0;



GetFileT(&AttachStatBuf.st_mtime, &MyTimeDate);



objFile.FileCreated = MyTimeDate;

objFile.FileModified = MyTimeDate;



dwItemSize = (DWORD)(sizeof(WORD) + ODSLength(_FILEOBJECT) + strlen(szAttachFileName));



if (sError = OSMemAlloc(0, dwItemSize, &bidFile.pool))

{

  NSFDbFreeObject(hDB, dwObjectID);

  return (ERR(sError));

}



bidFile.block = NULLBLOCK;



pw = OSLockBlock(WORD, bidFile);

*pw = TYPE_OBJECT;

pw++;



pchar = (char *) pw;

ODSWriteMemory (&pw, _FILEOBJECT, &objFile, 1);

pchar += ODSLength(_FILEOBJECT);

memcpy(pchar, szAttachFileName, strlen(szAttachFileName)); 



OSUnlockBlock(bidFile);



if (sError = NSFItemAppendObject(hNote,

                                 ITEM_SIGN | ITEM_SEAL | ITEM_SUMMARY,

                                 ITEM_NAME_ATTACHMENT,

                                 (WORD)strlen(ITEM_NAME_ATTACHMENT),

                                 bidFile,

                                 dwItemSize,

                                 TRUE))

{

  OSMemFree(bidFile.pool);

  return (ERR(sError));

}



OSUnlockObject(hObjectItemBuffer);



pchar = OSLock(char, hObjectItemBuffer);

#if defined (UNIX)

read(hAttachFile, pchar, (UINT)objFile.FileSize);

#else

_read(hAttachFile, pchar, (UINT)objFile.FileSize);

#endif

OSUnlockObject(hObjectItemBuffer);

sError = NSFDbWriteObject(hDB,

                          dwObjectID,

                          hObjectItemBuffer,

                          0,

                          objFile.FileSize);



OSMemFree(hObjectItemBuffer);



if (sError = NSFNoteUpdate(hNote, 0))

{

    return (ERR(sError));

}

LAPI_CloseFile(hAttachFile);



return(NOERROR);

}