I have a host application written in Delphi (C++ Builder) XE2 and a plugin written in VS2012, C#, I want to pass an integer array by an interface, but I have a problem in creating the array. Here’s my Delphi code:
procedure TestProcedure();
var
Bounds: TVarArrayBoundArray;
ind: array [0..0] of Integer;
TestArray: PVarArray;
TestValue: Integer;
begin
TestArray := nil;
Bounds[0].LowBound := 0;
Bounds[0].ElementCount := 3;
TestArray := SafeArrayCreate(varInteger, 1, Bounds);
ind[0] := 0;
TestValue := 5;
SafeArrayPutElement(TestArray, @ind, PInteger(TestValue));
ind[0] := 1; // HERE I GET A RUNTIME ERROR
TestValue := 6;
SafeArrayPutElement(TestArray, @ind, PInteger(TestValue));
ind[0] := 2;
TestValue := 7;
SafeArrayPutElement(TestArray, @ind, PInteger(TestValue));
TestProcedureFromInterface(TestArray);
end;
I can compile this code, but then I get a runtime error in line “ind[0] := 1;”. What am I doing wrong?
Thank you for answers, with “GetMem” and “FreeMem” it works. With my code I refered to the article http://wiki.remobjects.com/wiki/How_to_pass_arrays , but there “GetMem” and “FreeMem” is missing, perhaps you should add it.
I’m sorry, but I still have a problem with my integer array. The test array has two elements and the C# plugin receives two elements, no error and no exception, but both values are 0, so I think “SafeArrayPutElement” ist the problem. This is my current code:
Hello, I have the same problem. I tried all the solutions in this topic, but nothing works.
Is it possible to pass integer array to an hydra interface ?
Marie.
hello, i want to pass integer array delphi to .net hydra plugin.
I use this method :
function IntArrayAsSafeArray(Arr: ArrayOfInt): PVarArray;
var
pvData: PIntegerArray;
Bounds: TVarArrayBoundArray;
begin
Bounds[0].LowBound:=0;
Bounds[0].ElementCount:=Length(Arr);
I also tried : to transform int to an object :
ArrayOfIntObject = array of IIntObjectDto;
IIntObjectDto = interface(IHYCrossPlatformInterface)
[‘{0A2B8CD8-E929-4B66-A455-E6FB11A8C9B7}’]
function get_Value: LongInt; safecall;
procedure set_Value(const value: LongInt); safecall;
property Value: LongInt read get_Value write set_Value;
end;
and i use the function:
function IntArrayAsSafeArray(Arr: ArrayOfInt): PVarArray;
var
Bounds: TVarArrayBoundArray;
i: Integer;
ind: array[0…0] of Integer;
//intObj: IIntObjectDto;
intObjsArray: ArrayOfIntObject;
begin
Result := nil;
if (Arr = nil) or (Length(Arr) = 0) then
Exit;
SetLength(intObjsArray, Length(Arr));
for i := 0 to Length(Arr) - 1 do
begin
intObjsArray[i] := IntObjectDto.Create(Arr[i]);
end;
Result := SafeArrayCreate(varDispatch, 1, Bounds);
for i := 0 to Length(Arr) - 1 do
begin
ind[0] := i;
SafeArrayPutElement(Result, @ind, PDispatch(intObjsArray[i]));
end;
end;
But with this code i have a exception :SafeArrayRankMismatchException in .net plugin hydra.