Hi All,
I am trying to write a series of DLLs to be called by VB 2008 apps as well as VBA code in Excel and Access. I have had some success but I have been unable to pass any more than one variable to a function in a DLL. If I try to pass more than one then VB6/VBA just crashes and VB2008 gives the error message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I have tried every possible combination of passing variables ByVal and ByRef. The DLLs work fine when called from a PB application.
I have got to a stage where I need to emulate some legacy functions that pass variables as ByRef arrays. Here is an example of the DLL code:
----------------------
#COMPILE DLL
#DIM ALL
%USEMACROS = 1
#INCLUDE "Win32API.inc"
FUNCTION BRfunc ALIAS "BRfunc"(BYREF Int0 AS INTEGER, BYREF Arr1() AS BYTE, BYREF Arr2() AS STRING, BYREF Int1 AS INTEGER) EXPORT AS LONG
DIM i AS INTEGER
DIM tStr AS STRING
FOR i = 0 TO Int0
tStr = Arr2(i)
Arr2(i) = STR$(arr1(i)) & " " & tStr
NEXT
Int1 = i-1
FUNCTION = i
END FUNCTION
------------------------------------
And here is the Powerbasic code that calls it:
------------------------------------
#COMPILE EXE
#DIM ALL
DECLARE FUNCTION BRfunc LIB "ByRefDll.dll" ALIAS "BRfunc" (BYREF Int0 AS INTEGER, BYREF Arr1() AS BYTE, BYREF Arr2() AS STRING, BYREF Int1 AS INTEGER) AS LONG
FUNCTION PBMAIN () AS LONG
DIM Rint AS INTEGER
DIM Rlng AS LONG
DIM Rstr AS STRING
DIM i AS INTEGER
DIM AnimalQty(5) AS BYTE
DIM AnimalName(5) AS STRING
AnimalQty(0) = 5
AnimalName(0) = "snakes"
AnimalQty(1) = 3
AnimalName(1) = "emus"
AnimalQty(2) = 23
AnimalName(2) = "sheep"
AnimalQty(3) = 202
AnimalName(3) = "caterpillars"
AnimalQty(4) = 7
AnimalName(4) = "elephants"
AnimalQty(5) = 2
AnimalName(5) = "horses"
Rlng = BRfunc(5, AnimalQty(), AnimalName(), Rint)
Rstr = STR$(Rlng) & " types of animals:"
FOR i = 0 TO Rint
Rstr = Rstr & CHR$(13) & AnimalName(i)
NEXT
MSGBOX Rstr
END FUNCTION
----------------------------------
This works fine in PowerBasic. The same code in any VB does not work. Attached are some code examples in VB6 and VB 2008 that demonstrate the problem.
Can anyone please let me know where I am going wrong.
Thanks in advance,
Rob
I am trying to write a series of DLLs to be called by VB 2008 apps as well as VBA code in Excel and Access. I have had some success but I have been unable to pass any more than one variable to a function in a DLL. If I try to pass more than one then VB6/VBA just crashes and VB2008 gives the error message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I have tried every possible combination of passing variables ByVal and ByRef. The DLLs work fine when called from a PB application.
I have got to a stage where I need to emulate some legacy functions that pass variables as ByRef arrays. Here is an example of the DLL code:
----------------------
#COMPILE DLL
#DIM ALL
%USEMACROS = 1
#INCLUDE "Win32API.inc"
FUNCTION BRfunc ALIAS "BRfunc"(BYREF Int0 AS INTEGER, BYREF Arr1() AS BYTE, BYREF Arr2() AS STRING, BYREF Int1 AS INTEGER) EXPORT AS LONG
DIM i AS INTEGER
DIM tStr AS STRING
FOR i = 0 TO Int0
tStr = Arr2(i)
Arr2(i) = STR$(arr1(i)) & " " & tStr
NEXT
Int1 = i-1
FUNCTION = i
END FUNCTION
------------------------------------
And here is the Powerbasic code that calls it:
------------------------------------
#COMPILE EXE
#DIM ALL
DECLARE FUNCTION BRfunc LIB "ByRefDll.dll" ALIAS "BRfunc" (BYREF Int0 AS INTEGER, BYREF Arr1() AS BYTE, BYREF Arr2() AS STRING, BYREF Int1 AS INTEGER) AS LONG
FUNCTION PBMAIN () AS LONG
DIM Rint AS INTEGER
DIM Rlng AS LONG
DIM Rstr AS STRING
DIM i AS INTEGER
DIM AnimalQty(5) AS BYTE
DIM AnimalName(5) AS STRING
AnimalQty(0) = 5
AnimalName(0) = "snakes"
AnimalQty(1) = 3
AnimalName(1) = "emus"
AnimalQty(2) = 23
AnimalName(2) = "sheep"
AnimalQty(3) = 202
AnimalName(3) = "caterpillars"
AnimalQty(4) = 7
AnimalName(4) = "elephants"
AnimalQty(5) = 2
AnimalName(5) = "horses"
Rlng = BRfunc(5, AnimalQty(), AnimalName(), Rint)
Rstr = STR$(Rlng) & " types of animals:"
FOR i = 0 TO Rint
Rstr = Rstr & CHR$(13) & AnimalName(i)
NEXT
MSGBOX Rstr
END FUNCTION
----------------------------------
This works fine in PowerBasic. The same code in any VB does not work. Attached are some code examples in VB6 and VB 2008 that demonstrate the problem.
Can anyone please let me know where I am going wrong.
Thanks in advance,
Rob
Comment