【MS C++】请问怎么设置参考模型中的元素的EC属性

老师,您好,我需要设置元素的EC属性,但是该元素是参考模型中的,目前已拿到参考dgnmodel和参考的dgnfile,但是执行完代码后没有生效。

bool CElementTool::SetElementEC(EditElementHandleR eeh, const WString & libName, const ElementEC& ec, DgnModelRefP model)
{
	ElementRefP oldref = eeh.GetElementRef();
	DgnFileP dgnfile = model->GetDgnFileP();
	auto str = dgnfile->GetFileName();
	ItemTypeLibraryPtr itemTypeLibrary = ItemTypeLibrary::FindByName(libName.c_str(), *dgnfile);
	if (itemTypeLibrary == NULL)
	{
		itemTypeLibrary = ItemTypeLibrary::Create(libName.c_str(), *dgnfile);
	}

    ItemTypeP itemType = itemTypeLibrary->GetItemTypeByName(libName.GetWCharCP());
	if (itemType == NULL)
	{
		itemType = itemTypeLibrary->AddItemType(libName.GetWCharCP());
	}

	CustomPropertyP Custype = itemType->GetPropertyByName(ec.key.GetWCharCP());
	if (Custype == NULL)
	{
		Custype = itemType->AddProperty(ec.key.GetWCharCP());
		Custype->SetType(ec.valType);
	}
	
	itemTypeLibrary->Write();
	CustomItemHost itemHost(eeh, true);  // true = schedule changed
    DgnECInstancePtr pInstance = itemHost.ApplyCustomItem(*itemType);
	if (pInstance == nullptr)
	{
		pInstance = itemHost.GetCustomItem(libName.GetWCharCP(), libName.GetWCharCP());
		if (pInstance == nullptr)
		{
			return false;
		}
	}
	WString encodedProp;
	ECN::ECNameValidation::EncodeToValidName(encodedProp, ec.key);
	pInstance->SetValue(encodedProp.GetWCharCP(), ec.val);
	pInstance->ScheduleWriteChanges(eeh);
	eeh.ReplaceInModel(oldref);
	return true;
}

调试看到dgnfile的名称是参考的dgn文件名。