将SafeArray从Delphi传递到ms uiautomation库

将SafeArray从Delphi传递到ms uiautomation库,delphi,microsoft-ui-automation,Delphi,Microsoft Ui Automation,关于a,我现在有一个部分工作的实现,它封装了TStringGrid,并允许自动化访问它 我需要实现ISelectionProvider的GetSelection方法,但尽管我认为我已经创建了一个数组,但当我使用ms uiautomation获取结果数组时,它有0个条目。下面的代码肯定是被调用的,因为我可以在方法中放置一个断点并停止它 我尝试了几种创建和填充数组的方法,这是我最新的(基于 有没有想过我做错了什么 更新: 只是澄清一下,调用的自动化代码如下 var collection

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

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

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

有没有想过我做错了什么

更新:

只是澄清一下,调用的自动化代码如下

  var
    collection : IUIAutomationElementArray;
  ...
  // Assume that we have a valid pattern
  FSelectionPattern.GetCurrentSelection(collection);
  collection.Get_Length(length);
从Get_Length返回的值为0。

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

请尝试类似以下内容:

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;
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;

我已经解决了访问违规问题,由于我无法在评论中发布代码,我将发布一个答案。唯一真正的区别是我将IUnknown转换为指向IUnknown的指针,因为这解决了我看到的访问违规问题

function TAutomationStringGrid.GetSelection(out pRetVal: PSafeArray): HResult;
var
  intf : TAutomationStringGridItem;
  outBuffer : PSafeArray;
  offset : integer;
  unk : IUnknown;
  iRow, iCol : integer;
  Bounds : array [0..0] of TSafeArrayBound;

begin
  pRetVal := nil;
  result := S_FALSE;

  iRow := Self.Row;
  iCol := Self.Col;

  // is a cell selected?
  if (iRow > -1) and (iCol > -1) then
  begin
    intf := TAutomationStringGridItem.create(self, iCol, iRow,  self.Cells[self.Col, self.Row]);

    bounds[0].lLbound := 0;
    bounds[0].cElements := 1;
    outBuffer := SafeArrayCreate(VT_UNKNOWN, 1, @Bounds);

    if intf <> nil then
    begin
      offset := 0;
      unk := intf as IUnknown;
      Result := SafeArrayPutElement(&outBuffer, offset, Pointer(unk)^);
      if Result <> S_OK then
      begin
        SafeArrayDestroy(outBuffer);
        pRetVal := nil;
        result := E_OUTOFMEMORY;
      end
      else
      begin
        pRetVal := outBuffer;
        result := S_OK;
      end;
    end;
  end
  else
  begin
    pRetVal := nil;
    result := S_FALSE;
  end;
end;
函数TAutomationStringGrid.GetSelection(out pRetVal:PSafeArray):HResult; 变量 intf:TAutomationStringGridItem; 爆发者:沙雷; 偏移量:整数; 未知:未知; iRow,iCol:整数; 边界:数组[0..0]的边界; 开始 pRetVal:=零; 结果:=S_假; iRow:=Self.Row; iCol:=Self.Col; //是否选择了单元格? 如果(iRow>-1)和(iCol>-1),则 开始 intf:=TAutomationStringGridItem.create(self、iCol、iRow、self.Cells[self.Col、self.Row]); 边界[0]。lLbound:=0; 界限[0]。芹菜:=1; exputffer:=SafeArrayCreate(VT_未知,1,@Bounds); 如果intf为零,则 开始 偏移量:=0; 未知:=作为IUnknown的intf; 结果:=SafeArrayPutElement(&exputfer,offset,Pointer(unk)^); 如果结果正常,那么 开始 安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安全安; pRetVal:=零; 结果:=E_OUTOFMEMORY; 结束 其他的 开始 pRetVal:=突发事件; 结果:=S_正常; 结束; 结束; 结束 其他的 开始 pRetVal:=零; 结果:=S_假; 结束; 结束;

更新:我已经编辑了代码片段,以与下面Remy的注释一致。

你真的是说数组中没有元素,还是仅仅是你最初分配的一个元素最终没有包含你认为你存储的值?当我在自动化的“另一端”使用客户端代码时,集合的计数在上是0。我将把它作为上面的更新发布。哇,详细的答案,看起来它更接近我需要的,但是我已经很快地更改了我的代码以使用您的第一个示例,当我从客户端代码调用Get_选择时,第一个应用程序现在崩溃。这可能是我已经做的事情,所以我将继续挖掘,看看崩溃在哪里。在将未知对象添加到SafeArray时,无论是使用SafeArrayCreateVector创建的还是使用SafeArrayPutElement创建的,我都会遇到访问冲突。好的,我已经解决了访问冲突,我将发布更改作为答案,但接受Remy的更改,因为这几乎是我需要做的100%。向
PUnknown
然后取消对它的引用是完全错误的。此外,无论如何也没有必要这样做。请阅读:“变量类型VT_DISPATCH、VT_UNKNOWN和VT_BSTR都是指针,不需要其他级别的间接寻址。”您的AV是由其他原因引起的。Delphi声明
SafeArrayPutElement()
采用非类型化的
常量
参数,而不是像真实API中那样的
指针
。这是Delphi强制的额外间接寻址。不要使用
PUnknown(unk)^
,尝试只使用
unk^
,或者至少使用
指针(unk)^
。unk^代码不编译,但指针(unk)^是的,而且仍然有效,所以我修改了上面的代码片段。
function TAutomationStringGrid.GetSelection(out pRetVal: PSafeArray): HResult;
var
  intf : TAutomationStringGridItem;
  outBuffer : PSafeArray;
  offset : integer;
  unk : IUnknown;
  iRow, iCol : integer;
  Bounds : array [0..0] of TSafeArrayBound;

begin
  pRetVal := nil;
  result := S_FALSE;

  iRow := Self.Row;
  iCol := Self.Col;

  // is a cell selected?
  if (iRow > -1) and (iCol > -1) then
  begin
    intf := TAutomationStringGridItem.create(self, iCol, iRow,  self.Cells[self.Col, self.Row]);

    bounds[0].lLbound := 0;
    bounds[0].cElements := 1;
    outBuffer := SafeArrayCreate(VT_UNKNOWN, 1, @Bounds);

    if intf <> nil then
    begin
      offset := 0;
      unk := intf as IUnknown;
      Result := SafeArrayPutElement(&outBuffer, offset, Pointer(unk)^);
      if Result <> S_OK then
      begin
        SafeArrayDestroy(outBuffer);
        pRetVal := nil;
        result := E_OUTOFMEMORY;
      end
      else
      begin
        pRetVal := outBuffer;
        result := S_OK;
      end;
    end;
  end
  else
  begin
    pRetVal := nil;
    result := S_FALSE;
  end;
end;