gpt4 book ai didi

delphi - 将 SafeArray 从 Delphi 传递到 ms-uiautomation 库

转载 作者:行者123 更新时间:2023-12-02 03:10:01 35 4
gpt4 key购买 nike

关于previous question ,我现在有一个部分工作的实现,它包装了 TStringGrid,并允许自动化访问它。

Sort of anyway .

我需要实现 ISelectionProvider 的 GetSelection 方法,但即使我认为我已经创建了一个 pSafeArray,当我使用 ms-uiautomation 获取结果数组时,它有 0 个条目。下面的代码肯定会被调用,因为我可以在方法中放置一个断点并停止它。

我尝试了几种创建和填充数组的方法,这是我最新的方法(基于 different question on StackOverflow ..

function TAutomationStringGrid.GetSelection(out pRetVal: PSafeArray): HResult;
var
obj : TAutomationStringGridItem;
outBuffer : PSafeArray;
offset : integer;
begin
obj := TAutomationStringGridItem.create(self);
obj.Row := self.row;
obj.Column := self.Col;
obj.Value := self.Cells[self.Col, self.Row];

offset := 0;
outBuffer := SafeArrayCreateVector(VT_VARIANT, 0, 1);
SafeArrayPutElement(outBuffer, offset, obj);
pRetVal := outBuffer;
result := S_OK;
end;

对我做错了什么有什么想法吗?

更新:

为了澄清,被调用的自动化代码如下..

  var
collection : IUIAutomationElementArray;
...
// Assume that we have a valid pattern
FSelectionPattern.GetCurrentSelection(collection);
collection.Get_Length(length);

Get_Length 返回的值为 0。

最佳答案

您的 GetSelection() 实现预计会返回 IRawElementProviderSimpleSAFEARRAY接口(interface)指针。但是,您改为创建 VARIANT 元素的 SAFEARRAY,然后使用 TAutomationStringGridItem 对象指针填充这些元素。 SafeArrayPutElement() 要求您向其传递一个与数组类型匹配的值(在您的代码中,该值将是指向 VARIANT 的指针,然后其值将被复制) 。因此,在为客户端应用初始化 IUIAutomationElementArray 时,UIAutomation 将无法使用格式错误的数组,这是有道理的。

尝试更多类似这样的事情:

type
TAutomationStringGridItem = class(TInterfacedObject, IRawElementProviderSimple, IValueProvider, ...)
...
public
constructor Create(AGrid: TAutomationStringGrid; ARow, ACol: Integer; const AValue: string);
...
end;

constructor TAutomationStringGridItem.Create(AGrid: TAutomationStringGrid; ARow, ACol: Integer; const AValue: string);
begin
...
Self.Row := ARow;
Self.Column := ACol;
Self.Value := AValue;
...
end;

function TAutomationStringGrid.get_CanSelectMultiple(out pRetVal: BOOL): HResult;
begin
pRetVal := False;
Result := S_OK;
end;

function TAutomationStringGrid.get_IsSelectionRequired(out pRetVal: BOOL): HResult;
begin
pRetVal := False;
Result := S_OK;
end;

function TAutomationStringGrid.GetSelection(out pRetVal: PSafeArray): HResult;
var
intf: IRawElementProviderSimple;
unk: IUnknown;
outBuffer : PSafeArray;
offset, iRow, iCol : integer;
begin
// get the current selected cell, if any...
iRow := Self.Row;
iCol := Self.Col;

// is a cell selected?
if (iRow > -1) and (iCol > -1) then
begin
// yes...
intf := TAutomationStringGridItem.Create(Self, iRow, iCol, Self.Cells[iCol, iRow]);
outBuffer := SafeArrayCreateVector(VT_UNKNOWN, 0, 1);
end else
begin
// no ...

// you would have to check if UIA allows you to return a nil
// array, possibly with S_FALSE instead of S_OK, so as to
// avoid having to allocate memory for an empty array...
{
// pRetVal is already nil because of 'out'...
Result := S_FALSE; // or S_OK if S_FALSE is not allowed...
Exit;
}

outBuffer := SafeArrayCreateVector(VT_UNKNOWN, 0, 0);
end;

if outBuffer = nil then
begin
Result := E_OUTOFMEMORY;
Exit;
end;

if intf <> nil then
begin
offset := 0;
unk := intf as IUnknown;
Result := SafeArrayPutElement(outBuffer, offset, unk);
if Result <> S_OK then
begin
SafeArrayDestroy(outBuffer);
Exit;
end;
end;

pRetVal := outBuffer;
end;

话虽如此,TStringGrid 支持多选,并且 GetSelection() 的输出预计会返回所有选定项的数组。因此,更准确的实现看起来更像是这样的:

function TAutomationStringGrid.get_CanSelectMultiple(out pRetVal: BOOL): HResult;
begin
pRetVal := goRangeSelect in Self.Options;
Result := S_OK;
end;

function TAutomationStringGrid.get_IsSelectionRequired(out pRetVal: BOOL): HResult;
begin
pRetVal := False;
Result := S_OK;
end;

function TAutomationStringGrid.GetSelection(out pRetVal: PSafeArray): HResult;
var
intfs: array of IRawElementProviderSimple;
unk: IUnknown;
outBuffer : PSafeArray;
offset, iRow, iCol: Integer;
R: TGridRect;
begin
// get the current range of selected cells, if any...
R := Self.Selection;

// are any cells selected?
if (R.Left > -1) and (R.Right > -1) and (R.Top > -1) and (R.Bottom > -1) then
begin
// yes...
SetLength(intfs, ((R.Right-R.Left)+1)*((R.Bottom-R.Top)+1));
offset := Low(intfs);
for iRow := R.Top to R.Bottom do
begin
for iCol := R.Left to R.Right do
begin
intfs[offset] := TAutomationStringGridItem.Create(Self, iRow, iCol, Self.Cells[iCol, iRow]);
Inc(offset);
end;
end;
end;

// you would have to check if UIA allows you to return a nil
// array, possibly with S_FALSE instead of S_OK, so as to
// avoid having to allocate memory for an empty array...
{
if Length(intfs) = 0 then
begin
// pRetVal is already nil because of 'out'...
Result := S_FALSE; // or S_OK if S_FALSE is not allowed...
Exit;
end;
}

outBuffer := SafeArrayCreateVector(VT_UNKNOWN, Low(intfs), Length(intfs));
if outBuffer = nil then
begin
Result := E_OUTOFMEMORY;
Exit;
end;

for offset := Low(intfs) to High(intfs) do
begin
unk := intfs[offset] as IUnknown;
Result := SafeArrayPutElement(outBuffer, offset, unk);
if Result <> S_OK then
begin
SafeArrayDestroy(outBuffer);
Exit;
end;
end;

pRetVal := outBuffer;
Result := S_OK;
end;

关于delphi - 将 SafeArray 从 Delphi 传递到 ms-uiautomation 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30665930/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com