Sie sind auf Seite 1von 3

// SummaryPropPage.

cpp : Defines the entry point


// for the application.
//

#include "stdafx.h"

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

// Implicitly link ole32.dll


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

const FMTID PropSetfmtid ={


/* F29F85E0-4FF9-1068-AB91-08002B27B3D9 */
0xf29f85e0,
0x4ff9,
0x1068,
{0xab, 0x91, 0x08, 0x00, 0x2b, 0x27, 0xb3, 0xd9 }
};

int APIENTRY WinMain(HINSTANCE hInstance,


HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.

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( L"C:\\Document.txt",
STGM_DIRECT|STGM_SHARE_EXCLUSIVE|
STGM_READWRITE,
STGFMT_ANY,
// STGFMT_STORAGE //Structured
//Storage property sets
// STGFMT_FILE //NTFS file system
//property sets
0,
NULL,
NULL,
IID_IPropertySetStorage,
reinterpret_cast<void**>(&pPropSetStg) );

if( FAILED(hr) )
throw L"Failed StgOpenStorageEx";

/*
hr = pPropSetStg->Open( PropSetfmtid,
STGM_WRITE|
STGM_SHARE_EXCLUSIVE,
&pPropStg );
*/

hr = pPropSetStg->Create( PropSetfmtid, NULL,


PROPSETFLAG_DEFAULT,
STGM_CREATE|STGM_READWRITE|
STGM_SHARE_EXCLUSIVE,
&pPropStg );

if( FAILED(hr) )
throw L"Failed IPropertySetStorage::Open";

//we can identify any property through its Name or its ID


// propspec.ulKind = PRSPEC_LPWSTR;
// propspec.lpwstr = L"Title";

propspec.ulKind = PRSPEC_PROPID;
propspec.propid = 0x00000002;

//specify the value of property


propWrite.vt = VT_LPWSTR;
propWrite.pwszVal = L"this value set through code";

hr = pPropStg->WriteMultiple( 1, &propspec,
&propWrite, PID_FIRST_USABLE );

if( FAILED(hr) )
throw L"Failed IPropertyStorage::WriteMultiple";

pPropStg->Release();
pPropStg = NULL;

//again open the property set

hr = pPropSetStg->Open( PropSetfmtid,
STGM_READ|STGM_SHARE_EXCLUSIVE,
&pPropStg );

if( FAILED(hr) )
throw L"Failed IPropertySetStorage::Open";

// Read the property back and validate it


hr = pPropStg->ReadMultiple( 1, &propspec, &propRead );
if( FAILED(hr) )
throw L"Failed IPropertyStorage::ReadMultiple";

char* str = new char [wcslen(propRead.pwszVal) + 1];


// the "%S" will implicitly convert UNICODE to ANSI.
wsprintfA ( str, "%S", propRead.pwszVal);

//if you want to display


// MessageBox(NULL,str,"Reading Value",MB_OK);

if( hr == S_FALSE )
throw L"Property didn't exist after "
L"reopening the property set";
else if( propWrite.vt != propRead.vt )
throw L"Property types didn't match "
L"after reopening the property set";
else if( wcscmp( propWrite.pwszVal, propRead.pwszVal ) != 0 )
throw L"Property values didn't match"
L" after reopening the property set";
else
wprintf( L"Success\n" );

return 0;
}

Das könnte Ihnen auch gefallen