> А может можно какой нибудь примерчик???
А может можно почитать MSDN по ключевым словам IDispatch, Invoke, IEnumVariant и далее со всеми остановками по ссылкам "See Also"?
Дарю, ффтыкай:
unit jsobj;
interface
uses
ActiveX;
type
TJSArraySortFunc = function(elt1, elt2: OleVariant): Integer;
TJSArraySortMethod = function(elt1, elt2: OleVariant): Integer of object;
TJSArrayWrapper = class
private
FObject: OleVariant;
FObjectInterface: IDispatch;
function GetItem(Index: Integer): OleVariant;
procedure SetItem(Index: Integer; const Value: OleVariant);
function GetLength: Integer;
public
constructor Create(const AObject: OleVariant);
property AsVariant: OleVariant read FObject;
property AsInterface: IDispatch read FObjectInterface;
property Item[Index: Integer]: OleVariant read GetItem write SetItem; default;
function GetEnum: IEnumVARIANT;
property Length: Integer read GetLength;
end;
implementation
uses
Windows,
SysUtils,
ComObj;
const
NullGuid: TGuid = '';
NullParams: DISPPARAMS = (rgvarg: nil; rgdispidNamedArgs: nil; cArgs: 0; cNamedArgs: 0);
constructor TJSArrayWrapper.Create(const AObject: OleVariant);
begin
FObject := AObject;
FObjectInterface := IDispatch(FObject);
end;
function TJSArrayWrapper.GetEnum: IEnumVARIANT;
var
Res: OleVariant;
begin
OleCheck(
FObjectInterface.Invoke(
DISPID_NEWENUM,
NullGuid,
GetSystemDefaultLCID,
DISPATCH_METHOD or DISPATCH_PROPERTYGET,
NullParams,
@Res,
nil,
nil
)
);
Result := IUnknown(Res) as IEnumVARIANT;
end;
function TJSArrayWrapper.GetItem(Index: Integer): OleVariant;
var
Member: WideString;
DispId: DWORD;
begin
Member := IntToStr(Index);
OleCheck(
FObjectInterface.GetIDsOfNames(
NullGuid,
@Member,
1,
GetSystemDefaultLCID,
@DispId
)
);
OleCheck(
FObjectInterface.Invoke(
DispId,
NullGuid,
GetSystemDefaultLCID,
DISPATCH_PROPERTYGET,
NullParams,
@Result,
nil,
nil
)
);
end;
function TJSArrayWrapper.GetLength: Integer;
begin
Result := FObject.length;
end;
procedure TJSArrayWrapper.SetItem(Index: Integer; const Value: OleVariant);
const
ParamId: TDispId = DISPID_PROPERTYPUT;
var
Member: WideString;
DispId: TDispId;
Params: DISPPARAMS;
begin
Member := IntToStr(Index);
OleCheck(
FObjectInterface.GetIDsOfNames(
NullGuid,
@Member,
1,
GetSystemDefaultLCID,
@DispId
)
);
Params.rgvarg := @Value;
Params.rgdispidNamedArgs := @ParamId;
Params.cArgs := 1;
Params.cNamedArgs := 1;
OleCheck(
FObjectInterface.Invoke(
DispId,
NullGuid,
GetSystemDefaultLCID,
DISPATCH_PROPERTYPUT or DISPATCH_PROPERTYPUTREF,
Params,
nil,
nil,
nil
)
);
end;
end.
использование:
uses
ActiveX, ComObj, jsobj;
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('about:<HTML><HEAD><SCRIPT>var oArray = new Array(1,2,3,\"Один\",\"Два\",\"Три\");</SCRIPT></HEAD><BODY></BODY></HTML>');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
jsarray: TJSArrayWrapper;
enm: IEnumVARIANT;
v: OleVariant;
Dummy: Cardinal;
begin
jsarray := TJSArrayWrapper.Create(OleVariant(Webbrowser1.Document).parentWindow.oArray);
try
enm := jsarray.GetEnum();
while enm.Next(1, v, Dummy) = S_OK do
Memo1.Lines.Add(v);
jsarray[0] := 'Hello, World!';
enm.Reset;
while enm.Next(1, v, Dummy) = S_OK do
Memo1.Lines.Add(v);
Memo1.Lines.Add(jsarray.AsVariant.join('_-_'));
finally
jsarray.Free;
end;
end;