K
kingofhourse
Unregistered / Unconfirmed
GUEST, unregistred user!
1:VC的智能指针翻译问题。
HRESULT CheckStream(BOOL bVideo, BOOL *pVal)
{
HRESULT hr = S_FALSE;
long lParam = 0;
if(m_pFG)
{
if(bVideo)
{
CComQIPtr <IBasicVideo,&IID_IBasicVideo> pBV = m_pFG;
hr = pBV->get_BitRate(&lParam);
}
else
{
CComQIPtr <IBasicAudio,&IID_IBasicAudio> pBA = m_pFG;
hr = pBA->get_Balance(&lParam);
}
}
*pVal = (hr == S_OK);
return hr;
}
上例的CComQIPtr <IBasicAudio,&IID_IBasicAudio> pBA = m_pFG;
翻译成Delphi的话,是否直接定义即可?Delphi本身会类似C的智能指针一样自动管理计数器?
问题2:如果是,那么下面的例子中:
//find out specify filter from system
HRESULT FindFilter(IBaseFilter **ppSrcFilter, REFCLSID clsid, BSTR bstrFilter)
{
HRESULT hr = S_OK;
IBaseFilter *pSrc = NULL;
CComPtr <IMoniker> pMoniker = NULL;
CComPtr <IPropertyBag> pPropBag = NULL;
ULONG cFetched;
// Create the system device enumerator
CComPtr <ICreateDevEnum> pDevEnum =NULL;
hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
IID_ICreateDevEnum, (void ** ) &pDevEnum);
if (FAILED(hr))
{
ErrMsg("SystemDeviceEnum of Find filter",hr);
return hr;
}
// Create an enumerator for the video capture devices
CComPtr <IEnumMoniker> pClassEnum = NULL;
hr = pDevEnum->CreateClassEnumerator (clsid, &pClassEnum, 0);
if (FAILED(hr))
{
ErrMsg("CreateClassEnumerator of Find Filter",hr);
return hr;
}
// If there are no enumerators for the requested type, then
// CreateClassEnumerator will succeed, but pClassEnum will be NULL.
if (pClassEnum == NULL)
{
ErrMsg("Class Enum is NULL",S_FALSE);
return E_FAIL;
}
// Use the first video capture device on the device list,if filterName is NULL.
// Otherwise Search the specify filter in list.
// Note that if the Next() call succeeds but there are no monikers,
// it will return S_FALSE (which is not a failure). Therefore, we
// check that the return code is S_OK instead of using SUCCEEDED() macro.
while((pClassEnum->Next (1, &pMoniker, &cFetched)) == S_OK)
{
// Bind Moniker to a PropertyBag filter object
hr = pMoniker->BindToStorage(0,0,IID_IPropertyBag, (void**)&pPropBag);
if (FAILED(hr))
{
ErrMsg("BindToStorage of Find filter",hr);
return hr;
}
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"FriendlyName",&varName,0);
if(SUCCEEDED(hr))
{
if(_bstr_t(varName.bstrVal) == _bstr_t(bstrFilter) ||
_bstr_t(bstrFilter) == _bstr_t(""))
{
VariantClear(&varName);
hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc);
if(FAILED(hr))
{
ErrMsg("BindToObject of Find Filter",hr);
pSrc->Release();
return hr;
}
break;
}
}
VariantClear(&varName);
pPropBag = NULL;
pMoniker = NULL;
}
*ppSrcFilter = pSrc;
return S_OK;
}
pSrc->Release();既然是智能指针了,为什么这里要释放呢?翻译成Delphi的话,是否也需要调用呢?
HRESULT CheckStream(BOOL bVideo, BOOL *pVal)
{
HRESULT hr = S_FALSE;
long lParam = 0;
if(m_pFG)
{
if(bVideo)
{
CComQIPtr <IBasicVideo,&IID_IBasicVideo> pBV = m_pFG;
hr = pBV->get_BitRate(&lParam);
}
else
{
CComQIPtr <IBasicAudio,&IID_IBasicAudio> pBA = m_pFG;
hr = pBA->get_Balance(&lParam);
}
}
*pVal = (hr == S_OK);
return hr;
}
上例的CComQIPtr <IBasicAudio,&IID_IBasicAudio> pBA = m_pFG;
翻译成Delphi的话,是否直接定义即可?Delphi本身会类似C的智能指针一样自动管理计数器?
问题2:如果是,那么下面的例子中:
//find out specify filter from system
HRESULT FindFilter(IBaseFilter **ppSrcFilter, REFCLSID clsid, BSTR bstrFilter)
{
HRESULT hr = S_OK;
IBaseFilter *pSrc = NULL;
CComPtr <IMoniker> pMoniker = NULL;
CComPtr <IPropertyBag> pPropBag = NULL;
ULONG cFetched;
// Create the system device enumerator
CComPtr <ICreateDevEnum> pDevEnum =NULL;
hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
IID_ICreateDevEnum, (void ** ) &pDevEnum);
if (FAILED(hr))
{
ErrMsg("SystemDeviceEnum of Find filter",hr);
return hr;
}
// Create an enumerator for the video capture devices
CComPtr <IEnumMoniker> pClassEnum = NULL;
hr = pDevEnum->CreateClassEnumerator (clsid, &pClassEnum, 0);
if (FAILED(hr))
{
ErrMsg("CreateClassEnumerator of Find Filter",hr);
return hr;
}
// If there are no enumerators for the requested type, then
// CreateClassEnumerator will succeed, but pClassEnum will be NULL.
if (pClassEnum == NULL)
{
ErrMsg("Class Enum is NULL",S_FALSE);
return E_FAIL;
}
// Use the first video capture device on the device list,if filterName is NULL.
// Otherwise Search the specify filter in list.
// Note that if the Next() call succeeds but there are no monikers,
// it will return S_FALSE (which is not a failure). Therefore, we
// check that the return code is S_OK instead of using SUCCEEDED() macro.
while((pClassEnum->Next (1, &pMoniker, &cFetched)) == S_OK)
{
// Bind Moniker to a PropertyBag filter object
hr = pMoniker->BindToStorage(0,0,IID_IPropertyBag, (void**)&pPropBag);
if (FAILED(hr))
{
ErrMsg("BindToStorage of Find filter",hr);
return hr;
}
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"FriendlyName",&varName,0);
if(SUCCEEDED(hr))
{
if(_bstr_t(varName.bstrVal) == _bstr_t(bstrFilter) ||
_bstr_t(bstrFilter) == _bstr_t(""))
{
VariantClear(&varName);
hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc);
if(FAILED(hr))
{
ErrMsg("BindToObject of Find Filter",hr);
pSrc->Release();
return hr;
}
break;
}
}
VariantClear(&varName);
pPropBag = NULL;
pMoniker = NULL;
}
*ppSrcFilter = pSrc;
return S_OK;
}
pSrc->Release();既然是智能指针了,为什么这里要释放呢?翻译成Delphi的话,是否也需要调用呢?