5,530
社区成员




//接上帖
void DumpBuiltInProps(IPropertySetStorage *pPropSetStg) {// Dump's built-in properties of a property storage.
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);
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();
}
void DumpCustomProps(IPropertySetStorage *pPropSetStg) {// Dump's custom properties of a property storage.
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();
}
void DumpProps(char *filename) {// Dump's custom and built-in properties of a compound document.
// 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.\nTry use WinRAR to open docx/xlsx/pptx,view docProps\\core.xml and docProps\\app.xml\n");
if ('.'==filename[strlen(filename)-5]) TryWinRAR(filename);
} 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();
}
void main(int argc, char **argv) {
if (argc != 2) {
printf("OLE/ZIP Document Property Viewer\n");
printf("Usage: %s filename", argv[0]);
return;
}
setlocale( LC_ALL, "chs" );
printf("[File] %s\n",argv[1]);
DumpProps(argv[1]);
}
#pragma comment(lib,"ole32")
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <ole2.h>
#include <locale.h>
void DumpXML(char *xml) {
static char b[8192];
static WCHAR wstr[1024];
char *p,*q;
FILE *f;
int i,L;
f=fopen(xml,"rb");
if (NULL==f) return;
fread(b,1,8192,f);
fclose(f);
printf("================= %s =================\n",xml);
p=b;
for (i=0;i<3;i++) {//跳过前两个标签
p=strchr(p,'<');
if (!p) return;
p++;
}
while (1) {
if ('/'!=p[0]) {
q=strchr(p,'>');
if (!q) return;
printf("%32.*s: ",q-p,p);
p=q+1;
q=strchr(p,'<');
if (!q) return;
L=q-p;
for (i=0;i<L;i++) if ((unsigned char)p[i]>=0x80) break;
if (i<L) {
i=MultiByteToWideChar(CP_UTF8,0,p,L,wstr,1024);
wstr[i] = 0;
printf("%.*s UTF8(%S)\n",L,p,wstr);
} else {
printf("%.*s\n",L,p);
}
p=q+1;
} else {
p=strchr(p,'<');
if (!p) return;
p++;
}
}
}
void TryWinRAR(char *filename) {
char cmd[1080];
if (access("c:\\progra~1\\winrar\\winrar.exe",0)) return;
system("rd /s /q docProps 1>NUL 2>NUL");
sprintf(cmd,"c:\\progra~1\\winrar\\winrar.exe x \"%.1000s\" docProps\\*.xml",filename);
system(cmd);
if (!access("docProps\\core.xml",0)) DumpXML("docProps\\core.xml");
if (!access("docProps\\app.xml",0)) DumpXML("docProps\\app.xml");
system("rd /s /q docProps 1>NUL 2>NUL");
}
void DumpPropVariant(PROPVARIANT *pPropVar) {// Dumps simple PROPVARIANT values.
if (pPropVar->vt & VT_ARRAY) {// Don't iterate arrays, just inform as an array.
printf("%32s%s",""," (Array)\n");
return;
}
if (pPropVar->vt & VT_BYREF) {// Don't handle byref for simplicity, just inform byref.
printf("%32s%s",""," (ByRef)\n");
return;
}
switch(pPropVar->vt) {
case VT_EMPTY:printf("%32s%s",""," (VT_EMPTY)\n" );break;
case VT_NULL :printf("%32s%s",""," (VT_NULL)\n" );break;
case VT_BLOB :printf("%32s%s",""," (VT_BLOB)\n" );break;
case VT_BOOL :printf("%-32s" " (VT_BOOL)\n", pPropVar->boolVal ? "TRUE/YES" : "FALSE/NO");break;
case VT_I2 :printf("%-32d" " (VT_I2)\n" , (int) pPropVar-> iVal );break;
case VT_I4 :printf("%-32d" " (VT_I4)\n" , (int) pPropVar-> lVal );break;
case VT_R4 :printf("%-32.2lf (VT_R4)\n" , (double)pPropVar-> fltVal );break;
case VT_R8 :printf("%-32.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("%-32s (VT_BSTR)\n", dbcs);
}
break;
case VT_LPSTR: // Null-terminated string.
{
char *pbstr = (char *)pPropVar->pszVal;
int i,L;
L=strlen(pbstr);
for (i=0;i<L;i++) if ((unsigned char)pbstr[i]>=0x80) break;
if (i<L) {
WCHAR wstr[1024];
int i = MultiByteToWideChar(CP_UTF8,0,pbstr,-1,wstr,1024);
wstr[i] = 0;
printf("%-32s (VT_LPSTR) UTF8(%S)\n", pPropVar->pszVal,wstr);
} else {
printf("%-32s (VT_LPSTR)\n", pPropVar->pszVal);
}
}
break;
case VT_LPWSTR:
printf("%-32S (VT_LPWSTR)\n", pPropVar->pwszVal);
break;
case VT_FILETIME:
{
FILETIME lft;
FileTimeToLocalFileTime(&pPropVar->filetime, &lft);
SYSTEMTIME lst;
FileTimeToSystemTime(&lft, &lst);
printf("%d-%02d-%02d %02d:%02d.%02d (VT_FILETIME)\n",
lst.wYear, lst.wMonth, lst.wDay,
lst.wHour, lst.wMinute, lst.wSecond);
}
break;
case VT_CF: // Clipboard format.
printf("%32s%s",""," (Clipboard format)\n");
break;
default: // Unhandled type, consult wtypes.h's VARENUM structure.
printf("%32s (Unhandled type: 0x%08lx)\n", "", pPropVar->vt);
break;
}
}
//未完待续