⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 property.cpp

📁 用测试OLE DB提供者的一个程序。能够测试出OEL DB提供者到底实现了哪些接口?很灵的。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// Microsoft OLE DB RowsetViewer
// Copyright (C) 1994 - 1998 By Microsoft Corporation.
//
// @doc
//
// @module PROPERTY.CPP
//
//-----------------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////
// Includes
//
////////////////////////////////////////////////////////////////////////
#include "Common.h"
#include "Property.h"



////////////////////////////////////////////////////////////////////////////
//  Find the Property within the PropSets and return a pointer to it
//
////////////////////////////////////////////////////////////////////////////
BOOL FindProperty(DBPROPID PropertyID, GUID guidPropertySet, ULONG cPropSets, DBPROPSET* rgPropSets, DBPROP** ppProp)
{
	//Will find the property with in the PropSets and return an index to it
	ASSERT(ppProp);

	//Loop over the number of property sets
	for(ULONG iSet=0; iSet<cPropSets; iSet++)
	{
		//Make sure where looking in the right property set
		if(guidPropertySet != rgPropSets[iSet].guidPropertySet)
			continue;

		//Loop over the array of properties in this set
		for(ULONG iProp=0; iProp<rgPropSets[iSet].cProperties; iProp++)
			if(rgPropSets[iSet].rgProperties[iProp].dwPropertyID == PropertyID)
			{	
				*ppProp = &rgPropSets[iSet].rgProperties[iProp];
				return TRUE;
			}
	}

	return FALSE;
}


/////////////////////////////////////////////////////////////////////////////
// HRESULT GetProperty
//
// Get the property information from the data source or object.
// propid specifies the property and propset specifies the property set to which
// propid belongs. The datatype of the property is required for the correct 
// coercion of the data
/////////////////////////////////////////////////////////////////////////////
HRESULT GetProperty(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet,
				   DBPROP** ppProperty)
{
	ASSERT(pIUnknown && ppProperty);
	*ppProperty = NULL;

	HRESULT hr = S_OK;
	
	IDBProperties* pIDBProperties = NULL;
	ICommandProperties* pICommandProperties = NULL;
	IRowsetInfo* pIRowsetInfo = NULL;

	ULONG		cPropSets = 0;
	DBPROPSET*	rgPropSets = NULL;
		
	const ULONG		cPropertyIDSets = 1;
	DBPROPIDSET		rgPropertyIDSets[cPropertyIDSets];
	
	//SetUp input DBPROPIDSET struct (all static)
	rgPropertyIDSets[0].cPropertyIDs = cPropertyIDSets;
	rgPropertyIDSets[0].rgPropertyIDs = &PropertyID;
	rgPropertyIDSets[0].guidPropertySet = guidPropertySet;
	
	//Need to figure out which Interface was passed in
	//IDBInitialize, or ICommand, or IRowset, all three allow GetProperties
	if(SUCCEEDED(hr = pIUnknown->QueryInterface(IID_IDBProperties,(void **)&pIDBProperties)))
	{
		//GetProperties	
		//Property might not be supported
		hr = pIDBProperties->GetProperties(cPropertyIDSets, rgPropertyIDSets, &cPropSets, &rgPropSets);
	}
	else if(SUCCEEDED(hr = pIUnknown->QueryInterface(IID_ICommandProperties,(void **)&pICommandProperties)))
	{
		//GetProperties	
		//Property might not be supported
		hr = pICommandProperties->GetProperties(cPropertyIDSets, rgPropertyIDSets, &cPropSets, &rgPropSets);
	}
	else
	{
		XTESTC(NULL, hr = pIUnknown->QueryInterface(IID_IRowsetInfo, (void **)&pIRowsetInfo));
					
		//GetProperties	
		//Property might not be supported
		hr = pIRowsetInfo->GetProperties(cPropertyIDSets, rgPropertyIDSets, &cPropSets, &rgPropSets);
	}
		
	//Check return result
	//Can be DB_S_ / DB_E_ERRORSOCCURRED if notsupported property...
	if(FAILED(hr) && hr != DB_E_ERRORSOCCURRED)
		XTESTC(NULL, hr);

	//Verify results
	CHECKC(cPropSets==1 && rgPropSets!=NULL);
	CHECKC(rgPropSets[0].cProperties==1 && rgPropSets[0].rgProperties!=NULL);
	CHECKC(rgPropSets[0].rgProperties[0].dwPropertyID == PropertyID);

	//Return the property to the user
	*ppProperty = rgPropSets[0].rgProperties;

CLEANUP:	
	//Just Free the outer Struct, 
	//since we return the inner Property for the user to free
	SAFE_FREE(rgPropSets);
	
	SAFE_RELEASE(pIDBProperties);
	SAFE_RELEASE(pICommandProperties);
	SAFE_RELEASE(pIRowsetInfo);
	return hr;
}

/////////////////////////////////////////////////////////////////////////////
// HRESULT GetProperty
//
/////////////////////////////////////////////////////////////////////////////
HRESULT GetProperty(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet,	WCHAR** ppwszValue)
{
	ASSERT(pIUnknown && ppwszValue);
	HRESULT hr;

	ULONG cProperties = 1;
	DBPROP* pProperty = NULL;
	*ppwszValue = NULL;

	//Property might not be supported
	hr = GetProperty(pIUnknown, PropertyID, guidPropertySet, &pProperty);

	//Copy the value
	//GetProperty might return VT_EMPTY for no default string
	if(SUCCEEDED(hr) && pProperty && pProperty->vValue.vt == VT_BSTR)
		(*ppwszValue) = wcsDuplicate(V_BSTR(&pProperty->vValue));

	if(*ppwszValue == NULL)
	{
		SAFE_ALLOC(*ppwszValue, WCHAR, 1);
		(*ppwszValue)[0] = wEOL;
	}

CLEANUP:
	FreeProperties(&cProperties, &pProperty);
	return hr;
}

/////////////////////////////////////////////////////////////////////////////
// HRESULT GetProperty
//
/////////////////////////////////////////////////////////////////////////////
HRESULT GetProperty(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet,	WCHAR* pwszValue)
{
	ASSERT(pIUnknown && pwszValue);
	HRESULT hr;

	ULONG cProperties = 1;
	DBPROP* pProperty = NULL;
	*pwszValue = wEOL;

	//Might not be supported
	hr = GetProperty(pIUnknown, PropertyID, guidPropertySet, &pProperty);
	
	//Copy the value
	//GetProperty might return VT_EMPTY for no default string
	if(SUCCEEDED(hr) && pProperty && pProperty->vValue.vt == VT_BSTR)
		wcscpy(pwszValue, V_BSTR(&pProperty->vValue));

	FreeProperties(&cProperties, &pProperty);
	return hr;
}

/////////////////////////////////////////////////////////////////////////////
// HRESULT GetProperty
//
/////////////////////////////////////////////////////////////////////////////
HRESULT GetProperty(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet,	ULONG* pcValue)
{
	ASSERT(pIUnknown && pcValue);
	HRESULT hr;

	ULONG cProperties = 1;
	DBPROP* pProperty = NULL;
	*pcValue = 0;

	//Might not be supported
	hr = GetProperty(pIUnknown, PropertyID, guidPropertySet, &pProperty);

	//Copy the value
	//GetProperty might return VT_EMPTY for no default string
	if(SUCCEEDED(hr) && pProperty && pProperty->vValue.vt == VT_I4)
		*pcValue = V_I4(&pProperty->vValue);

	FreeProperties(&cProperties, &pProperty);
	return hr;
}
				   
/////////////////////////////////////////////////////////////////////////////
// HRESULT GetProperty
//
/////////////////////////////////////////////////////////////////////////////
HRESULT GetProperty(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet,	BOOL* pbValue)
{
	ASSERT(pIUnknown && pbValue);
	HRESULT hr;

	ULONG cProperties = 1;
	DBPROP* pProperty = NULL;
	*pbValue = FALSE;

	//Might not be supported
	hr = GetProperty(pIUnknown, PropertyID, guidPropertySet, &pProperty);

	//Copy the value
	//GetProperty might return VT_EMPTY for no default string
	if(SUCCEEDED(hr) && pProperty && pProperty->vValue.vt == VT_BOOL)
		*pbValue = V_BOOL(&pProperty->vValue) == VARIANT_TRUE;

	FreeProperties(&cProperties, &pProperty);
	return hr;
}


/////////////////////////////////////////////////////////////////////////////
// HRESULT GetPropInfo
//
/////////////////////////////////////////////////////////////////////////////
HRESULT GetPropInfo(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet, DBPROPINFO** ppPropInfo)
{
	if(ppPropInfo)
		*ppPropInfo = NULL;

	if(pIUnknown == NULL)
		return E_FAIL;	
	
	HRESULT hr;
	IDBProperties* pIDBProperties = NULL;

	const ULONG cPropIDSets = 1;
	DBPROPIDSET rgPropIDSets[cPropIDSets];

	ULONG cPropInfoSets = 0;
	DBPROPINFOSET* rgPropInfoSets = NULL;
	
	//Construct the DBPROPIDSET structure
	rgPropIDSets[0].cPropertyIDs = 1;
	rgPropIDSets[0].rgPropertyIDs = &PropertyID;
	rgPropIDSets[0].guidPropertySet = guidPropertySet;
		
	//Obtain IDBProperties
	TESTC(hr = pIUnknown->QueryInterface(IID_IDBProperties,(void**)&pIDBProperties));
	
	//Don't display dialog if errors occurred.
	//Errors can occur if properties are not supported by the provider
	TESTC(hr = pIDBProperties->GetPropertyInfo(cPropIDSets,rgPropIDSets,&cPropInfoSets,&rgPropInfoSets,NULL));
	
CLEANUP:
	*ppPropInfo = rgPropInfoSets ? rgPropInfoSets[0].rgPropertyInfos  : NULL;
 	SAFE_FREE(rgPropInfoSets);
	SAFE_RELEASE(pIDBProperties);
	return hr;
}


/////////////////////////////////////////////////////////////////////////////
// DBPROPFLAGS GetPropInfoFlags
//
/////////////////////////////////////////////////////////////////////////////
DBPROPFLAGS GetPropInfoFlags(IUnknown* pIUnknown, DBPROPID PropertyID, GUID guidPropertySet)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -