1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#define CLRSTRINGA(name,clr_name) ClrStringA ^ clr_name = gcnew ClrStringA(name);
public ref class SummaryInfoHelper
{
private:
static void CopyStrings(CHAR * pDest,LPCSTR pSource,LONG Length)
{
if(pSource == NULL)
ZeroMemory(pDest,Length);
else
strcpy(pDest,pSource);
}
static BOOL nativeSetSummaryInfoItem(
LPCSTR fileName,
LPCSTR title,
LPCSTR subject,
LPCSTR author,
LPCSTR keywords,
LPCSTR comments
)
{
#define STR_BUF_SIZE 1024
WCHAR wcFilename[STR_BUF_SIZE];
IPropertySetStorage *pPropSetStg = NULL;
IPropertyStorage *pPropStg = NULL;
ZeroMemory(wcFilename,STR_BUF_SIZE);
//Convert to unicode
mbstowcs(wcFilename, fileName, strlen(fileName ));
CHAR p_title[STR_BUF_SIZE];
CHAR p_subject[STR_BUF_SIZE];
CHAR p_author[STR_BUF_SIZE];
CHAR p_keywords[STR_BUF_SIZE];
CHAR p_comments[STR_BUF_SIZE];
#define COPY_STRINGS(a,b) CopyStrings(a,b,STR_BUF_SIZE)
COPY_STRINGS(p_title,title);
COPY_STRINGS(p_subject,subject);
COPY_STRINGS(p_author,author);
COPY_STRINGS(p_keywords,keywords);
COPY_STRINGS(p_comments,comments);
//Get the IPropertySetStorage interface
HRESULT rx = StgOpenStorageEx(wcFilename, STGM_READWRITE|STGM_SHARE_EXCLUSIVE, STGFMT_ANY, 0, NULL, NULL, IID_IPropertySetStorage, (void**)&pPropSetStg );
//CREATE summary information, getting an IpropertyStorage. ( AUDREY )
HRESULT rt = pPropSetStg->Create(FMTID_SummaryInformation,NULL,PROPSETFLAG_DEFAULT,STGM_SIMPLE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &pPropStg);
//Tell Windows to set Title,Subject,Author,Keywords and Comments properties
PROPSPEC ps[5];
ps[0].ulKind = PRSPEC_PROPID;
ps[0].propid = PIDSI_TITLE;
ps[1].ulKind = PRSPEC_PROPID;
ps[1].propid = PIDSI_SUBJECT;
ps[2].ulKind = PRSPEC_PROPID;
ps[2].propid = PIDSI_AUTHOR;
ps[3].ulKind = PRSPEC_PROPID;
ps[3].propid = PIDSI_KEYWORDS;
ps[4].ulKind = PRSPEC_PROPID;
ps[4].propid = PIDSI_COMMENTS;
//Properties values
PROPVARIANT pv[5];
pv[0].vt = VT_LPSTR;
pv[0].pszVal = p_title;//"The title";
pv[1].vt = VT_LPSTR;
pv[1].pszVal = p_subject;//"The subject";
pv[2].vt = VT_LPSTR;
pv[2].pszVal = p_author;//"The author of this file";
pv[3].vt = VT_LPSTR;
pv[3].pszVal = p_keywords;//"keywords,keywords,keywords";
pv[4].vt = VT_LPSTR;
pv[4].pszVal = p_comments;//"Comment";
//Write to file
pPropStg->WriteMultiple(5, ps, pv, 0);
//Release interfaces in the reverse order in which they where obtained
pPropStg->Release();
pPropSetStg->Release();
return TRUE;
}
public:
static void SetSummaryInfo(
System::String ^ fileName,
System::String ^ title,
System::String ^ subject,
System::String ^ author,
System::String ^ keywords,
System::String ^ comments
)
{
CLRSTRINGA(fileName,_fileName);
CLRSTRINGA(title,_title);
CLRSTRINGA(subject,_subject);
CLRSTRINGA(author,_author);
CLRSTRINGA(keywords,_keywords);
CLRSTRINGA(comments,_comments);
try
{
nativeSetSummaryInfoItem(
_fileName->native_str(),
_title->native_str(),
_subject->native_str(),
_author->native_str(),
_keywords->native_str(),
_comments->native_str()
);
}
finally
{
delete _fileName;
delete _title;
delete _subject;
delete _author;
delete _comments;
}
}
}; |
Partager