how to pass Safearrays really byref (avoid copy) 
Author Message
 how to pass Safearrays really byref (avoid copy)

hi, i've noticed that there is no way to pass arrays in Vb "really" byref
i mean this:
suppose class A has the method
public sub getarray(byref p_Array() as double)
i_array = p_array 'i_array is an instance variable
exit sub

if i call this method in this way:

dim a1 as new A
dim l_array() as double
redim l_Array(1 to 1000000) as double
for x = 1 to 100000
    l_Array(x) = rnd
next
a1.getarray(l_array)

i can see that the memory grows once when i redim in the caller code (as i
expected)
but i can see that the memory grows again in the line i_array = p_array in
the getarray method and this is not what i want

i would like to avoid the copy of data , in C++ you get this passing a
pointer .. how to do it in VB ?
i remember there are some undocumented functions in Vb about pointer could
they help me ?
TIA,sabba
--
Enrico Sabbadin

http://www.*-*-*.com/
MTS FAQ: http://www.*-*-*.com/



Mon, 01 Jul 2002 03:00:00 GMT  
 how to pass Safearrays really byref (avoid copy)
sabba,

I'm far from the most expert here, but from what I have come to understand
about arrays and array assignment statements in VB - the behavior you are
experiencing is exactly what VB specifies should occur. The i_array =
p_array statement is an assignment statement where you are assigning
(copying/duplicating) the p_array array to a new (variant) array variable
(variants are the only variable types allowed to be on the left of the "="
sign in an array assignment statement).

If i read your intentions correctly, why do you even need the i_array
variable? Why not just keep referencing the p_array variable throught the
remainder of your function (& sub-function calls), which BTW does indeed
seem to be being passed "byref" BTW (else how could it take part in the
assignment statement and not eat RAM until the i_array=p_array line - a
byval p_array call would have eaten memory at (just after it was executed)
the a1.getarray(l-array) line, right?"


Quote:
> hi, i've noticed that there is no way to pass arrays in Vb "really" byref
> i mean this:
> suppose class A has the method
> public sub getarray(byref p_Array() as double)
> i_array = p_array 'i_array is an instance variable
> exit sub

> if i call this method in this way:

> dim a1 as new A
> dim l_array() as double
> redim l_Array(1 to 1000000) as double
> for x = 1 to 100000
>     l_Array(x) = rnd
> next
> a1.getarray(l_array)

> i can see that the memory grows once when i redim in the caller code (as i
> expected)
> but i can see that the memory grows again in the line i_array = p_array in
> the getarray method and this is not what i want

> i would like to avoid the copy of data , in C++ you get this passing a
> pointer .. how to do it in VB ?
> i remember there are some undocumented functions in Vb about pointer could
> they help me ?
> TIA,sabba
> --
> Enrico Sabbadin

> http://sabbadin.tripod.com
> MTS FAQ: http://sabbadin.tripod.com/mts_faq.htm



Mon, 01 Jul 2002 03:00:00 GMT  
 how to pass Safearrays really byref (avoid copy)
There are ObjPtr, StrPtr and VarPtr functions within VBA.HiddenModule.
I haven't used them extensively but you could try to play with them and see
if it helps.


Quote:
> hi, i've noticed that there is no way to pass arrays in Vb "really" byref
> i mean this:
> suppose class A has the method
> public sub getarray(byref p_Array() as double)
> i_array = p_array 'i_array is an instance variable
> exit sub

> if i call this method in this way:

> dim a1 as new A
> dim l_array() as double
> redim l_Array(1 to 1000000) as double
> for x = 1 to 100000
>     l_Array(x) = rnd
> next
> a1.getarray(l_array)

> i can see that the memory grows once when i redim in the caller code (as i
> expected)
> but i can see that the memory grows again in the line i_array = p_array in
> the getarray method and this is not what i want

> i would like to avoid the copy of data , in C++ you get this passing a
> pointer .. how to do it in VB ?
> i remember there are some undocumented functions in Vb about pointer could
> they help me ?
> TIA,sabba
> --
> Enrico Sabbadin

> http://sabbadin.tripod.com
> MTS FAQ: http://sabbadin.tripod.com/mts_faq.htm



Mon, 01 Jul 2002 03:00:00 GMT  
 how to pass Safearrays really byref (avoid copy)
Try this code, it may work. Did not test it, but the way should be right.

cu
Thomas

Public Declare Sub RtlMoveMemory Lib "kernel32" (lpDest As Any, lpSource As Any, ByVal lCount As Long)

public sub getarray(byval lpArray as long)  'receive a pointer here
    dim i_array As Variant
    const SIZE_OF_STRUCT_VARIANT = 8 ' or is ist 16?
    ' copy the bytes lpArray points to, to the address where i_array resides in memory
    RTLMoveMemory i_array, ByVal lpArray, SIZE_OF_STRUCT_VARIANT

    i_array(32) = 3.1415 ' or whatever you wanna do :-)

    ' you must clear i_array manual, or vb will destroy your array on sub-exit, I think.
    RTLMoveMemory i_array, ByVal 0, SIZE_OF_STRUCT_VARIANT
    exit sub
end sub
....
....
a1.getarray(VarPtr(l_array)) 'pass a pointer here
Msgbox I_array(32)


Quote:
> There are ObjPtr, StrPtr and VarPtr functions within VBA.HiddenModule.
> I haven't used them extensively but you could try to play with them and see
> if it helps.



> > hi, i've noticed that there is no way to pass arrays in Vb "really" byref
> > i mean this:
> > suppose class A has the method
> > public sub getarray(byref p_Array() as double)
> > i_array = p_array 'i_array is an instance variable
> > exit sub

> > if i call this method in this way:

> > dim a1 as new A
> > dim l_array() as double
> > redim l_Array(1 to 1000000) as double
> > for x = 1 to 100000
> >     l_Array(x) = rnd
> > next
> > a1.getarray(l_array)

> > i can see that the memory grows once when i redim in the caller code (as i
> > expected)
> > but i can see that the memory grows again in the line i_array = p_array in
> > the getarray method and this is not what i want

> > i would like to avoid the copy of data , in C++ you get this passing a
> > pointer .. how to do it in VB ?
> > i remember there are some undocumented functions in Vb about pointer could
> > they help me ?
> > TIA,sabba
> > --
> > Enrico Sabbadin

> > http://sabbadin.tripod.com
> > MTS FAQ: http://sabbadin.tripod.com/mts_faq.htm



Mon, 01 Jul 2002 03:00:00 GMT  
 how to pass Safearrays really byref (avoid copy)
thank thomas,
i had come to the conclusion i had to use varptr, still i had no idea how to
re-attach the pointer to an array in my method call
i'll try the code you posted and let you know
thank again
--

Home Page: http://sabbadin.tripod.com
MTS FAQ: http://sabbadin.tripod.com/mts_faq.htm

Try this code, it may work. Did not test it, but the way should be right.

cu
Thomas

Public Declare Sub RtlMoveMemory Lib "kernel32" (lpDest As Any, lpSource As
Any, ByVal lCount As Long)

public sub getarray(byval lpArray as long)  'receive a pointer here
    dim i_array As Variant
    const SIZE_OF_STRUCT_VARIANT = 8 ' or is ist 16?
    ' copy the bytes lpArray points to, to the address where i_array resides
in memory
    RTLMoveMemory i_array, ByVal lpArray, SIZE_OF_STRUCT_VARIANT

    i_array(32) = 3.1415 ' or whatever you wanna do :-)

    ' you must clear i_array manual, or vb will destroy your array on
sub-exit, I think.
    RTLMoveMemory i_array, ByVal 0, SIZE_OF_STRUCT_VARIANT
    exit sub
end sub
....
....
a1.getarray(VarPtr(l_array)) 'pass a pointer here
Msgbox I_array(32)



Quote:
> There are ObjPtr, StrPtr and VarPtr functions within VBA.HiddenModule.
> I haven't used them extensively but you could try to play with them and
see
> if it helps.



> > hi, i've noticed that there is no way to pass arrays in Vb "really"
byref
> > i mean this:
> > suppose class A has the method
> > public sub getarray(byref p_Array() as double)
> > i_array = p_array 'i_array is an instance variable
> > exit sub

> > if i call this method in this way:

> > dim a1 as new A
> > dim l_array() as double
> > redim l_Array(1 to 1000000) as double
> > for x = 1 to 100000
> >     l_Array(x) = rnd
> > next
> > a1.getarray(l_array)

> > i can see that the memory grows once when i redim in the caller code (as
i
> > expected)
> > but i can see that the memory grows again in the line i_array = p_array
in
> > the getarray method and this is not what i want

> > i would like to avoid the copy of data , in C++ you get this passing a
> > pointer .. how to do it in VB ?
> > i remember there are some undocumented functions in Vb about pointer
could
> > they help me ?
> > TIA,sabba
> > --
> > Enrico Sabbadin

> > http://sabbadin.tripod.com
> > MTS FAQ: http://sabbadin.tripod.com/mts_faq.htm



Fri, 05 Jul 2002 03:00:00 GMT  
 how to pass Safearrays really byref (avoid copy)
Hi Sabba,
I thought that in VB6 all arrays were passed ByRef.
I'm sure I read this on the MSDN CD

as in:
Public Sub getarray(p_Array() as Double)

passes a reference to p_Array

however,

 i_Array = p_Array

would that not make a copy of p_Array in i_Array?

the same as :

dim i_Variant as Variant

i_Variant = p_Array

No?


Quote:
> hi, i've noticed that there is no way to pass arrays in Vb "really" byref
> i mean this:
> suppose class A has the method
> public sub getarray(byref p_Array() as double)
> i_array = p_array 'i_array is an instance variable
> exit sub

> if i call this method in this way:

> dim a1 as new A
> dim l_array() as double
> redim l_Array(1 to 1000000) as double
> for x = 1 to 100000
>     l_Array(x) = rnd
> next
> a1.getarray(l_array)

> i can see that the memory grows once when i redim in the caller code (as i
> expected)
> but i can see that the memory grows again in the line i_array = p_array in
> the getarray method and this is not what i want

> i would like to avoid the copy of data , in C++ you get this passing a
> pointer .. how to do it in VB ?
> i remember there are some undocumented functions in Vb about pointer could
> they help me ?
> TIA,sabba
> --
> Enrico Sabbadin

> http://sabbadin.tripod.com
> MTS FAQ: http://sabbadin.tripod.com/mts_faq.htm



Mon, 08 Jul 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. how to pass Safearrays really byref (avoid copy)

2. how to pass Safearrays really byref (avoid copy)

3. How to pass a SafeArray to a dll

4. Passing Multi-dimensional SAFEARRAY from VB6 to VC++6 DLL

5. Passing Empty SafeArrays from VB

6. Passing SAFEARRAYs to VB

7. Passing SAFEARRAYS of BSTR and int to a VBScript

8. VB.NET: Bug in passing SafeArray to COM?

9. URGENT : Passing SAFEARRAY of UDT from ATL DLL to VB

10. How to pass SAFEARRAY by VARIANT

11. EASY Way to Pass SAFEARRAY from VC to VB using COM

12. Copying Variables ByRef

 

 
Powered by phpBB® Forum Software