如何修改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;
}



©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值