非VC+Direct高手莫入(100分)

  • 主题发起人 主题发起人 kingofhourse
  • 开始时间 开始时间
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,&amp;IID_IBasicVideo> pBV = m_pFG;
hr = pBV->get_BitRate(&amp;lParam);
}
else

{
CComQIPtr <IBasicAudio,&amp;IID_IBasicAudio> pBA = m_pFG;
hr = pBA->get_Balance(&amp;lParam);
}
}

*pVal = (hr == S_OK);
return hr;
}


上例的CComQIPtr <IBasicAudio,&amp;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 ** ) &amp;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, &amp;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, &amp;pMoniker, &amp;cFetched)) == S_OK)
{
// Bind Moniker to a PropertyBag filter object
hr = pMoniker->BindToStorage(0,0,IID_IPropertyBag, (void**)&amp;pPropBag);
if (FAILED(hr))
{
ErrMsg("BindToStorage of Find filter",hr);
return hr;
}

VARIANT varName;
VariantInit(&amp;varName);
hr = pPropBag->Read(L"FriendlyName",&amp;varName,0);
if(SUCCEEDED(hr))
{
if(_bstr_t(varName.bstrVal) == _bstr_t(bstrFilter) ||
_bstr_t(bstrFilter) == _bstr_t(""))
{
VariantClear(&amp;varName);
hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&amp;pSrc);
if(FAILED(hr))
{
ErrMsg("BindToObject of Find Filter",hr);
pSrc->Release();
return hr;
}
break;
}
}
VariantClear(&amp;varName);
pPropBag = NULL;
pMoniker = NULL;
}

*ppSrcFilter = pSrc;
return S_OK;
}

pSrc->Release();既然是智能指针了,为什么这里要释放呢?翻译成Delphi的话,是否也需要调用呢?
 
很久不用 delphi 了。

1. Delphi中的所有 Interface 应该都是自动计数的.

不过如果你的代码如果涉及到和VC等代码传递接口指针, 有可能需要注意形式. 你必须要让DELPHI知道确实是一个接口指针, 他才会自动进行计数操作.

every thing is in dbging!

2.
IBaseFilter *pSrc = NULL;
pSrc->Release();

这个接口指针 pSrc 明显不是智能指针嘛, 你在想什么?
 
噢,没看清楚。。。
1:CComQIPtr, CComPtr有什么区别?
2:能不能把上面两个函数翻译成Delph的?
 
接受答案了.
 
后退
顶部