5,927
社区成员




以下代码编译时,提示“ inputValue := ArgsArray[0]^;”的编译错误为:“[dcc64 Error] ExcelXLL64.dpr(104): E2016 Array type required”, 请教如何修改代码?
function Excel_WriteCurrentCell(Args: Integer; ResultVal: PXLOPER12;
ArgsArray: PXLOPER12): Integer; cdecl;
var
inputValue: TXLOPER12;
begin
try
// 参数检查(只接受1个参数)
if Args <> 1 then Exit(xlretInvCount);
// 复制输入值
inputValue := ArgsArray[0]^;
inputValue := ArgsArray[0]^;
inputValue := ArgsArray[0];
// 执行赋值操作
if SetCurrentCellValue(@inputValue) then
begin
ResultVal.xbool := 1; // TRUE
ResultVal.w := 2; // Boolean 类型标记
Result := xlretSuccess;
end
else
begin
ResultVal.err := xlretFailed;
Result := xlretFailed;
end;
except
ResultVal.err := xlretFailed;
Result := xlretFailed;
end;
end;
你需要确保在代码的某个地方定义了 PXLOPER12Array 类型,通常是这样的:
delphi
type
PXLOPER12Array = ^TXLOPER12Array;
TXLOPER12Array = array[0..0] of TXLOPER12;
或者更灵活的定义方式:
delphi
type
PXLOPER12Array = array[0..MaxInt div SizeOf(TXLOPER12) - 1] of TXLOPER12;
如果你不想定义新的类型,也可以直接使用类型转换:
delphi
inputValue := PXLOPER12Array(ArgsArray)^[0]^;
或者更简洁的方式:
delphi
inputValue := ArgsArray[0];
因为 ArgsArray 实际上是一个指向数组的指针,而 Delphi 允许你直接使用数组语法访问指针指向的数组元素。
一般这样的问题我都是直接DS,这是它的回复:
这个编译错误是因为 ArgsArray 被声明为 PXLOPER12(指向 TXLOPER12 的指针),而不是一个数组指针。在 Delphi 中,要访问指针指向的数组元素,你需要进行适当的类型转换。
以下是修改后的代码:
function Excel_WriteCurrentCell(Args: Integer; ResultVal: PXLOPER12; ArgsArray: PXLOPER12): Integer; cdecl;
var
inputValue: TXLOPER12;
ArgsArrayAsArray: array of PXLOPER12;
begin
try
// 参数检查(只接受1个参数)
if Args <> 1 then Exit(xlretInvCount);
// 将指针转换为数组指针
ArgsArrayAsArray := PXLOPER12Array(ArgsArray);
// 复制输入值
inputValue := ArgsArrayAsArray[0]^;
// 执行赋值操作
if SetCurrentCellValue(@inputValue) then
begin
ResultVal.xbool := 1; // TRUE
ResultVal.w := 2; // Boolean 类型标记
Result := xlretSuccess;
end
else
begin
ResultVal.err := xlretFailed;
Result := xlretFailed;
end;
except
ResultVal.err := xlretFailed;
Result := xlretFailed;
end;
end;