如何读取直接使用 VC + + 的复合文档属性

注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。

点击这里察看该文章的英文版: 186898
本文已归档。它按“原样”提供,并且不再更新。
概要
您可以从使用标准的接口没有运行或甚至在安装该服务器的文档中检索复合文档属性。例如,您可以检索内置文档属性 (如 Microsoft Office 97 文档的作者、 上一次修改时间和页数属性,以及其他自定义文档属性。
更多信息
下面的步骤说明如何构建 Microsoft Visual c + + 的复合文档属性查看器。本示例是一个 Win32 控制台应用程序项目,并可对其进行修改以满足您的需要。

创建示例的步骤

  1. 创建一个新的 Win32 控制台应用程序项目,并调用它 PropDump。
  2. 添加新的文件称为 main.cpp 到您的项目。
  3. 将下面的代码复制到 main.cpp:
       #include <stdio.h>   #include <windows.h>   #include <ole2.h>   #include <locale.h>   // Dumps simple PROPVARIANT values.   void DumpPropVariant(PROPVARIANT *pPropVar) {      // Don't iterate arrays, just inform as an array.      if(pPropVar->vt & VT_ARRAY) {         printf("(Array)\n");         return;      }      // Don't handle byref for simplicity, just inform byref.      if(pPropVar->vt & VT_BYREF) {         printf("(ByRef)\n");         return;       }      // Switch types.      switch(pPropVar->vt) {      case VT_EMPTY:         printf("(VT_EMPTY)\n");         break;      case VT_NULL:         printf("(VT_NULL)\n");         break;      case VT_BLOB:         printf("(VT_BLOB)\n");         break;      case VT_BOOL:         printf("%s (VT_BOOL)\n",         pPropVar->boolVal ? "TRUE/YES" : "FALSE/NO");         break;      case VT_I2: // 2-byte signed int.         printf("%d (VT_I2)\n", (int)pPropVar->iVal);         break;      case VT_I4: // 4-byte signed int.         printf("%d (VT_I4)\n", (int)pPropVar->lVal);         break;      case VT_R4: // 4-byte real.         printf("%.2lf (VT_R4)\n", (double)pPropVar->fltVal);         break;      case VT_R8: // 8-byte real.         printf("%.2lf (VT_R8)\n", (double)pPropVar->dblVal);         break;         case VT_BSTR: // OLE Automation string.         {            // Translate into ASCII.            char dbcs[1024];            char *pbstr = (char *)pPropVar->bstrVal;            int i = wcstombs(            dbcs, pPropVar->bstrVal, *((DWORD *)(pbstr-4)));            dbcs[i] = 0;            printf("%s (VT_BSTR)\n", dbcs);         }         break;      case VT_LPSTR: // Null-terminated string.         {         printf("%s (VT_LPSTR)\n", pPropVar->pszVal);         }         break;      case VT_FILETIME:         {            char *dayPre[] =                         {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};            FILETIME lft;            FileTimeToLocalFileTime(&pPropVar->filetime, &lft);                SYSTEMTIME lst;            FileTimeToSystemTime(&lft, &lst);            printf("%02d:%02d.%02d %s, %s %02d/%02d/%d (VT_FILETIME)\n",               1+(lst.wHour-1)%12, lst.wMinute, lst.wSecond,               (lst.wHour>=12) ? "pm" : "am",               dayPre[lst.wDayOfWeek%7],               lst.wMonth, lst.wDay, lst.wYear);         }         break;      case VT_CF: // Clipboard format.         printf("(Clipboard format)\n");         break;      default: // Unhandled type, consult wtypes.h's VARENUM structure.         printf("(Unhandled type: 0x%08lx)\n", pPropVar->vt);         break;      }   }   // Dump's built-in properties of a property storage.   void DumpBuiltInProps(IPropertySetStorage *pPropSetStg) {      printf("\n==================================================\n");      printf("BuiltInProperties Properties...\n");      printf("==================================================\n");      IPropertyStorage *pPropStg = NULL;      HRESULT hr;      // Open summary information, getting an IpropertyStorage.      hr = pPropSetStg->Open(FMTID_SummaryInformation,      STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);      //hr = pPropSetStg->Open(FMTID_UserDefinedProperties,         //STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);      if(FAILED(hr)) {         printf("No Summary-Information.\n");         return;      }      // Array of PIDSI's you are interested in.      struct pidsiStruct {         char *name;         long pidsi;      } pidsiArr[] = {         {"Title",            PIDSI_TITLE}, // VT_LPSTR         {"Subject",          PIDSI_SUBJECT}, // ...         {"Author",           PIDSI_AUTHOR},         {"Keywords",         PIDSI_KEYWORDS},         {"Comments",         PIDSI_COMMENTS},         {"Template",         PIDSI_TEMPLATE},         {"LastAuthor",       PIDSI_LASTAUTHOR},         {"Revision Number",  PIDSI_REVNUMBER},         {"Edit Time",        PIDSI_EDITTIME}, // VT_FILENAME (UTC)         {"Last printed",     PIDSI_LASTPRINTED}, // ...         {"Created",          PIDSI_CREATE_DTM},         {"Last Saved",       PIDSI_LASTSAVE_DTM},         {"Page Count",       PIDSI_PAGECOUNT}, // VT_I4         {"Word Count",       PIDSI_WORDCOUNT}, // ...         {"Char Count",       PIDSI_CHARCOUNT},         {"Thumpnail",        PIDSI_THUMBNAIL}, // VT_CF         {"AppName",          PIDSI_APPNAME}, // VT_LPSTR         {"Doc Security",     PIDSI_DOC_SECURITY}, // VT_I4         {0, 0}      };      // Count elements in pidsiArr.      int nPidsi = 0;      for(nPidsi=0; pidsiArr[nPidsi].name; nPidsi++);      // Initialize PROPSPEC for the properties you want.      PROPSPEC *pPropSpec = new PROPSPEC [nPidsi];      PROPVARIANT *pPropVar = new PROPVARIANT [nPidsi];      for(int i=0; i<nPidsi; i++) {         ZeroMemory(&pPropSpec[i], sizeof(PROPSPEC));         pPropSpec[i].ulKind = PRSPEC_PROPID;         pPropSpec[i].propid = pidsiArr[i].pidsi;      }      // Read properties.      hr = pPropStg->ReadMultiple(nPidsi, pPropSpec, pPropVar);      if(FAILED(hr)) {         printf("IPropertyStg::ReadMultiple() failed w/error %08lx\n",                hr);      }      else {         // Dump properties.         for(i=0; i<nPidsi; i++) {            printf("%16s: ", pidsiArr[i].name);            DumpPropVariant(pPropVar + i);         }      }      // De-allocate memory.      delete [] pPropVar;      delete [] pPropSpec;      // Release obtained interface.      pPropStg->Release();   }   // Dump's custom properties of a property storage.   void DumpCustomProps(IPropertySetStorage *pPropSetStg) {      printf("\n==================================================\n");      printf("Custom Properties...\n");      printf("==================================================\n");      IPropertyStorage *pPropStg = NULL;      HRESULT hr;      IEnumSTATPROPSTG *pEnumProp;      // Open User-Defined-Properties, getting an IpropertyStorage.      hr = pPropSetStg->Open(FMTID_UserDefinedProperties,         STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);      if(FAILED(hr)) {         printf("No User Defined Properties.\n");         return;      }      // Get property enumerator.      hr = pPropStg->Enum(&pEnumProp);      if(FAILED(hr)) {      pPropStg->Release();         printf("Couldn't enumerate custom properties.\n");         return;      }      // Enumerate properties.      STATPROPSTG sps;      ULONG fetched;      PROPSPEC propSpec[1];      PROPVARIANT propVar[1];      while(pEnumProp->Next(1, &sps, &fetched) == S_OK) {         // Build a PROPSPEC for this property.         ZeroMemory(&propSpec[0], sizeof(PROPSPEC));         propSpec[0].ulKind = PRSPEC_PROPID;         propSpec[0].propid = sps.propid;         // Read this property.         hr = pPropStg->ReadMultiple(1, &propSpec[0], &propVar[0]);         if(!FAILED(hr)) {            // Translate Prop name into ASCII.            char dbcs[1024];            char *pbstr = (char *)sps.lpwstrName;            int i = wcstombs(dbcs, sps.lpwstrName,                             *((DWORD *)(pbstr-4)));            dbcs[i] = 0;            // Dump this property.            printf("%16s: ", dbcs);            DumpPropVariant(&propVar[0]);         }      }      // Release obtained interface.      pEnumProp->Release();      pPropStg->Release();   }   // Dump's custom and built-in properties of a compound document.   void DumpProps(char *filename) {      // Translate filename to Unicode.      WCHAR wcFilename[1024];      setlocale( LC_ALL, "" );      int i = mbstowcs(wcFilename, filename, strlen(filename));      setlocale( LC_ALL, "C" );      wcFilename[i] = 0;      IStorage *pStorage = NULL;      IPropertySetStorage *pPropSetStg = NULL;      HRESULT hr;      // Open the document as an OLE compound document.      hr = ::StgOpenStorage(wcFilename, NULL,      STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);      if(FAILED(hr)) {         if(hr == STG_E_FILENOTFOUND)            printf("File not found.");         else if(hr == STG_E_FILEALREADYEXISTS)            printf("Not a compound file.");         else            printf("StgOpenStorage() failed w/error %08lx", hr);         return;      }      // Obtain the IPropertySetStorage interface.      hr = pStorage->QueryInterface(              IID_IPropertySetStorage, (void **)&pPropSetStg);      if(FAILED(hr)) {         printf("QI for IPropertySetStorage failed w/error %08lx", hr);         pStorage->Release();         return;      }      // Dump properties.      DumpBuiltInProps(pPropSetStg);      DumpCustomProps(pPropSetStg);      // Release obtained interfaces.      pPropSetStg->Release();      pStorage->Release();   }   // Program entry-point.   void main(int argc, char **argv) {      // Validate arguments.      if(argc != 2) {         printf("- OLE Document Property Viewer\n");         printf("- Usage: %s filename", argv[0]);         return;      }      // Pass filename to the subroutine.      DumpProps(argv[1]);   }					
  4. 编译此程序。
若要进行本示例应复制 PropDump.exe 文件默认路径中的目录 ; 例如 c:\Windows\ 或 c:\Windows\Command\。然后,在一个包含一个复合文档文件的目录中键入 PropDump 跟文件的名称。您应看到输出类似于以下内容:

==================================================

BuiltInProperties 属性...

           Title: MyTitle (VT_LPSTR)         Subject: MySubject (VT_LPSTR)          Author: MyAuthor (VT_LPSTR)        Keywords: MyKeywords (VT_LPSTR)        Comments: MyComments (VT_LPSTR)        Template: Normal (VT_LPSTR)      LastAuthor: Me (VT_LPSTR) Revision Number: 8 (VT_LPSTR)       Edit Time: 01:05.47 pm, Mon 01/01/1601 (VT_FILETIME)    Last printed: (VT_EMPTY)         Created: 01:42.00 pm, Fri 05/29/1998 (VT_FILETIME)      Last Saved: 12:31.00 pm, Mon 06/01/1998 (VT_FILETIME)      Page Count: 1 (VT_I4)      Word Count: 3 (VT_I4)      Char Count: 19 (VT_I4)       Thumpnail: (VT_EMPTY)         AppName: Microsoft Word 8.0 (VT_LPSTR)    Doc Security: 0 (VT_I4)				
==================================================

自定义属性...

   _PID_LINKBASE: (VT_BLOB)       _PID_GUID: (VT_BLOB)       CustProp1: CustProp1TextValue (VT_LPSTR)       CustProp2: 77777 (VT_I4)       CustProp3: TRUE/YES (VT_BOOL)       CustProp4: 00:00.00 am, Tue 05/17/1977 (VT_FILETIME)				

其他说明

IPropertyStorage 和 IPropertySetStorage 接口未定义 COM 的原始发行版中 ; 因此,此示例代码要求系统拥有:
  • Windows NT 4.0 或更高版本
  • 与 Internet Explorer 4.0 或更高版本的 windows 95
  • windows 95 安装 DCOM 与
以前版本的 COM 指定很小的属性和它们的用法尊重但未定义允许开发人员能够在 IStorage 实例中存储属性和 $ 属性集的序列化的格式。此外定义属性的标识符和一个使用的一个文档的摘要信息的单个属性集的语义。该时间时所需创建和操作直接为数据流该结构。有关详细信息属性设置序列化的数据格式结构,请参阅 Microsoft 开发人员网络中的"OLE 序列化属性设置格式"。

(c) Microsoft Corporation 1999,保留的所有权限。李先生 Crump,Microsoft 公司的贡献。

参考
Microsoft 开发人员网络: 持久性的属性集

Microsoft 开发人员网络: OLE 序列化的属性设置格式

Excel Word Access Powerpoint 活页夹 documentproperties builtindocumentproperties customdocumentproperties

警告:本文已自动翻译

属性

文章 ID:186898 - 上次审阅时间:12/05/2015 08:45:19 - 修订版本: 4.4

Microsoft Project 2000 标准版, Microsoft Excel 2000 标准版, Microsoft Visual C++ 5.0 企业版, Microsoft Visual C++ 5.0 专业版, Microsoft Excel 97 标准版, Microsoft PowerPoint 97 标准版, Microsoft Word 97 标准版, Microsoft PowerPoint 2000 标准版, Microsoft Word 2000 标准版, Microsoft Excel 2002 标准版, Microsoft PowerPoint 2002 标准版, Microsoft Word 2002 标准版

  • kbnosurvey kbarchive kbmt kbcmpdoc kbfaq kbhowto KB186898 KbMtzh
反馈