Greetings! The code just below is in an Excel Module. The intent here is to pass an array of strings from Excel to a PowerBASIC DLL. (I am using Excel 2007 and PowerBASIC 9.01.)
The PowerBasic code which was compiled with a #COMPILE DLL is just below:
When I execute the Excel VBA code, the following messages are displayed by the PowerBASIC DLL:
Current value of Result0 is: xc
Current value of Result1 is
Current value of Result2 is
and then Excel hangs with the message: "Microsoft Office Excel has encountered a problem and needs to close ..." (Excel hangs, and consequently the fourth message is not displayed.) I must be close to the correct code since the first message displayed is correct. The second message should have been "Current value of Result1 is SR", and the third message should have been: "Current value of Result2 is DIR",
I tried the third line of the DLL (xPtr = VARPTR(x$)) both with and without $ after x in the VARPTR call with the same result. I also tried using
STRPTR instead of VARPTR but that causes Excel to bomb as soon as instruction "Result0 = @xPtr[0]" of the DLL is executed. Any help will be greatly appreciated.
For comparison purposes, analogous (and very similar) code succeeded in correctly transferring an array of Double Precision numbers from Excel to a PowerBASIC DLL.
The Excel code for this is:
and the PowerBASIC DLL code is:
Upon execution of the Excel code, all correct results were obtained.
Code:
Declare Function ProcStrArry Lib "C:\PBWin90\MySamples\MySample1.dll" (ByRef x$) As String Sub TestPassStrArrs() Dim MyVarArr(0 To 3) As String MyVarArr(0) = "xc" MyVarArr(1) = "SR" MyVarArr(2) = "DIR" MyVarArr(3) = "COO" Dim AnswStr As String AnswStr = ProcStrArry(MyVarArr(0)) MsgBox "AnswStr = " & AnswStr End Sub
Code:
FUNCTION ProcStrArry ALIAS "ProcStrArry" (BYREF x AS STRING) EXPORT AS STRING DIM xPtr AS STRING POINTER xPtr = VARPTR(x$) DIM Result0 AS STRING DIM Result1 AS STRING DIM Result2 AS STRING DIM Result3 AS STRING Result0 = @xPtr[0] MSGBOX "Current value of Result0 is: " & Result0 Result1 = @xPtr[1] MSGBOX "Current value of Result1 is: " & Result1 Result2 = @xPtr[2] MSGBOX "Current value of Result2 is: " & Result2 Result3 = @xPtr[3] MSGBOX "Current value of Result3 is: " & Result3 FUNCTION = "DONE" END FUNCTION
Current value of Result0 is: xc
Current value of Result1 is
Current value of Result2 is
and then Excel hangs with the message: "Microsoft Office Excel has encountered a problem and needs to close ..." (Excel hangs, and consequently the fourth message is not displayed.) I must be close to the correct code since the first message displayed is correct. The second message should have been "Current value of Result1 is SR", and the third message should have been: "Current value of Result2 is DIR",
I tried the third line of the DLL (xPtr = VARPTR(x$)) both with and without $ after x in the VARPTR call with the same result. I also tried using
STRPTR instead of VARPTR but that causes Excel to bomb as soon as instruction "Result0 = @xPtr[0]" of the DLL is executed. Any help will be greatly appreciated.
For comparison purposes, analogous (and very similar) code succeeded in correctly transferring an array of Double Precision numbers from Excel to a PowerBASIC DLL.
The Excel code for this is:
Code:
Declare Function ProcArry Lib "C:\PBWin90\MySamples\MySample1.dll" (ByRef x#) As Double Sub TestPassArrs() Dim MyVarArr(0 To 3) As Double MyVarArr(0) = 123.456 MyVarArr(1) = 789.111 MyVarArr(2) = 246.864 MyVarArr(3) = 111.222 Dim AnswDbl As Double AnswDbl = ProcArry(MyVarArr(0)) MsgBox "AnswDbl = " & AnswDbl End Sub
Code:
FUNCTION ProcArry ALIAS "ProcArry" (BYREF x AS DOUBLE) EXPORT AS DOUBLE DIM xPtr AS DOUBLE POINTER xPtr = VARPTR(x#) DIM Result0 AS STRING DIM Result1 AS STRING DIM Result2 AS STRING DIM Result3 AS STRING Result0 = STR$(@xPtr[0]) MSGBOX "Current value of Result0 is: " & Result0 Result1 = STR$(@xPtr[1]) MSGBOX "Current value of Result1 is: " & Result1 Result2 = STR$(@xPtr[2]) MSGBOX "Current value of Result2 is: " & Result2 Result3 = STR$(@xPtr[3]) MSGBOX "Current value of Result3 is: " & Result3 FUNCTION = @xPtr[0] + @xPtr[1] + @xPtr[2] + @xPtr[3] END FUNCTION
Comment