Updating Existing Objects
This section shows example code to update existing objects. The example code uses:
CMetaObject::WriteObject
The example code also shows the use of Transactions.
///////////////////////////////////////////////////////////////////
// ReWrite function
//
///////////////////////////////////////////////////////////////////
HRESULT ReWrite(CMetaRepository &repos)
{
CMetaObject obj;
HRESULT result = S_OK;
CMetaProperty PropObj;
unsigned long carId = 0x1015aeff;
// Begin an explicit transaction
if (SUCCEEDED(repos.BeginTransaction()))
{
obj.SetClassGUID(CLSGUID_AutoDiv);
obj.SetObjectName(_T("AmericanAutos"));
// Read the object to fill in all existing properties
// and lock the object
if (SUCCEEDED(result = obj.ReadObjectWithLock()))
{
// set updated fields
obj.SetDescription(_T("New Description for AmericanAutos"));
// Write updated object
result = obj.WriteObject();
}
// Commit the changes
repos.Commit();
}
// Begin an explicit transaction
if (SUCCEEDED(repos.BeginTransaction()))
{
obj.Initialize();
obj.SetClassGUID(CLSGUID_Auto);
obj.SetObjectName(_T("Yearling"));
// Read the object to fill in all existing properties
if (SUCCEEDED(result = obj.ReadObjectWithLock()))
{
// set updated fields
if (SUCCEEDED(result = PropObj.SetBinary(carId)))
{
obj.SetPropertyValue(PID_AUTO_CARID, PropObj);
PropObj.SetShort(22000);
obj.SetPropertyValue(PID_AUTO_BASEPRICE, PropObj);
PropObj.SetString(_T("1965-04-25"));
obj.SetPropertyValue(PID_AUTO_DESIGNDATE, PropObj);
// Write updated object
result = obj.WriteObject();
}
}
// Commit the changes
repos.Commit();
}
return (result);
}