Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how do you dereference a variable pointer and get it's value back?

Posted on 2004-11-04
10
Medium Priority
?
1,698 Views
Last Modified: 2008-02-01
Seen many articles here on the use of varptr() to get the address of a variable, but how do you dereference it to get a variable's value from the address?
0
Comment
Question by:keyboards
  • 5
  • 4
10 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 12499435
What type of variable are you getting the address of?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12499895
You can use rtlMoveMemory API (aka CopyMemory) to dereference a pointer:

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Command1_Click()

    Dim lSource As Long
    Dim lDest As Long
    Dim lPointer As Long
   
    lSource = 101
    lPointer = VarPtr(lSource)
   
    'get the value from the pointer
    Call CopyMemory(lDest, ByVal lPointer, LenB(lSource))
   
    Debug.Print lDest

End Sub
0
 

Author Comment

by:keyboards
ID: 12500174
So, at least as far as the above technique, you can't dereference the address to see what's there directly, but have to copy what's there to another location that is already of the correct type, yes?

Truthfully, I have dynamic arrays of user defined types, and I'd just like to be able to pass any of these arrays to a function.  But the arrays are not pre-declared, and have to be allocated at run-time.

Is there such a thing as a collection of arrays?  Or any other VB object type that would allow dynamic instantiation of at least a reference to something dynamically dimentioned?

I appreciate any advice, unless it's going to be "do it in C" *lol*; which I know would be a snap after porting.  Just can't screw myself up to the porting effort without exhausting all VB options. ;0}

Regards
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 32

Expert Comment

by:Erick37
ID: 12500384
As long as you know the Type of the array, you can pass a dynamic array of user Types to a function.

e.g.

Private Sub Command1_Click()

    Dim udtArray() As MyType
    Dim i As Long
   
    For i = 0 To 9
        ReDim Preserve udtArray(i)
        udtArray(i).member1 = i
        udtArray(i).member2 = i + 100
        udtArray(i).member3 = i + 1000
    Next
   
    Call PrintUDTArray(udtArray())
   
End Sub


Private Sub PrintUDTArray(udta() As MyType)
   
    Dim i As Long
   
    For i = 0 To UBound(udta)
        Debug.Print udta(i).member3
    Next
   
   
End Sub
0
 

Author Comment

by:keyboards
ID: 12500532
you didn't pass it the address of the array.  I don't know what the name of the array will be at "compile time", it's dynamically allocated at run time.

I specifically need to know the syntax for obtaining, passing, and dereferencing the address of the array, without knowing it's name.
0
 

Author Comment

by:keyboards
ID: 12500738
...and I guess I have to add "without copying the entire contents of the array passed to the function into another variable within the function". The dynamic arrays hold structures representing event absolute time, duration and magnatude and there might be a few million in a given array; this makes repeated copying prohibitive....unless we know the entire size of the array in memory and it's guarenteed contiguous and can be copied as a chunk relatively quickly and THEN dereferenced within the function.
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 1600 total points
ID: 12500827
You can grab any member of your array using CopyMemory without having to copy the entire array.  You will need a pointer to the array, plus the size of the member UDT in order to do this.

The array will always be contiguous in memory.

e.g.

'Get the pointer to the array
Call PrintUDTArray( VarPtr( udtArray(0) ) )



~~~~~~~~~~~~~~~
'Pass in a pointer to the beginning of the array
Private Sub PrintUDTArray(ByVal UDTPtr As Long)
   
    Dim udt As MyType
    Dim offset As Long
   
    'Use CopyMemory to get the values of any member of the array
   
    'Get the 5th member of the array
    offset = LenB(udt) * 5
   
    'Copy the 5th member to a local variable
    Call CopyMemory(udt, ByVal (UDTPtr + offset), LenB(udt))
   
    Debug.Print udt.member1, udt.member2, udt.member3
   
End Sub

0
 
LVL 32

Expert Comment

by:Erick37
ID: 12500848
Also be advised that CopyMemory does no bounds checking.  If you attempt to read or write to memory outside the heap, the program will crash and burn.  You must know that the pointer is valid and the extent of the array.

Save your work before running in the IDE.
0
 

Author Comment

by:keyboards
ID: 12500975
Great stuff Erik, thanks for all the advice and code.  It shows me what the capabilities and limitations of VB are and that's it'll be quite a poor choice for this project, though it's done well enough for the slightly dumbed down draft version.

Imagine a mess of real-time events as input, each of a different, uncombinable type.  But you don't know what types there can possibly be or what event types you're going to get on any particular run, nor how many unique event types.  Similar types of events must be delt with along side each other but separate from other types, so you have to allocate an array for each type as it comes in, populate it,  and be able to find it again when another event of the same type is encountered from the input.  Thus I have to create a logical index of keys and addresses for dynamically allocated arrays for each event type I encounter from the input.  It may just seem like sorting from this superficial description, or that some presorting/prefiltering should have been done to the input; I'll just have to assure you it cannot.  And all event types cannot be in one array with an event_type property for reasons I cannot mention here.  And there is too much data to physically copy each time a new event type is encountered when there are potentially millions of events of each of the various kinds encountered at random distributions, and still meet performance requirements.

Lovely eh?  ;0}

Thanks again,
Don
0
 

Author Comment

by:keyboards
ID: 12508338
Got this link http://www.ftponline.com/archives/premier/mgznarch/vbpj/2000/08aug00/bb0008/bb0008.asp from another VB solution post here on EE.  Excellent article on casting variables through function aliasing.  That's the effect I was looking for, though the technique is somewhat surprising.

Regards,
Don (aka Keyboards)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question