2015-07-11 57 views
0

我试图在我的Delphi应用程序使用视频采集SDK从如何在Delphi XE2中将IDispatch转换为TOleServer?

DTK Software

。他们提供的唯一真正的帮助是如何导入他们的类型库!我做了这个成功,并在我的项目中有一个DTKVideoCapLib_TLB.pas

我明白了这一点。

procedure TForm1.FormCreate(Sender: TObject); 
var 
    i: Integer; 
    s: String; 
    VideoCaptureUtils: TVideoCaptureUtils; 
    VideoDevice: TVideoDevice; 
begin 
    VideoCaptureUtils := TVideoCaptureUtils.Create(Self); 
    for i := 0 to VideoCaptureUtils.VideoDevices.Count - 1 do 
    begin 
     s := VideoCaptureUtils.VideoDevices.Item[i].Name; 
     ShowMessage(s); 
     VideoDevice := TVideoDevice(VideoCaptureUtils.Videodevices.Item[i]); 
    end; 

ShowMessage好心显示我微软的LifeCam VX-800

所以我必须做一些正确的事情,但下一行后,在调试器,VideoDevicenil

看在DTKVideoCapLib_TLB.pas,我看到了下面

TVideoDevice = class(TOleServer) 
    private 
    FIntf: IVideoDevice; 
    function GetDefaultInterface: IVideoDevice; 
    protected 
    ... 

    IVideoDevice = interface(IDispatch) 
    ['{8A40EA7D-692C-40EE-9258-6436D1724739}'] 
    function Get_Name: WideString; safecall; 
    ... 

所以,现在,我真的没有关于如何执行该方法的想法?

更新

更正项[0],在问题项[I]。在IDE中的项目[i]和选择查找宣言右键单击需要我

type 
    IVideoDeviceCollection = interface(IDispatch) 
    ... 
    property Item[index: Integer]: IVideoDevice read Get_Item; 
    ... 
    end; 
+2

未经检查的强制类型转换通常是不好的。你怎么知道'VideoCaptureUtils.Videodevices.Item [0]'确实属于'TVideoDevice'类型?为什么使用索引'0'? 'VideoCaptureUtils.Videodevices.Item [i]'是什么类型? –

回答

3

您应该使用as。 Delphi将自动尝试为您获取所需的界面。 (!未经测试)像这样的东西应该工作:

var 
    VideoDevice: IVideoDevice; // note the type of the variable 
.... 
VideoDevice := VideoCaptureUtils.Videodevices.Item[0] as IVideoDevice; 

你的更新,但是,提供当我写我原来的答复中不存在更多的细节。这更新包括代码,表示Videodevices已经包含IVideoDevice,所以你不需要投在所有 - 你只需要正确的变量声明:

var 
    VideoDevice: IVideoDevice; // note the type of the variable 
.... 
VideoDevice := VideoCaptureUtils.Videodevices.Item[i]; 
+0

该行产生编译器错误:_ [DCC错误] Unit1.pas(65):E2010不兼容的类型:'TVideoDevice'和'IVideoDevice'_,而这行'VideoDevice:= VideoCaptureUtils.Videodevices.Item [0]作为TVideoDevice; '编译但产生了一个运行时错误“EIntfCastError:接口不支持” – nolaspeaker

+0

好吧,我上面的答复也有点快。一旦我将VideoDevice类型更改为IVideoDevice,问题就解决了。我将其标记为答案。 – nolaspeaker

+0

根据David Heffernen的回答,如果类型是IVideoDevice,则演员(如IVideoDevice)是多余的。你想纠正你的答案吗? – nolaspeaker

1
VideoCaptureUtils.Videodevices.Item[i] 

具有类型IVideoDevice。所以你不能将它投射到TVideoDevice

您需要更正变量的类型:

var 
    VideoDevice: IVideoDevice; 

然后分配给它这样的:

VideoDevice := VideoCaptureUtils.VideoDevices.Item[i]; 
+0

那我接下来呢,那个人也不能把TVideoDevice当成IVideoDevice呢?这解释了为什么Tx = Class(TOleServer)类,都具有:private ... function GetDefaultInterface:Ix方法,对不对? – nolaspeaker

+0

如果你确实有'TVideoDevice',那么你肯定可以从中获得'IVideoDevice'。但你有一个'IVideoDevice'。这不是你需要的吗? –

+0

就具体问题而言,是的!在使用库的其他功能方面,没有。大多数情况下,我将使用x:= Tx.Create(nil); y.Ix:= x.GetDefaultInterface,你看? – nolaspeaker