【问题标题】:How to inherit Interface如何继承接口
【发布时间】:2015-07-05 15:05:48
【问题描述】:

我从dll 创建了一个 tlb 库。所以我有一些com 对象。 我想扩展接口,但我不能。

_Ckdu_cache:

// *********************************************************************//
// Interface: _Ckdu_cache
// Flags:     (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
// GUID:      {0137B6A2-76D0-3E27-A13C-557542A3AAD5}
// *********************************************************************//
   _Ckdu_cache = interface(IDispatch)
['{0137B6A2-76D0-3E27-A13C-557542A3AAD5}']
function Get_ToString: WideString; safecall;
function Equals(obj: OleVariant): WordBool; safecall;
function GetHashCode: Integer; safecall;
function GetType: _Type; safecall;
function close: Byte; safecall;
function get_capabilities: Integer; safecall;
function read(buf: PSafeArray; num_bytes: Integer): Integer; safecall;
function seek(offset: Int64): Byte; safecall;
function get_pos: Int64; safecall;
function set_tileheader_scope(tnum: Integer; num_tiles: Integer): Byte; safecall;
function set_precinct_scope(unique_id: Int64): Byte; safecall;
procedure Dispose; safecall;
function add_to_databin(databin_class: Integer; codestream_id: Int64; databin_id: Int64; 
                        data: PSafeArray; offset: Integer; num_bytes: Integer; is_final: Byte; 
                        add_as_most_recent: Byte; mark_if_augmented: Byte): PByte1; safecall;

Cockdu_cache :

// *********************************************************************//
// The Class CoCkdu_cache provides a Create and CreateRemote method to          
// create instances of the default interface _Ckdu_cache exposed by              
// the CoClass Ckdu_cache. The functions are intended to be used by             
// clients wishing to automate the CoClass objects exposed by the         
// server of this typelibrary.                                            
// *********************************************************************//
  CoCkdu_cache = class
    class function Create: _Ckdu_cache;
    class function CreateRemote(const MachineName: string): _Ckdu_cache;
  end;

我的班级:

type
  TKduCache = class(CoCkdu_cache)

我的构造函数:

constructor TKduCache.Create(_targetID, _cachePath: String);
var
  cache : _Ckdu_cache;
begin
  cache:= inherited Create;
end;

错误: 带有消息“不支持接口”的 EIntfCastError

我创建了另一个没有扩展的方法,如下:cache:= Cockdu_cache.Create; 但我得到了同样的错误。我按照这种方法创建了另一个 COM 对象,它们可以工作。但这对我不起作用。如何使用 _Ckdu_cache 方法?

补充:

unit kducache;

interface

uses imagecachestatus, jpipdatasegment, kdu_mni_TLB, globalvalues, SysUtils, Dialogs,
    System.Classes, System.Variants, Winapi.ActiveX, jpipdatabinclass,
  jpipresponse;

type
  TIntArray = array of Integer;
type
  TKduCache = class
  private
    { private declarations }
    //The cache file to use. Null if its not using a chace file.
    cacheFile     : TFileStream;
    Flags         : Word;
    cachePath     : String;
    status        : IImageCacheStatus;
    //The targetID for the image as given by the JPIP server.
    //Should be a unique hash for the image and thus serves
    //as a good way of naming the cache file.
    targetID      : String;
    //The amount of new data placed in this object via the
    //addDataSegment method starting after the initial
    //readCacheFromFile method.
    newData       : Integer;
    //This flags indicates if the server has to be loaded/saved to disk.
    iamPersistent : Boolean;
    class var maxCacheSize  : LongInt;
    cacheExists   : Boolean;

    class function uByteToInt(_x : array of Byte) : TIntArray;
    class function getCacheFiles(_cachePath : String) : TStringStream;
  protected
    { protected declarations }
  public
    FCache: _Ckdu_cache;
    { public declarations }
    procedure setImageCacheStatus(imageCacheStatus : IImageCacheStatus);
    procedure setInitialScope;
    procedure addDataSegment(_data : TJPIPDataSegment);
    class procedure updateCacheDirectory(_cachePath : String; maxSize : Double); overload;
    class procedure updateCacheDirectory(_cachePath : String); overload;
    function getNewDataSize : Integer;
    function getTotalDataSize : Integer;
    function isDataBinCompleted(_binClass : TJPIPDatabinClass; _streamID : Integer; _binID : Integer) : Boolean;
    function Close : Boolean;
    function addJPIPResponseData(jRes : TJPIPResponse) : Boolean;
    function writeCacheToFile : Boolean;
    function readCacheFromFile : Boolean;
    function buildCacheModelUpdateString(force : Boolean) : String;
    function getCacheFile : TStringStream;
    constructor Create(_targetID : String; _cachePath : String); virtual;
//    constructor Create(_targetID : String; _cachePath : String; _iamPersistent : Boolean); overload;
  published
    { published declarations }
  end;
implementation

{ TJHV_Kdu_Cache }

{ TKduCache }

//Main constructor used when you want to use a cache file.
constructor TKduCache.Create(_targetID, _cachePath: String);
begin
  FCache:= CoCkdu_cache.Create;
  targetID:= _targetID;
  cachePath:= _cachePath + '\' + targetID + '.cache';
  cacheExists:= true;
  Flags:= fmOpenReadWrite;
  if _cachePath = '' then
    cacheExists:= false
  else
  begin
    Flags := fmCreate;
    cacheFile:= TFileStream.Create(cachePath, Flags);
  end;
  newData:= 0;

  cacheFile.Free;
  if cacheExists then
    readCacheFromFile;
end;

我在哪里打电话:

TKduCache.Create(jpipTargetID, 'C:\Users\myComputer\Desktop\WorkStation\cache');

【问题讨论】:

  • 实现接口的对象的实现存在于 COM 服务器中。您不能使用类继承来扩展它。在不确切知道您要达到的目标的情况下,很难推荐前进的方向。
  • 我正在尝试使用一些方法,例如 add_to_databin 方法来缓存 jpeg2000。我正在使用 kakadu sdk,但它没有 delphi 库。只是dll文件。所以我将dll文件转换为tlb。我可以在其他类中使用方法。但是为此,我得到了错误。

标签: delphi


【解决方案1】:

您无需使用继承。不要声明派生类。像这样创建接口的实例:

cache := CoCkdu_cache.Create;

然后直接调用cache上的方法。

【讨论】:

  • 我总是喜欢你的代码。但是这个类给出了如上所述的错误。那个错误是什么意思,你能解释一下吗:/
  • 你问题中的代码不是这样写的。在您的问题中,您询问继承,并使用无效的cache := inherited Create。如果你问一件事,然后期待另一件事的答案,这很困难。你为什么不问你原来的问题?
  • 我的意思是,我尝试了两种方法。第一个是继承,另一个是,就像你的一样。但是编译器给了我同样的错误。直接调用或继承调用不会改变任何东西,它们都发生相同的错误。
  • 鉴于您问题中的代码,我的答案中的代码不会导致EIntfCastError。如果您遇到EIntfCastError,那么您的代码在某些方面有所不同。
  • CoCkdu_cache 只不过是一个为您提供接口引用的工厂,正如前面所说,没有必要从它继承。你可以写something like this
猜你喜欢
  • 1970-01-01
  • 2021-04-10
  • 2019-12-22
  • 2020-09-09
  • 2010-09-21
  • 2015-10-12
相关资源
最近更新 更多