Microsoft KB Archive/173353: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "<" to "<")
m (Text replacement - "&" to "&")
 
(One intermediate revision by the same user not shown)
Line 78: Line 78:
           #define INITGUID
           #define INITGUID


           #include <initguid.h&gt;
           #include <initguid.h>
           #include <mapiutil.h&gt;
           #include <mapiutil.h>
   */  
   */  


Line 91: Line 91:
       ULONG cVals = 0;
       ULONG cVals = 0;


       if (FAILED(hRes = pMsg-&gt;GetProps((LPSPropTagArray) &amp;g_sptMsgProps,
       if (FAILED(hRes = pMsg->GetProps((LPSPropTagArray) &g_sptMsgProps,
                                       0,
                                       0,
                                       &amp;cVals,
                                       &cVals,
                                       &amp;pProps)))
                                       &pProps)))
         goto Quit;
         goto Quit;
       else
       else
         hRes = S_OK;
         hRes = S_OK;


       if (PR_HASATTACH == pProps[0].ulPropTag &amp;&amp; pProps[0].Value.b)
       if (PR_HASATTACH == pProps[0].ulPropTag && pProps[0].Value.b)
       {
       {
         LPMAPITABLE pAttTbl = NULL;
         LPMAPITABLE pAttTbl = NULL;
Line 106: Line 106:
                                                   PR_ATTACH_NUM};
                                                   PR_ATTACH_NUM};


         if (SUCCEEDED(hRes = pMsg -&gt; OpenProperty(PR_MESSAGE_ATTACHMENTS,
         if (SUCCEEDED(hRes = pMsg -> OpenProperty(PR_MESSAGE_ATTACHMENTS,
                                                   &amp;IID_IMAPITable,
                                                   &IID_IMAPITable,
                                                   0,
                                                   0,
                                                   0,
                                                   0,
                                                   (LPUNKNOWN *) &amp;pAttTbl)))
                                                   (LPUNKNOWN *) &pAttTbl)))
         {
         {


           if (SUCCEEDED(hRes = pAttTbl -&gt; SetColumns(
           if (SUCCEEDED(hRes = pAttTbl -> SetColumns(
                                               (LPSPropTagArray) &amp;sptCols,
                                               (LPSPropTagArray) &sptCols,
                                               TBL_BATCH)))
                                               TBL_BATCH)))
           {
           {


             if (SUCCEEDED(hRes = HrQueryAllRows(pAttTbl,
             if (SUCCEEDED(hRes = HrQueryAllRows(pAttTbl,
                                               (LPSPropTagArray) &amp;sptCols,
                                               (LPSPropTagArray) &sptCols,
                                               NULL,
                                               NULL,
                                               NULL,
                                               NULL,
                                               0,
                                               0,
                                               &amp;pRows)))
                                               &pRows)))
             {
             {


               for (ULONG i = 0; i < pRows -&gt; cRows; i++)
               for (ULONG i = 0; i < pRows -> cRows; i++)
               {
               {
                 LPATTACH lpAttach = NULL;
                 LPATTACH lpAttach = NULL;
Line 132: Line 132:
                 // Verify we received a filename from GetProps
                 // Verify we received a filename from GetProps
                 if (! PR_ATTACH_FILENAME ==
                 if (! PR_ATTACH_FILENAME ==
                       pRows-&gt;aRow[i].lpProps[0].ulPropTag)
                       pRows->aRow[i].lpProps[0].ulPropTag)
                   break;
                   break;


                 // Verify we received an Attachment Index from GetProps
                 // Verify we received an Attachment Index from GetProps
                 if (! PR_ATTACH_NUM == pRows-&gt;aRow[i].lpProps[1].ulPropTag)
                 if (! PR_ATTACH_NUM == pRows->aRow[i].lpProps[1].ulPropTag)
                   break;
                   break;


                 // Open the attachment
                 // Open the attachment
                 if (SUCCEEDED(hRes = pMsg-&gt;OpenAttach (
                 if (SUCCEEDED(hRes = pMsg->OpenAttach (
                                   pRows-&gt;aRow[i].lpProps[1].Value.l,
                                   pRows->aRow[i].lpProps[1].Value.l,
                                   NULL, MAPI_BEST_ACCESS, &amp;lpAttach)))
                                   NULL, MAPI_BEST_ACCESS, &lpAttach)))
                 {
                 {
                   LPSTREAM pStrmSrc = NULL, pStrmDest = NULL;
                   LPSTREAM pStrmSrc = NULL, pStrmDest = NULL;
Line 149: Line 149:
                   // Open the property of the attachment
                   // Open the property of the attachment
                   // containing the file data
                   // containing the file data
                   if (FAILED(hRes = lpAttach-&gt;OpenProperty(
                   if (FAILED(hRes = lpAttach->OpenProperty(
                                                 PR_ATTACH_DATA_BIN,
                                                 PR_ATTACH_DATA_BIN,
                                                 (LPIID)&amp;IID_IStream,
                                                 (LPIID)&IID_IStream,
                                                 0,
                                                 0,
                                                 MAPI_MODIFY,
                                                 MAPI_MODIFY,
                                                 (LPUNKNOWN *)&amp;pStrmSrc)))
                                                 (LPUNKNOWN *)&pStrmSrc)))
                     break;
                     break;


Line 164: Line 164:
                                     MAPIFreeBuffer,
                                     MAPIFreeBuffer,
                                     STGM_CREATE | STGM_READWRITE,
                                     STGM_CREATE | STGM_READWRITE,
                                     pRows-&gt;aRow[i].lpProps[0].Value.lpszA,
                                     pRows->aRow[i].lpProps[0].Value.lpszA,
                                     NULL,
                                     NULL,
                                     &amp;pStrmDest)))
                                     &pStrmDest)))
                     break;
                     break;


                   pStrmSrc -&gt; Stat(&amp;StatInfo, STATFLAG_NONAME);
                   pStrmSrc -> Stat(&StatInfo, STATFLAG_NONAME);


                   hRes = pStrmSrc -&gt; CopyTo(pStrmDest,
                   hRes = pStrmSrc -> CopyTo(pStrmDest,
                                             StatInfo.cbSize,
                                             StatInfo.cbSize,
                                             NULL,
                                             NULL,
Line 177: Line 177:


                   // Commit changes to new stream
                   // Commit changes to new stream
                   pStrmDest -&gt; Commit(0);
                   pStrmDest -> Commit(0);


                   // Release each of our streams
                   // Release each of our streams
                   pStrmDest -&gt; Release();
                   pStrmDest -> Release();
                   pStrmSrc -&gt; Release();
                   pStrmSrc -> Release();


                 }
                 }


                 // Release the attachment
                 // Release the attachment
                 lpAttach -&gt; Release();
                 lpAttach -> Release();
               }
               }
             }
             }
Line 195: Line 195:


         if (pAttTbl)
         if (pAttTbl)
           pAttTbl -&gt; Release();
           pAttTbl -> Release();
       }
       }
     Quit:
     Quit:

Latest revision as of 11:30, 21 July 2020

Knowledge Base


Article ID: 173353

Article Last Modified on 8/18/2005



APPLIES TO

  • Microsoft Messaging Application Programming Interface



This article was previously published under Q173353

SUMMARY

The following code example demonstrates how to open an attachment and copy the contents to a file in the current directory.

MORE INFORMATION

Use the following steps to open an attachment and copy the contents to a file in the current directory:

  1. Open the Attachments table, including the following columns:


PR_ATTACH_FILENAME and PR_ATTACH_NUM.

  1. Open an attachment in the Attachments table.
  2. Create a file for the attachment.
  3. Copy the contents of the attachment to the newly created file.

Sample Code

   /* This code assumes that you already have an IMessage object,
      pointed to by the variable pMsg. This IMessage object
      should have been opened with the MAPI_MODIFY flag.

      This code also assumes that PR_ATTACH_METHOD equals
      ATTACH_BY_VALUE. See the note that follows this sample 
      code for more information.
   */ 

   /* You should have the following define and include lines for this
      code to compile and link correctly:

          #define USES_IID_IMAPITable
          #define INITGUID

          #include <initguid.h>
          #include <mapiutil.h>
   */ 

    HRESULT WriteAttachToFile ( LPMESSAGE pMsg )
    {
      SizedSPropTagArray(1,g_sptMsgProps) = {1,
                                             PR_HASATTACH};

      LPSPropValue pProps = NULL;
      HRESULT hRes = 0;
      ULONG cVals = 0;

      if (FAILED(hRes = pMsg->GetProps((LPSPropTagArray) &g_sptMsgProps,
                                       0,
                                       &cVals,
                                       &pProps)))
        goto Quit;
      else
        hRes = S_OK;

      if (PR_HASATTACH == pProps[0].ulPropTag && pProps[0].Value.b)
      {
        LPMAPITABLE pAttTbl = NULL;
        LPSRowSet pRows = NULL;
        static SizedSPropTagArray(2,sptCols) = {2,PR_ATTACH_LONG_FILENAME,
                                                  PR_ATTACH_NUM};

        if (SUCCEEDED(hRes = pMsg -> OpenProperty(PR_MESSAGE_ATTACHMENTS,
                                                  &IID_IMAPITable,
                                                  0,
                                                  0,
                                                  (LPUNKNOWN *) &pAttTbl)))
        {

          if (SUCCEEDED(hRes = pAttTbl -> SetColumns(
                                              (LPSPropTagArray) &sptCols,
                                              TBL_BATCH)))
          {

            if (SUCCEEDED(hRes = HrQueryAllRows(pAttTbl,
                                              (LPSPropTagArray) &sptCols,
                                              NULL,
                                              NULL,
                                              0,
                                              &pRows)))
            {

              for (ULONG i = 0; i < pRows -> cRows; i++)
              {
                LPATTACH lpAttach = NULL;

                // Verify we received a filename from GetProps
                if (! PR_ATTACH_FILENAME ==
                      pRows->aRow[i].lpProps[0].ulPropTag)
                  break;

                // Verify we received an Attachment Index from GetProps
                if (! PR_ATTACH_NUM == pRows->aRow[i].lpProps[1].ulPropTag)
                   break;

                // Open the attachment
                if (SUCCEEDED(hRes = pMsg->OpenAttach (
                                  pRows->aRow[i].lpProps[1].Value.l,
                                  NULL, MAPI_BEST_ACCESS, &lpAttach)))
                {
                  LPSTREAM pStrmSrc = NULL, pStrmDest = NULL;
                  STATSTG StatInfo;

                  // Open the property of the attachment
                  // containing the file data
                  if (FAILED(hRes = lpAttach->OpenProperty(
                                                 PR_ATTACH_DATA_BIN,
                                                 (LPIID)&IID_IStream,
                                                 0,
                                                 MAPI_MODIFY,
                                                 (LPUNKNOWN *)&pStrmSrc)))
                     break;

                   // Open an IStream interface and create the file at the
                   // same time. This code will create the file in the
                   // current directory.
                   if (FAILED(hRes = OpenStreamOnFile(
                                     MAPIAllocateBuffer,
                                     MAPIFreeBuffer,
                                     STGM_CREATE | STGM_READWRITE,
                                     pRows->aRow[i].lpProps[0].Value.lpszA,
                                     NULL,
                                     &pStrmDest)))
                     break;

                   pStrmSrc -> Stat(&StatInfo, STATFLAG_NONAME);

                   hRes = pStrmSrc -> CopyTo(pStrmDest,
                                             StatInfo.cbSize,
                                             NULL,
                                             NULL);

                   // Commit changes to new stream
                   pStrmDest -> Commit(0);

                   // Release each of our streams
                   pStrmDest -> Release();
                   pStrmSrc -> Release();

                 }

                 // Release the attachment
                 lpAttach -> Release();
              }
            }
          }
        }

        FreeProws(pRows);

        if (pAttTbl)
          pAttTbl -> Release();
      }
    Quit:
        MAPIFreeBuffer((LPVOID) pProps);
        return hRes;
    }
                

NOTE: If the attachment is a message or structured storage (OLE object), there is no PR_ATTACH_DATA_BIN property. Another attachment property, PR_ATTACH_METHOD, essentially can contain four different values that tell you what to do:

  • ATTACH_BY_VALUE means that you work with the PR_ATTACH_DATA_BIN property.
  • ATTACH_BY_REFERENCE means that you work with PR_ATTACH_PATHNAME; the file is not physically included.
  • ATTACH_EMBEDDED_MESSAGE or ATTACH_OLE mean you call OpenProperty on PR_ATTACH_DATA_OBJ, which gives you back an IMessage or IStorage interface respectively.

For more information on the PR_ATTACH_METHOD property, see the following Microsoft Developer Network (MSDN) Web site:

Keywords: kbhowto kbmsg KB173353