
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