-
Sep 1st, 2012, 12:20 PM
#1
Thread Starter
New Member
How to get poniter of array in Variant
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
ReDim a(1 To 10) As Long
For w = 1 To 10
a(w) = w
Next
Debug.Print VarPtr(a(1)), Q(a)
' Error solution: Q(a) <> VarPtr(a(1))
' Positive solution: how to let Q(a) solution Equal VarPtr(a(1))
End Sub
Function Q(ar) As Long
Q = VarPtr(ar(1))
' purpose of the Problem is the following
Redim ar_2(1 to 10) as long
call CopyMemory ( varptr(ar_2(1)) , Q , 40)
End Function
Last edited by quickcccc; Sep 1st, 2012 at 12:32 PM.
-
Sep 1st, 2012, 01:23 PM
#2
Re: How to get poniter of array in Variant
Can't really make heads or tails out of that. You have not dimmed anything and you are using a as an array but also using it like a number
-
Sep 2nd, 2012, 03:29 AM
#3
Re: How to get poniter of array in Variant
Why do you want the pointers in a Variant ? come to that why do you want the addresses in the first place?
(EDIT: You understand that VarPtr stands for 'Variable Pointer' not 'Variant Pointer' don't you?)
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Sub Command_Click()
Dim intI As Integer
Dim w(9) As Long
Dim w_1(9) As Long
Dim w_2(9) As Long
For intI = 0 To 9
'
' Put some values into the array w
' Put the pointer to each element into w_1 array
' (which is a bit absurd since once you know the address
' of the first element you just need to add 4 to get the next one)
'
w(intI) = CLng(intI)
w_1(intI) = VarPtr(w(intI))
Next intI
For intI = 0 To 9
'
' Output the Address of each element to the Immediate Window
'
Debug.Print Hex(w_1(intI))
Next intI
'
' This will copy the values of the w array into w_2
' using the Address of the first element obtained from VarPtr
'
CopyMemory w_2(0), ByVal w_1(0), (UBound(w_2) + 1) * 4
For intI = 0 To 9
Debug.Print w_2(intI)
Next intI
'
' This will also copy the values of the w array to w_2
' So it begs the Question -
' why do you want the Addresess of the elements?
'
CopyMemory w_2(0), w(0), (UBound(w_2) + 1) * 4
For intI = 0 To 9
Debug.Print w_2(intI)
Next intI
End Sub
Last edited by Doogle; Sep 2nd, 2012 at 03:47 AM.
Reason: Modified just about everything
-
Sep 2nd, 2012, 06:31 AM
#4
Thread Starter
New Member
Re: How to get poniter of array in Variant
yes
I understand that VarPtr stands for 'Variable Pointer'
I attempt to find poniter as the following
http://www.vbforums.com/showthread.p...73#post4228173
-
Sep 2nd, 2012, 06:58 AM
#5
Thread Starter
New Member
Re: How to get poniter of array in Variant
Code:
Public Sub AR_Copy_Long(store() As Long, store_lb, src() As Long, Optional lb, Optional ub)
If IsMissing(lb) Then lb = LBound(src)
If IsMissing(ub) Then ub = UBound(src)
CopyMemory store(store_lb), src(lb), (ub - lb + 1) * Len(src(lb))
End Sub
Public Sub AR_Copy_Sng(store() As Single, store_lb, src() As Long, Optional lb, Optional ub)
If IsMissing(lb) Then lb = LBound(src)
If IsMissing(ub) Then ub = UBound(src)
CopyMemory store(store_lb), src(lb), (ub - lb + 1) * Len(src(lb))
End Sub
Public Sub AR_Copy_dbl(store() As Double, store_lb, src() As Long, Optional lb, Optional ub)
If IsMissing(lb) Then lb = LBound(src)
If IsMissing(ub) Then ub = UBound(src)
CopyMemory store(store_lb), src(lb), (ub - lb + 1) * Len(src(lb))
End Sub
' =======
' if I can get poniter of array in Variant ,
' I will have the onlyone sub
Private Declare Sub CopyMemory2 Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Public Sub AR_Copy_Any(store, store_lb, src, Optional lb, Optional ub)
If IsMissing(lb) Then lb = LBound(src)
If IsMissing(ub) Then ub = UBound(src)
if (ub-lb) > (ubound(store) - store_lb) then
err.raise ....
exit sub
end if
src_ptr = ? ' how to get poniter of array in Variant ?
store_ptr = ?
CopyMemory2 store_ptr, src_ptr, (ub - lb + 1) * Len(src(lb))
End Sub
Last edited by quickcccc; Sep 2nd, 2012 at 07:07 AM.
-
Sep 2nd, 2012, 09:04 AM
#6
Re: How to get poniter of array in Variant
OK, but you still haven't said exactly what you're trying to achieve.
-
Sep 2nd, 2012, 10:14 AM
#7
Thread Starter
New Member
Re: How to get poniter of array in Variant
VC++ 6.0 Variant define as the following
Code:
union {
struct __tagVARIANT {
VARTYPE vt;
WORD wReserved1;
WORD wReserved2;
WORD wReserved3;
union {
LONG lVal; /* VT_I4 */
BYTE bVal; /* VT_UI1 */
SHORT iVal; /* VT_I2 */
FLOAT fltVal; /* VT_R4 */
DOUBLE dblVal; /* VT_R8 */
VARIANT_BOOL boolVal; /* VT_BOOL */
_VARIANT_BOOL bool; /* (obsolete) */
SCODE scode; /* VT_ERROR */
CY cyVal; /* VT_CY */
DATE date; /* VT_DATE */
BSTR bstrVal; /* VT_BSTR */
IUnknown * punkVal; /* VT_UNKNOWN */
IDispatch * pdispVal; /* VT_DISPATCH */
SAFEARRAY * parray; /* VT_ARRAY */
BYTE * pbVal; /* VT_BYREF|VT_UI1 */
SHORT * piVal; /* VT_BYREF|VT_I2 */
LONG * plVal; /* VT_BYREF|VT_I4 */
FLOAT * pfltVal; /* VT_BYREF|VT_R4 */
DOUBLE * pdblVal; /* VT_BYREF|VT_R8 */
VARIANT_BOOL *pboolVal; /* VT_BYREF|VT_BOOL */
_VARIANT_BOOL *pbool; /* (obsolete) */
SCODE * pscode; /* VT_BYREF|VT_ERROR */
CY * pcyVal; /* VT_BYREF|VT_CY */
DATE * pdate; /* VT_BYREF|VT_DATE */
BSTR * pbstrVal; /* VT_BYREF|VT_BSTR */
IUnknown ** ppunkVal; /* VT_BYREF|VT_UNKNOWN */
IDispatch ** ppdispVal; /* VT_BYREF|VT_DISPATCH */
SAFEARRAY ** pparray; /* VT_BYREF|VT_ARRAY */
VARIANT * pvarVal; /* VT_BYREF|VT_VARIANT */
PVOID byref; /* Generic ByRef */
CHAR cVal; /* VT_I1 */
USHORT uiVal; /* VT_UI2 */
ULONG ulVal; /* VT_UI4 */
INT intVal; /* VT_INT */
UINT uintVal; /* VT_UINT */
DECIMAL * pdecVal; /* VT_BYREF|VT_DECIMAL */
CHAR * pcVal; /* VT_BYREF|VT_I1 */
USHORT * puiVal; /* VT_BYREF|VT_UI2 */
ULONG * pulVal; /* VT_BYREF|VT_UI4 */
INT * pintVal; /* VT_BYREF|VT_INT */
UINT * puintVal; /* VT_BYREF|VT_UINT */
struct __tagBRECORD {
PVOID pvRecord;
IRecordInfo * pRecInfo;
} __VARIANT_NAME_4; /* VT_RECORD */
} __VARIANT_NAME_3;
} __VARIANT_NAME_2;
DECIMAL decVal;
} __VARIANT_NAME_1;
};
try convert VC struct to VB Type ( but it exist error )
Code:
Private Type vt
vType As Integer
p1 As Long
p2 As Long
p3 As Long
p4 As Long
p5 As Long
p6 As Long
p7 As Long
p8 As Long
p9 As Long
End Type
-
Sep 2nd, 2012, 10:30 AM
#8
Thread Starter
New Member
Re: How to get poniter of array in Variant
Private Type vt
vType As integer
p1 As Long
p2 As Long
p3 As long
p4 As Long
End Type
Dim vt As vt
b = CStr("123")
Debug.Print VarType(b)
CopyMemory2 vt, VarPtr(b) - 4, Len(vt)
' get vt.vType = 8 ( Just right = VB VarType( string var) )
Private Type vt
vType As integer
p1 As Long
p2 As Long
p3 As long
p4 As Long
End Type
b = CLng(123)
Debug.Print VarType(b)
CopyMemory2 vt, VarPtr(b) - 4, Len(vt)
' get vt.vType = 3 ( = VB VarType( long var ) )
' vt.p3 = 123
Private Type vt
vType As integer
p1 As Long
p2 As Long
p3 As Single
p4 As Long
End Type
b = CSng(123)
Debug.Print VarType(b)
CopyMemory2 vt, VarPtr(b) - 4, Len(vt)
' get vt.vType = 4 ( = VB VarType( single var ) )
' vt.p3 = 123
Last edited by quickcccc; Sep 2nd, 2012 at 10:34 AM.
-
Sep 4th, 2012, 05:36 AM
#9
Re: How to get poniter of array in Variant
I still don't have a clue what he's trying to achieve
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|