3. |
将以下代码复制到 main.cpp:
#include <stdio.h>
#include <windows.h>
#include <ole2.h>
#include <locale.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Dumps simple PROPVARIANT values.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) void DumpPropVariant(PROPVARIANT *pPropVar) ...{
// Don't iterate arrays, just inform as an array.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) if(pPropVar->vt & VT_ARRAY) ...{
printf("(Array) ");
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Don't handle byref for simplicity, just inform byref.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) if(pPropVar->vt & VT_BYREF) ...{
printf("(ByRef) ");
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Switch types.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) switch(pPropVar->vt) ...{
case VT_EMPTY:
printf("(VT_EMPTY) ");
break;
case VT_NULL:
printf("(VT_NULL) ");
break;
case VT_BLOB:
printf("(VT_BLOB) ");
break;
case VT_BOOL:
printf("%s (VT_BOOL) ",
pPropVar->boolVal ? "TRUE/YES" : "FALSE/NO");
break;
case VT_I2: // 2-byte signed int.
printf("%d (VT_I2) ", (int)pPropVar->iVal);
break;
case VT_I4: // 4-byte signed int.
printf("%d (VT_I4) ", (int)pPropVar->lVal);
break;
case VT_R4: // 4-byte real.
printf("%.2lf (VT_R4) ", (double)pPropVar->fltVal);
break;
case VT_R8: // 8-byte real.
printf("%.2lf (VT_R8) ", (double)pPropVar->dblVal);
break;
case VT_BSTR: // OLE Automation string.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
// 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) ", dbcs);
}
break;
case VT_LPSTR: // Null-terminated string.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
printf("%s (VT_LPSTR) ", pPropVar->pszVal);
}
break;
case VT_FILETIME:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
char *dayPre[] =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FILETIME lft;
FileTimeToLocalFileTime(&pPropVar->filetime, &lft); SYSTEMTIME lst;
FileTimeToSystemTime(&lft, &lst);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%02d:%02d.%02d %s, %s %02d/%02d/%d (VT_FILETIME) ",
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) ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
default: // Unhandled type, consult wtypes.h's VARENUM structure.
printf("(Unhandled type: 0x%08lx) ", pPropVar->vt);
break;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Dump's built-in properties of a property storage.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) void DumpBuiltInProps(IPropertySetStorage *pPropSetStg) ...{
printf(" ================================================== ");
printf("BuiltInProperties Properties... ");
printf("================================================== ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IPropertyStorage *pPropStg = NULL;
HRESULT hr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) if(FAILED(hr)) ...{
printf("No Summary-Information. ");
return;
}
// Array of PIDSI's you are interested in.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) struct pidsiStruct ...{
char *name;
long pidsi;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) } pidsiArr[] = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Title", PIDSI_TITLE}, // VT_LPSTR
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Subject", PIDSI_SUBJECT}, // ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Author", PIDSI_AUTHOR},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Keywords", PIDSI_KEYWORDS},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Comments", PIDSI_COMMENTS},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Template", PIDSI_TEMPLATE},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"LastAuthor", PIDSI_LASTAUTHOR},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Revision Number", PIDSI_REVNUMBER},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Edit Time", PIDSI_EDITTIME}, // VT_FILENAME (UTC)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Last printed", PIDSI_LASTPRINTED}, // ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Created", PIDSI_CREATE_DTM},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Last Saved", PIDSI_LASTSAVE_DTM},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Page Count", PIDSI_PAGECOUNT}, // VT_I4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Word Count", PIDSI_WORDCOUNT}, // ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Char Count", PIDSI_CHARCOUNT},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Thumpnail", PIDSI_THUMBNAIL}, // VT_CF
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"AppName", PIDSI_APPNAME}, // VT_LPSTR
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{"Doc Security", PIDSI_DOC_SECURITY}, // VT_I4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{0, 0}
};
// Count elements in pidsiArr.
int nPidsi = 0;
for(nPidsi=0; pidsiArr[nPidsi].name; nPidsi++);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Initialize PROPSPEC for the properties you want.
PROPSPEC *pPropSpec = new PROPSPEC [nPidsi];
PROPVARIANT *pPropVar = new PROPVARIANT [nPidsi];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) for(int i=0; i<nPidsi; i++) ...{
ZeroMemory(&pPropSpec[i], sizeof(PROPSPEC));
pPropSpec[i].ulKind = PRSPEC_PROPID;
pPropSpec[i].propid = pidsiArr[i].pidsi;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Read properties.
hr = pPropStg->ReadMultiple(nPidsi, pPropSpec, pPropVar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) if(FAILED(hr)) ...{
printf("IPropertyStg::ReadMultiple() failed w/error %08lx ",
hr);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) else ...{
// Dump properties.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) for(i=0; i<nPidsi; i++) ...{
printf("%16s: ", pidsiArr[i].name);
DumpPropVariant(pPropVar + i);
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// De-allocate memory.
delete [] pPropVar;
delete [] pPropSpec;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Release obtained interface.
pPropStg->Release();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Dump's custom properties of a property storage.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) void DumpCustomProps(IPropertySetStorage *pPropSetStg) ...{
printf(" ================================================== ");
printf("Custom Properties... ");
printf("================================================== ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IPropertyStorage *pPropStg = NULL;
HRESULT hr;
IEnumSTATPROPSTG *pEnumProp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Open User-Defined-Properties, getting an IpropertyStorage.
hr = pPropSetStg->Open(FMTID_UserDefinedProperties,
STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) if(FAILED(hr)) ...{
printf("No User Defined Properties. ");
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Get property enumerator.
hr = pPropStg->Enum(&pEnumProp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) if(FAILED(hr)) ...{
pPropStg->Release();
printf("Couldn't enumerate custom properties. ");
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Enumerate properties.
STATPROPSTG sps;
ULONG fetched;
PROPSPEC propSpec[1];
PROPVARIANT propVar[1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) 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;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Read this property.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hr = pPropStg->ReadMultiple(1, &propSpec[0], &propVar[0]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) 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;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Dump this property.
printf("%16s: ", dbcs);
DumpPropVariant(&propVar[0]);
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Release obtained interface.
pEnumProp->Release();
pPropStg->Release();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Dump's custom and built-in properties of a compound document.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) 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;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IStorage *pStorage = NULL;
IPropertySetStorage *pPropSetStg = NULL;
HRESULT hr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Open the document as an OLE compound document.
hr = ::StgOpenStorage(wcFilename, NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) 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;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Obtain the IPropertySetStorage interface.
hr = pStorage->QueryInterface(
IID_IPropertySetStorage, (void **)&pPropSetStg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) if(FAILED(hr)) ...{
printf("QI for IPropertySetStorage failed w/error %08lx", hr);
pStorage->Release();
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Dump properties.
DumpBuiltInProps(pPropSetStg);
DumpCustomProps(pPropSetStg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Release obtained interfaces.
pPropSetStg->Release();
pStorage->Release();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Program entry-point.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) void main(int argc, char **argv) ...{
// Validate arguments.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) if(argc != 2) ...{
printf("- OLE Document Property Viewer ");
printf("- Usage: %s filename", argv[0]);
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Pass filename to the subroutine.
DumpProps(argv[1]);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
|