你好我有一个AxisRates
对象,它是Rate
个对象的ActiveX集合,都是用Delphi编写的。
当我在VB中调用这个集合时,或者用这样的代码调用C#:
For Each objRate In objAxisRates
rem do something
Next
第一次没关系,但第二次没什么。我已经在我的代码中进行了跟踪,我看到第一次调用Get_NewEnum
并且在Next
方法之后。第二次调用Get_NewEnum
,就是这样!
在我的代码中,我首先初始化g_Rates
,这是一个AxisRates
对象:
g_Rates[i]:=TAxisRates.Create;
g_Rates[2].fcount:=0;
AxisRates
类型就是这样:
IAxisRates = interface(IEnumVARIANT)
['{1339E0E3-6FF7-4352-B6C4-1AEC52361915}']
function Get__NewEnum: IUnknown; safecall;
function Get_Count: Integer; safecall;
function Get_Item(Index: Integer): IRate; safecall;
property _NewEnum: IUnknown read Get__NewEnum;
property Count: Integer read Get_Count;
property Item[Index: Integer]: IRate read Get_Item; default;
end;
// *********************************************************************//
// DispIntf : IAxisRatesDisp
// Indicateurs : (320) Dual OleAutomation
// GUID : {1339E0E3-6FF7-4352-B6C4-1AEC52361915}
// *********************************************************************//
IAxisRatesDisp = dispinterface
['{1339E0E3-6FF7-4352-B6C4-1AEC52361915}']
property _NewEnum: IUnknown readonly dispid -4;
property Count: Integer readonly dispid 1;
property Item[Index: Integer]: IRate readonly dispid 0; default;
function Next(celt: LongWord; const rgvar: OleVariant; out pceltFetched: LongWord): HResult; dispid 1610678272;
function Skip(celt: LongWord): HResult; dispid 1610678273;
function Reset: HResult; dispid 1610678274;
function Clone(out ppenum: IEnumVARIANT): HResult; dispid 1610678275;
end;
type
TAxisRates = class(TAutoObject,IAxisRates,IEnumVariant)
private
fIndex:integer;
protected
function Get__NewEnum: IUnknown; safecall;
function Get_Count: Integer; safecall;
{ Protected declarations }
{ IEnumVARIANT }
function Next(celt: LongWord; var rgvar : OleVariant;
out pceltFetched: LongWord): HResult; stdcall;
function Skip(celt: LongWord): HResult; stdcall;
function Reset: HResult; stdcall;
function Clone(out Enum: IEnumVariant): HResult; stdcall;
function Get_Item(Index: Integer): IRate; safecall;
public
fCount,fAxe:integer;
procedure Initialize; override;
destructor Destroy; override;
end;
Get_NewEnum
和Next
的代码是:
function TAxisRates.Get__NewEnum: IUnknown;
begin
result:=g_Rates;
findex:=0;
end;
function TAxisRates.Next(celt: LongWord; var rgvar : OleVariant; out pCeltFetched : LongWord) : HRESULT;
var i:integer;
begin
i:=0;
while (i<integer(celt)) and (fIndex<fCount) do
begin
TVariantList(rgvar)[i]:=Get_Item(fIndex+1) as IDispatch;
inc(i);
inc(fIndex);
end;
if (@pCeltFetched <> nil) then pCeltFetched:=i;
if i=integer(celt) then result:=S_OK else result:=S_FALSE;
end;
我不明白我的错误在哪里,因为它第一次完美运行而不是第二次:-( 谢谢你的帮助,抱歉我的英语很差。
此致
米歇尔