繁体   English   中英

如何在Delphi XE2中将IDispatch转换为TOleServer?

[英]How to cast an IDispatch as a TOleServer in Delphi XE2?

我正在尝试从以下位置使用Video Capture SDK

DTK软件

在我的Delphi应用中。 他们提供的唯一真正帮助是如何导入其类型库! 我成功完成了此操作,并且在我的项目中有一个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请向我显示Microsoft 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中的item [i],然后选择“查找声明”将我带到

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

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

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

但是,您的更新提供了我撰写原始答案时未提供的更多详细信息。 该更新包括指示Videodevices已经包含IVideoDevice ,因此您根本不需要IVideoDevice -您只需要适当的变量声明即可:

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

具有IVideoDevice类型。 因此,您不能将其TVideoDeviceTVideoDevice

您需要更正变量的类型:

var
  VideoDevice: IVideoDevice;

然后像这样分配它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM