简体   繁体   中英

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

I am attempting to use the Video Capture SDK from

DTK Software

in my Delphi App. 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.

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

so I must have done something right, but after the next line, in the debugger, VideoDevice is a nil .

Looking over the DTKVideoCapLib_TLB.pas , I see the following

 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. Right-click on item[i] in the IDE and selected Find Declaration takes me to

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

You should use as . Delphi will automatically attempt to obtain the desired interface for you. 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:

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

has type IVideoDevice . So you cannot cast it to TVideoDevice .

You need to correct the type of the variable:

var
  VideoDevice: IVideoDevice;

And then assign it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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