如何修改windows文件的摘要信息

代码里只修改了备注,其他字段相同原理


#include <stdio.h>
#include <windows.h>
#include <ole2.h>


#pragma comment( lib, "ole32.lib" )





int GetFileComments(wchar_t *filename, wchar_t *buf, size_t len)
{
    IPropertySetStorage *pPropSetStg = NULL;
    IPropertyStorage *pPropStg = NULL;
    PROPSPEC propspec; 
    PROPVARIANT propWrite; 
    PROPVARIANT propRead;
    HRESULT hr;
	size_t l;


    // Open a file and a property set within it.
    hr = StgOpenStorageEx(filename,
                    STGM_SHARE_EXCLUSIVE | STGM_READ,
                    STGFMT_ANY,
                    0,
                    NULL,
                    NULL,
                    IID_IPropertySetStorage,
                    reinterpret_cast<void**>(&pPropSetStg) );


    if( FAILED(hr) ) 
        return 0;
 
    hr = pPropSetStg->Open( FMTID_SummaryInformation, 
                            STGM_READ | STGM_SHARE_EXCLUSIVE,
                            &pPropStg );

    if( FAILED(hr) ) 
        return 0;


    propspec.ulKind = PRSPEC_PROPID;
    propspec.propid  = PIDSI_COMMENTS;


    hr = pPropStg->ReadMultiple( 1, &propspec, &propRead );
    if( FAILED(hr) ) 
	{
		pPropSetStg->Release();
		pPropSetStg = NULL;
        return 0;
	}

	l = wcslen(propRead.pwszVal)+1;

	if(0 == len)
	{
		pPropSetStg->Release();
		pPropSetStg = NULL;
		pPropStg->Release(); 
		pPropStg = NULL;
		return l;
	}

	wcscpy(buf,propRead.pwszVal);

	pPropSetStg->Release();
	pPropSetStg = NULL;
    pPropStg->Release(); 
    pPropStg = NULL;

	return l;
}



int SetFileComments(wchar_t *filename, wchar_t *buf)
{
    IPropertySetStorage *pPropSetStg = NULL;
    IPropertyStorage *pPropStg = NULL;
    PROPSPEC propspec; 
    PROPVARIANT propWrite; 
    PROPVARIANT propRead;
    HRESULT hr = S_OK;


    // Open a file and a property set within it.
    hr = StgOpenStorageEx(filename,
                    STGM_SHARE_EXCLUSIVE | STGM_READWRITE | STGM_DIRECT,
                    STGFMT_ANY,
                    0,
                    NULL,
                    NULL,
                    IID_IPropertySetStorage,
                    reinterpret_cast<void**>(&pPropSetStg) );


    if( FAILED(hr) ) 
        return 0;
 


   
	hr = pPropSetStg->Create( FMTID_SummaryInformation, NULL, 
							PROPSETFLAG_DEFAULT,
							STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
							&pPropStg );
	if( FAILED(hr) && STG_E_FILEALREADYEXISTS == hr)
	{
		hr = pPropSetStg->Open( FMTID_SummaryInformation, 
								STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
								&pPropStg );	
		if(FAILED(hr))
			return 0;
	}



    propspec.ulKind = PRSPEC_PROPID;
    propspec.propid  = PIDSI_COMMENTS;


    //specify the value of property
    propWrite.vt = VT_LPWSTR;
    propWrite.pwszVal = buf;


    hr = pPropStg->WriteMultiple( 1, &propspec, &propWrite,PID_FIRST_USABLE);
    if( FAILED(hr) ) 
	{
		pPropSetStg->Release();
		pPropSetStg = NULL;
        return 0;
	}
	pPropSetStg->Release();
	pPropSetStg = NULL;
    pPropStg->Release(); 
    pPropStg = NULL;

	return 1;
}



你可能感兴趣的:(如何修改windows文件的摘要信息)