A: You declare the parameter on the Delphi side as an open array, but it isn't one! An open array parameter in Delphi consists of a pointer to the array data and an additional word that gives the actual size of the passed array, 6 bytes in total. What VB provides is only the pointer to the data, so you have a mismatch in parameter list sizes, which is a sure recipe for GPF.
Try this one, modified from your code:
' Start of VB Code ****************************************
' Form level declarations
Declare Function ReadArray Lib "TestDLL.dll" (TestArr As Any, ByVal
Size as
Integer) As Integer
Dim TestArray(3) As Integer ' Declares an array[0..3] of integer in VB
Sub Form_Load
TestArray(0) = 2
TestArray(1) = 4
TestArray(2) = 6
TestArray(3) = 8
End Sub
Sub cmdDelphi_Click ()
Dim iResult As Integer
iResult = ReadArray(TestArray(0), 4) ' Documentation says
this is the way
to get VB to
' pass a pointer to an array
MsgBox "Back once again with result = " & iResult
End Sub
' End of VB Code ********************************************
Code in Delphi is:
{Start of Delphi Code ****************************************}
interface
.
.
Type
TIntArray = Array [1..High(Word) div Sizeof(Integer)] of Integer;
function ReadArray(var TestArray: TIntArray; size: Integer): integer;
export;
.
.
implementation
function ReadArray;
var
iCount : integer;
begin
for iCount := 1 to size do
begin
ShowMessage( 'Element ' + InttoStr(iCount) + ' =
' +
IntToStr(TestArray[iCount]));
end;
Result := 4;
end;
{End of Delphi Code ****************************************}