Property Containers

The IPropertyContainer interface provides an interface to an object’s properties. You can use it to retrieve an IProperty pointer to each property supported by the object. The IProperty interface only describes the property, it doesn’t give access to the value of the property. The IPropertyContainer interface has methods for getting and setting the value of a given property.

  • PutPropertyValue() takes a VARIANT and sets the value of property identified by the given property ID.

  • GetPropertyValue() returns a VARIANT given a property ID.

Using a property container to display an object’s properties demonstrates how to display each of the properties for a given property container.

Using a property container to display an object’s properties

void ShowProperties(IPropertyContainer* pContainer, CDC* pDC)

{

BSTR buf;

TCHAR tBuf[20];

COleVariant val;

USES_CONVERSION;

 

int i;

for (i=0; i < pContainer->GetPropertyCount(); i++)

{

// Get a pointer to the property at position i

// in the container

IProperty* pProp = pContainer->GetPropertyAt(i);

 

// Get the property ID

PropertyId propId = pProp->GetId();

pDC->TextOut(10, (i*90), _T("Property ID:"));

_itot(propId, tBuf, 10);

pDC->TextOut(150, (i*90), tBuf);

 

// Get the property name

pProp->GetName(buf);

pDC->TextOut(10,(i*90)+20,_T("Property Name:"));

pDC->TextOut(150,(i*90)+20,OLE2T(buf));

 

// Get the property description

pProp->GetDescription(buf);

pDC->TextOut(10,(i*90)+40,_T("Property Description:"));

pDC->TextOut(150, (i*90)+40, OLE2T(buf));

 

// Get the property value

pContainer->GetPropertyValue(propId, val);

val.ChangeType(VT_BSTR);

pDC->TextOut(10, (i*90)+60,

_T("Property Value:"));

pDC->TextOut(150, (i*90)+60, OLE2T(val.bstrVal));

}

}