Microsoft KB Archive/168903

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 11:29, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Article ID: 168903

Article Last Modified on 8/18/2005



APPLIES TO

  • Microsoft Messaging Application Programming Interface



This article was previously published under Q168903

SUMMARY

Using Extended MAPI, it is possible to embed a file in a message so that the contents of the file are viewed when the message is opened, embedding a bitmap into the message so that the actual bitmap appears when you open the message for example.

MORE INFORMATION

Steps to Embed a File in a Message

  1. Create an attachment for the message by calling the message's IMessage::CreateAttach method and pass NULL as the interface identifier.
  2. Call IMAPIProps::SetProps to set PR_ATTACH_METHOD to ATTACH_OLE, indicating an OLE object.
  3. Set PR_RENDERING_POSITION to indicate where the attachment should be displayed.
  4. Call IMAPIProp::OpenProperty to open the PR_ATTACH_DATA_OBJ property with an IStorage object.
  5. Use OleCreateFromFile to create the embedded IStorage object from the specified file.
  6. Call the new storage object's IStorage::Commit method.

Example

The following code creates an attachment for a message, prompts the user for a file, and then embeds that file in the message. This code assumes the message has already been created. The pointer to the message is contained in the variable pMsg.

The following .lib files should be linked:

  • Ole32.lib
  • Oleaut32.lib
  • Uuid.lib
  • Mapi32.lib

       // BEGIN CODE
       #include <windows.h>
    
       #define USES_IID_IMAPITable
       #define USES_IID_IMessage
       #define USES_IID_IMAPIStatus
       #define INITGUID
       #include <initguid.h>
    
       #include <ole2.h>
       #include <mapiguid.h>
       #include <mapiutil.h>
       #include <mapitags.h>
    
       #define USES_OID_OLE2_STORAGE
       #define INITOID
       #include <mapioid.h>
    
       LPSTORAGE     pstg = NULL;
       LPUNKNOWN     lpUnknown = NULL;
       LPATTACH      pAtt      = NULL;
       ULONG         ulAttNum;
       HRESULT       hRes;
       OPENFILENAME  ofn;
       TCHAR         szFile[MAX_PATH];
       OLECHAR       pszFile[MAX_PATH];
    
       enum {METHOD,RENDERING,NUM_ATT_PROPS};
       SPropValue    spvAttach[NUM_ATT_PROPS];
    
       ZeroMemory((LPVOID) &ofn, sizeof ofn);
    
       // Set up structure to retrieve filename
       lstrcpy(szFile,"*.*");
       ofn.lStructSize = sizeof(ofn);
       ofn.hwndOwner   = hWnd;
       ofn.lpstrFilter = "All files\0*.*\0";
       ofn.lpstrFile   = szFile;
       ofn.nMaxFile    = MAX_PATH;
       ofn.lpstrTitle  = "Attach File";
       ofn.Flags       = OFN_NONETWORKBUTTON | OFN_FILEMUSTEXIST |
                         OFN_NOCHANGEDIR     | OFN_PATHMUSTEXIST;
    
       // Prompt user for file name
       if (GetOpenFileName(&ofn))
       {
          // Create an attachment on the message
          if (FAILED(hRes = pMsg->CreateAttach(
                NULL, (ULONG)0, &ulAttNum, &pAtt)))
             goto Quit;
    
          spvAttach[METHOD].ulPropTag = PR_ATTACH_METHOD;
          spvAttach[METHOD].Value.l = ATTACH_OLE;
    
          spvAttach[RENDERING].ulPropTag = PR_RENDERING_POSITION;
          spvAttach[RENDERING].Value.l = 0;
    
          // Save the properties we have set on the attachment
          if (FAILED(hRes = pAtt -> SetProps(
                NUM_ATT_PROPS,
                (LPSPropValue)&spvAttach,
                NULL)))
             goto Quit;
    
          // PR_ATTACH_DATA_OBJ will contain the OLE object
          if (FAILED(hRes = pAtt->OpenProperty(
                PR_ATTACH_DATA_OBJ,
                (LPIID)&IID_IStorage, 0,
                MAPI_CREATE | MAPI_MODIFY,
                (LPUNKNOWN *)&pstg)))
             goto Quit;
    
          // We must convert the file name to a Wide Character string
          // for use in the OleCreateFromFile function
          MultiByteToWideChar(CP_ACP, 0, ofn.lpstrFile, -1, pszFile, 512);
    
          // Create an embedded object in the IStorage object from the
          // contents of the specified file
          if (FAILED(hRes = OleCreateFromFile(
                CLSID_NULL, pszFile, IID_IOleObject,
                OLERENDER_NONE, NULL, NULL, pstg,
                (LPVOID FAR*)&lpUnknown)))
             goto Quit;
    
          // Call the Commit Method of the IStorage Object
          if (FAILED(hRes = pstg->Commit(STGC_DEFAULT)))
             goto Quit;
    
          // Save the changes to the Attachment
          pAtt -> SaveChanges(0);
    
       Quit:
          if (pAtt)
             pAtt -> Release();
    
          if (lpUnknown)
             lpUnknown -> Release();
    
          if (pstg)
             pstg -> Release();
    
          return hRes;
    
       // END CODE
                            


Keywords: kbhowto kbmsg kbcode KB168903