Anda dapat mengambil dokumen senyawa properti dari dokumen menggunakan
standar antarmuka tanpa server berjalan atau bahkan yang diinstal. Untuk
misalnya, Anda dapat membuka dokumen built-in properti seperti penulis,
Terakhir diubah kali, dan halaman menghitung sifat Microsoft Office 97
dokumen serta properti kustom dokumen lainnya.
Langkah-langkah berikut menggambarkan bagaimana Anda bisa membangun sebuah senyawa dokumen
properti penampil dengan Microsoft Visual C++. Contoh adalah konsol Win32
Aplikasi proyek, dan dapat dimodifikasi sesuai dengan kebutuhan Anda.
Langkah-langkah untuk membuat sampel
Membuat aplikasi konsol Win32 proyek baru, dan menyebutnya PropDump.
Menambah sebuah berkas baru yang disebut main.cpp untuk proyek Anda.
Salin kode berikut ke 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]);
}
Menyusun program.
Untuk menjalankan contoh, Anda harus menyalin berkas PropDump.exe ke sebuah direktori di
default path; misalnya c:\Windows\ atau c:\Windows\Command\. Kemudian,
dalam sebuah direktori yang berisi file dokumen senyawa, ketik PropDump diikuti
dengan nama file. Anda akan melihat output yang mirip dengan berikut ini:
Antarmuka IPropertyStorage dan IPropertySetStorage tidak didefinisikan dalam
peluncuran asli COM; sehingga kode sampel ini memerlukan sistem dengan:
Windows NT 4.0 atau yang lebih baru
Windows 95 dengan Internet Explorer versi 4.0 atau yang lebih baru
Windows 95 dengan DCOM diinstal
Versi sebelumnya com ditentukan sangat sedikit sehubungan dengan properti
dan penggunaan mereka, tetapi melakukan menentukan format serial yang memungkinkan pengembang
untuk menyimpan properti dan properti set dalam contoh IStorage. Properti
pengidentifikasi dan semantik satu properti diatur, digunakan untuk ringkasan
informasi tentang dokumen, juga didefinisikan. Pada waktu itu, itu
diperlukan untuk membuat dan memanipulasi struktur yang secara langsung sebagai data
streaming. Untuk informasi lebih lanjut tentang properti diatur diserialkan data format
struktur, merujuk kepada "OLE diserialkan properti mengatur Format" dalam Microsoft
Jaringan pengembang.
(c) Microsoft Corporation 1999, Semua Hak Dilindungi Undang-Undang. Kontribusi oleh Joe sekaligus, Microsoft Corporation.
ID Artikel: 186898 - Kajian Terakhir: 16 September 2011 - Revisi: 2.0
Berlaku bagi:
Microsoft Project 2000 Standard Edition
Microsoft Excel 2000 Standard Edition
Microsoft Visual C++ 5.0 Enterprise Edition
Microsoft Visual C++ 5.0 Professional Edition
Microsoft Excel 97 Standard Edition
Microsoft PowerPoint 97 Standard Edition
Microsoft Word 97 Standard Edition
Microsoft PowerPoint 2000 Standard Edition
Microsoft Word 2000
Microsoft Excel 2002 Standard Edition
Microsoft PowerPoint 2002 Standard Edition
Microsoft Word 2002
Kata kunci:
kbcmpdoc kbfaq kbhowto kbmt KB186898 KbMtid
Penerjemahan Mesin
PENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:186898
Terima kasih! Masukan Anda akan digunakan untuk membantu kami meningkatkan konten dukungan. Untuk opsi bantuan lainnya, kunjungi Halaman Beranda Bantuan dan Dukungan.