[英]How to cast an IDispatch as a TOleServer in Delphi XE2?
I am attempting to use the Video Capture SDK from 我正在尝试从以下位置使用Video Capture SDK
in my Delphi App. 在我的Delphi应用中。 The only real help they provide, is how to import their type library! 他们提供的唯一真正帮助是如何导入其类型库! I did this succesfully and have a DTKVideoCapLib_TLB.pas in my project. 我成功完成了此操作,并且在我的项目中有一个DTKVideoCapLib_TLB.pas 。
I got this far. 我走了这么远。
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 kindly displays me Microsoft LifeCam VX-800 ShowMessage请向我显示Microsoft LifeCam VX-800
so I must have done something right, but after the next line, in the debugger, VideoDevice is a nil
. 所以我一定做对了,但是在调试器的下一行之后, VideoDevice是nil
。
Looking over the DTKVideoCapLib_TLB.pas , I see the following 查看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;
...
So now, I really have no idea about how to proceed with this? 所以现在,我真的不知道该如何进行?
Update 更新
Corrected item[0] to item[i] in the question. 将问题中的项目[0]纠正为项目[i]。 Right-click on item[i] in the IDE and selected Find Declaration takes me to 右键单击IDE中的item [i],然后选择“查找声明”将我带到
type
IVideoDeviceCollection = interface(IDispatch)
...
property Item[index: Integer]: IVideoDevice read Get_Item;
...
end;
You should use as
. 您应该使用as
。 Delphi will automatically attempt to obtain the desired interface for you. Delphi将自动尝试为您获取所需的界面。 Something like this (untested!) should work: 这样的东西(未经测试!)应该可以工作:
var
VideoDevice: IVideoDevice; // note the type of the variable
....
VideoDevice := VideoCaptureUtils.Videodevices.Item[0] as IVideoDevice;
Your update, however, provides more detail that weren't present when I wrote my original answer. 但是,您的更新提供了我撰写原始答案时未提供的更多详细信息。 That update includes code that indicates that Videodevices
already contains IVideoDevice
, so you don't need the cast at all - you just need the proper variable declaration: 该更新包括指示Videodevices
已经包含IVideoDevice
,因此您根本不需要IVideoDevice
-您只需要适当的变量声明即可:
var
VideoDevice: IVideoDevice; // note the type of the variable
....
VideoDevice := VideoCaptureUtils.Videodevices.Item[i];
VideoCaptureUtils.Videodevices.Item[i]
has type IVideoDevice
. 具有IVideoDevice
类型。 So you cannot cast it to TVideoDevice
. 因此,您不能将其TVideoDevice
为TVideoDevice
。
You need to correct the type of the variable: 您需要更正变量的类型:
var
VideoDevice: IVideoDevice;
And then assign it like this: 然后像这样分配它:
VideoDevice := VideoCaptureUtils.VideoDevices.Item[i];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.