我正在尝试访问来自ActiveX库的回调中的OleVariant.
以下是在TLB中定义事件处理程序的内容:
procedure(ASender: TObject; var structQSnap: {??structVTIQSnap}OleVariant) of object;
这是TLB中structVTIQSnap的定义:
structVTIQSnap = packed record bstrSymbol: WideString; bstrListingExch: WideString; bstrLastExch: WideString; fLastPrice: Double; nLastSize: Integer; bstrBbo: WideString; bstrBidExch: WideString; fBidPrice: Double; nBidSize: Integer; bstrAskExch: WideString; fAskPrice: Double; nAskSize: Integer; fHighPrice: Double; fLowPrice: Double; fOpenPrice: Double; fClosePrice: Double; nCumVolume: Integer; bstrTradeCondition: WideString; nQuoteCondition: Integer; bstrCompanyName: WideString; f52WeekHigh: Double; f52WeekLow: Double; fEps: Double; nSharesOutstanding: Integer; nSpCode: Integer; fBeta: Double; bstrExDivDate: WideString; nDivFreq: Integer; fDivAmt: Double; nAvgVolume: Integer; bstrCusip: WideString; fVwap: Double; bstrUpdateTime: WideString; bstrExch: WideString; nSharesPerContract: Integer; end;
它编译得很好,但每次我尝试访问bstrSymbol时,我都会得到一个"Invalid Variant Operation":
procedure TForm1.HandleVTIQuoteSnap(ASender: TObject; var structQSnap: OleVariant); var symbol: WideString; begin symbol := structQSnap.bstrSymbol; // this line causes the exception end;
如何在Delphi中访问structQSnap及其属性?
在C#中,此函数适用于事件处理程序:
void vtiQ_OnVTIQSnap(ref vtiLib.structVTIQSnap structQSnap) { MessageBox.Show("Got qsnap for " + structQuoteSnap.bstrSymbol); }
有任何想法吗?
我认为Delphi的ActiveX导入向导不知道如何正确处理structVTIQSnap类型(这似乎是一个记录),只使用默认的OleVariant.在生成的..._ TLB.pas文件中是否有一个名为structVTIQSnap或类似的类型声明?尝试使用它而不是OleVariant,例如
procedure (ASender: TObject; var structQSnap: structVTIQSnap) of object;
该类型可能会被声明为"打包记录".