以下是C示例代码的一部分(无错误处理):
#include <vidcap.h> // For IKsTopologyInfo #include <ksproxy.h> // For IKsControl .... //pKsTopologyInfo is passed from the outside IKsControl *pKsControl = NULL; DWORD dwNumNodes = 0; pKsTopologyInfo->get_NumNodes(&dwNumNodes); for(unsigned int i = 0; i < dwNumNodes; i++) { pKsTopologyInfo->get_NodeType(i, &guidNodeType); if(IsEqualGUID(guidNodeType, KSNODETYPE_DEV_SPECIFIC)) { hr = pKsTopologyInfo->CreateNodeInstance(i, IID_IKsControl, (void **)&pKsControl);
这是Delphi中我的代码的相应部分:
//KsTopologyInfo is IKsTopologyInfo passed from the outside //pKsControl is ^IKsControl, which is taken from DirectShow9.pas from DSPack components set. KsTopologyInfo.get_NumNodes(@dwNumNodes); for i:=0 to dwNumNodes-1 do begin KsTopologyInfo.get_NodeType(i,@guidNodeType); if IsEqualGUID(guidNodeType,KSNODETYPE_DEV_SPECIFIC) then begin KsTopologyInfo.CreateNodeInstance(i,IID_IKsControl,@pKsControl);
这里的错误发生在最后一行的Delphi代码中,而在C中它可以正常工作.
逐步调试期间的检查没有显示任何差异 – 在Delphi和C中,dwNumNodes得到11,然后当i == 3和guidNodeType == {941C7AC0-C559-11D0-8A2B-00A0C9255AC1}时,IsEqualGUID返回TRUE.因此,在这两种情况下,它都使用相同的值i = 3调用CreateNodeInstance,并且IID_IKsControl = {28F54685-06FD-11D2-B27A-00A0C9223196}
遗憾的是,我找不到vidcap.h转换为Delphi .pas文件,所以我为IKsTopologyInfo写了我自己的描述,我想我在CreateNodeInstance方法描述中有错误.这是来自vidcap.h:
virtual HRESULT STDMETHODCALLTYPE CreateNodeInstance( /* [in] */ DWORD dwNodeId, /* [in] */ REFIID iid, /* [out] */ void **ppvObject) = 0;
这是Delphi的变种:
function CreateNodeInstance(dwNodeId:DWord; iid:TGuid; p:Pointer):HRESULT; stdcall;
我用第三个参数 – var Obj,Pointer,PPointer(它是^ Pointer)尝试了很多变体,并尝试将不同的变量值传递给它(IKsControl,^ IKsControl,^(^ IKsControl),指针,^指针 – 并且它们都不起作用.无论如何我得到了同样的错误.
需要有关如何使其工作的任何建议,以及如何看起来正确的IKsTopologyInfo.CreateNodeInstance描述并在Delphi中调用.
根据MSDN definition of REFIID:typedef GUID IID; typedef IID* REFIID;
因此REFIID是指向GUID的指针,应该被翻译为PGUID,或者,因为iid是输入参数并且似乎是必需的,所以也可以作为const TGUID传递.
此外,MSDN声明在CreateNodeInstance:
ppvObject receives a pointer to the requested interface on the node object. The caller must release the interface.
所以第三个参数应该被声明为一个指向接口的指针,以便Delphi能够管理它.将其声明为var IUnknown,或者作为输出参数,将其声明为IUnknown会强制参数为IUnknown类型的变量,因此必须将其转换为实际接口类型才能使用它.
C声明
virtual HRESULT STDMETHODCALLTYPE CreateNodeInstance( /* [in] */ DWORD dwNodeId, /* [in] */ REFIID iid, /* [out] */ void **ppvObject) = 0;
变
function CreateNodeInstance(dwNodeId: DWord; const iid: TGUID; ppvObject: PUnknown): HRESULT; stdcall;
可以如下调用:
var KsControl: IKsControl; KsTopologyInfo.CreateNodeInstance(i, IID_IKsControl, @KsControl);