📄 property.cpp
字号:
{
if(pIUnknown==NULL)
return 0;
DBPROPINFO* pPropInfo = NULL;
DBPROPFLAGS dwFlags = DBPROPFLAGS_NOTSUPPORTED;
//GetPropInfo
TESTC(GetPropInfo(pIUnknown, PropertyID, guidPropertySet, &pPropInfo))
if(pPropInfo)
dwFlags = pPropInfo->dwFlags;
CLEANUP:
SAFE_FREE(pPropInfo);
return dwFlags;
}
/////////////////////////////////////////////////////////////////////////////
// BOOL IsSupportedProperty
//
/////////////////////////////////////////////////////////////////////////////
BOOL IsSupportedProperty(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet)
{
return GetPropInfoFlags(pIUnknown, PropertyID, guidPropertySet) != DBPROPFLAGS_NOTSUPPORTED;
}
/////////////////////////////////////////////////////////////////////////////
// BOOL IsSettableProperty
//
/////////////////////////////////////////////////////////////////////////////
BOOL IsSettableProperty(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet)
{
return GetPropInfoFlags(pIUnknown, PropertyID, guidPropertySet) & DBPROPFLAGS_WRITE;
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT SetProperty
//
/////////////////////////////////////////////////////////////////////////////
HRESULT SetProperty(DBPROPID PropertyID, GUID guidPropertySet, ULONG* pcPropSets, DBPROPSET** prgPropSets, DBTYPE wType, ULONG ulValue, DBPROPOPTIONS dwOptions, DBID colid)
{
if(wType == DBTYPE_BOOL)
ulValue = ulValue ? VARIANT_TRUE : VARIANT_FALSE;
return SetProperty(PropertyID, guidPropertySet, pcPropSets, prgPropSets, wType, &ulValue, dwOptions, colid);
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT SetProperty
//
/////////////////////////////////////////////////////////////////////////////
HRESULT SetProperty(DBPROPID PropertyID, GUID guidPropertySet, ULONG* pcPropSets, DBPROPSET** prgPropSets, DBTYPE wType, void* pv, DBPROPOPTIONS dwOptions, DBID colid)
{
ASSERT(PropertyID && prgPropSets && pcPropSets && pv);
HRESULT hr = S_OK;
ULONG cProperties = 0;
DBPROP* rgProperties = NULL;
//Make our lives a little easier
ULONG cPropSets = *pcPropSets;
DBPROPSET* rgPropSets = *prgPropSets;
ULONG iPropSet = ULONG_MAX;
//Find the correct PropSet structure to add the property to
for(ULONG i=0; i<cPropSets; i++)
if(guidPropertySet == rgPropSets[i].guidPropertySet)
iPropSet = i;
//Do we need to create another PropSets structure for this property
if(iPropSet==ULONG_MAX)
{
iPropSet = cPropSets;
SAFE_REALLOC(rgPropSets, DBPROPSET, cPropSets+1);
rgPropSets[iPropSet].cProperties = 0;
rgPropSets[iPropSet].rgProperties = NULL;
rgPropSets[iPropSet].guidPropertySet = guidPropertySet;
cPropSets++;
}
//Now make our lives really easy
cProperties = rgPropSets[iPropSet].cProperties;
rgProperties = rgPropSets[iPropSet].rgProperties;
//do we need to enlarge the list
SAFE_REALLOC(rgProperties, DBPROP, cProperties+1);
//Add the new property to the list
rgProperties[cProperties].dwPropertyID = PropertyID;
rgProperties[cProperties].dwOptions = dwOptions;
rgProperties[cProperties].dwStatus = DBPROPSTATUS_OK;
rgProperties[cProperties].colid = colid;
VariantInit(&rgProperties[cProperties].vValue);
switch(wType)
{
case DBTYPE_BOOL:
rgProperties[cProperties].vValue.vt = VT_BOOL;
V_BOOL(&(rgProperties[cProperties].vValue)) = *(VARIANT_BOOL*)pv;
break;
case DBTYPE_I2:
rgProperties[cProperties].vValue.vt = VT_I2;
V_I2(&(rgProperties[cProperties].vValue)) = *(SHORT*)pv;
break;
case DBTYPE_I4:
rgProperties[cProperties].vValue.vt = VT_I4;
V_I4(&(rgProperties[cProperties].vValue)) = *(LONG*)pv;
break;
case DBTYPE_WSTR:
case DBTYPE_BSTR:
rgProperties[cProperties].vValue.vt = VT_BSTR;
V_BSTR(&(rgProperties[cProperties].vValue)) = SysAllocString((BSTR)pv);
break;
case DBTYPE_VARIANT:
VariantCopy(&rgProperties[cProperties].vValue, (VARIANT*)pv);
break;
default:
ASSERT(FALSE); //Unhandled property type
break;
}
//Increment the number of properties
cProperties++;
CLEANUP:
//Now go back to the rediculous property structures
rgPropSets[iPropSet].cProperties = cProperties;
rgPropSets[iPropSet].rgProperties = rgProperties;
*pcPropSets = cPropSets;
*prgPropSets = rgPropSets;
return hr;
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT SetRestriction
//
/////////////////////////////////////////////////////////////////////////////
HRESULT SetRestriction(VARIANT* pRestriction, WCHAR* pwszValue)
{
ASSERT(pRestriction);
ASSERT(pwszValue);
//VT_BSTR case
if(pwszValue && pwszValue[0])
{
pRestriction->vt = VT_BSTR;
SAFE_SYSALLOC(V_BSTR(pRestriction), pwszValue);
}
CLEANUP:
return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT InitVariants
//
/////////////////////////////////////////////////////////////////////////////
HRESULT InitVariants(ULONG cVariants, VARIANT* rgVariants)
{
HRESULT hr = S_OK;
//Free all variants
for(ULONG i=0; i<cVariants; i++)
VariantInit(&rgVariants[i]);
return hr;
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT FreeVariants
//
/////////////////////////////////////////////////////////////////////////////
HRESULT FreeVariants(ULONG cVariants, VARIANT* rgVariants)
{
HRESULT hr = S_OK;
//Free the inner variants first
for(ULONG i=0; i<cVariants; i++)
XTEST(NULL, hr = VariantClear(&rgVariants[i]));
return hr;
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT FreeProperties
//
/////////////////////////////////////////////////////////////////////////////
HRESULT FreeProperties(ULONG* pcProperties, DBPROP** prgProperties)
{
ASSERT(pcProperties);
ASSERT(prgProperties);
HRESULT hr = S_OK;
//no-op case
if(*pcProperties==0 || *prgProperties==NULL)
return S_OK;
//Free the inner variants first
for(ULONG i=0; i<*pcProperties; i++)
{
//if DBPROPSTATUS_NOTSUPPORTED then vValue is undefined
if((*prgProperties)[i].dwStatus != DBPROPSTATUS_NOTSUPPORTED)
XTEST(NULL, hr = VariantClear(&((*prgProperties)[i].vValue)));
}
//Now NULL the set
*pcProperties = 0;
SAFE_FREE(*prgProperties);
return hr;
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT FreeProperties
//
/////////////////////////////////////////////////////////////////////////////
HRESULT FreeProperties(ULONG* pcPropSets, DBPROPSET** prgPropSets)
{
ASSERT(pcPropSets);
ASSERT(prgPropSets);
HRESULT hr = S_OK;
//Loop over all the property sets
for(ULONG i=0; i<*pcPropSets; i++)
FreeProperties(&((*prgPropSets)[i].cProperties), &((*prgPropSets)[i].rgProperties));
//Now NULL the outer set
*pcPropSets = 0;
SAFE_FREE(*prgPropSets);
return hr;
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT FreeProperties
//
/////////////////////////////////////////////////////////////////////////////
HRESULT FreeProperties(ULONG* pcPropInfoSets, DBPROPINFOSET** prgPropInfoSets)
{
ASSERT(pcPropInfoSets);
ASSERT(prgPropInfoSets);
HRESULT hr = S_OK;
//Loop over all the property info sets
for(ULONG i=0; i<*pcPropInfoSets; i++)
{
//Free the inner variants first
for(ULONG j=0; j<(*prgPropInfoSets)[i].cPropertyInfos; j++)
{
//if DBPROPSTATUS_NOTSUPPORTED then vValue is undefined
if((*prgPropInfoSets)[i].rgPropertyInfos[j].dwFlags != DBPROPSTATUS_NOTSUPPORTED)
XTEST(NULL, hr = VariantClear(&((*prgPropInfoSets)[i].rgPropertyInfos[j].vValues)));
}
//Now free the set
SAFE_FREE((*prgPropInfoSets)[i].rgPropertyInfos);
}
//Now free the outer set
*pcPropInfoSets = 0;
SAFE_FREE((*prgPropInfoSets));
return hr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -