I have a COM object with an interface containing many properties defined as follows:
[propget] HRESULT Width([out, retval] LONG *lValue);
To access this property from C ++, I need to add the following code:
LONG lValue;
HRESULT hr = pInterface->get_Width(&lValue);
if (FAILED(hr)) lValue = DEFAULT_VALUE;
This block is not too long, but when many properties are used, the code becomes not very good. Is there a way to split the property access code into some functions of a macro or template in order to be able to use the properties directly, for example:
printf("The width of the object is %d", GET_OBJECT_PROPERTY(pInterace, Width, DEFAULT_VALUE));
UPD: VC2008 compiler is used to create a project
UPD: Thanks everyone! Here is my solution:
template <class interface_type, class property_type>
property_type GetPropertyValue(interface_type* pInterface, HRESULT(STDMETHODCALLTYPE interface_type::*pFunc)(property_type*), property_type DefaultValue = 0)
{
property_type lValue;
HRESULT hr = (*pInterface.*pFunc)(&lValue);
if (FAILED(hr))
lValue = DefaultValue;
return lValue;
}
which can be called
LONG lVideoStreamCount = GetPropertyValue(pInfo, &IInterfaceName::get_VideoStreamCount);
I'm still looking for a way to remove this part of "IInterfaceName ::" from the call.
source
share