delphi调用c#类库如何传递数组参数 20
非数组参数的函数可以调用成功,但是如果参数是数组的话,调用报“参数错误”,有哪位大神帮忙解决了,不胜感激。c#端函数:publicintSumAll(int[]arg);...
非数组参数的函数可以调用成功,但是如果参数是数组的话,调用报“参数错误”,有哪位大神帮忙解决了,不胜感激。c#端函数: public int SumAll(int[] arg);delphi端:procedure TForm1.Button13Click(Sender: TObject);
var
OBJ: Variant;
VarBound: array [0..0] of TVarArrayBound;
psa: PSafeArray;
i : Integer;
OleValue: OleVariant;
begin
try
try
OBJ := CreateOleObject('ClassLib.Tester');
VariantInit(OleValue);//初始化OleValue
VarBound[0].LowBound := 0;
VarBound[0].ElementCount := 3;
psa := SafeArrayCreate(VT_I4, 1, VarBound);//创建SafeArray对象
for i := 0 to 2 do
begin
SafeArrayPutElement(psa, i, i + 10);
end;
TVarData(OleValue).VType := VT_ARRAY or VT_I4;
TVarData(OleValue).Varray := pVarArray(psa);
h := OBJ.SumAll(OleValue);//运行时报错:“参数错误”
ShowMessage('返回结果: ' + IntToStr(h));
except
on e: Exception do
begin
ShowMessage(e.Message);
end;
end;
finally
OBJ := Unassigned;
end;
end; 展开
var
OBJ: Variant;
VarBound: array [0..0] of TVarArrayBound;
psa: PSafeArray;
i : Integer;
OleValue: OleVariant;
begin
try
try
OBJ := CreateOleObject('ClassLib.Tester');
VariantInit(OleValue);//初始化OleValue
VarBound[0].LowBound := 0;
VarBound[0].ElementCount := 3;
psa := SafeArrayCreate(VT_I4, 1, VarBound);//创建SafeArray对象
for i := 0 to 2 do
begin
SafeArrayPutElement(psa, i, i + 10);
end;
TVarData(OleValue).VType := VT_ARRAY or VT_I4;
TVarData(OleValue).Varray := pVarArray(psa);
h := OBJ.SumAll(OleValue);//运行时报错:“参数错误”
ShowMessage('返回结果: ' + IntToStr(h));
except
on e: Exception do
begin
ShowMessage(e.Message);
end;
end;
finally
OBJ := Unassigned;
end;
end; 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询