Results 1 to 5 of 5

Thread: Pointers in VB

Threaded View

  1. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Pointers in VB

    Sure, you can find some more details by googling too....

    StrPtr. Value is the memory location where the actual string data/chars begin. 4bytes before that memory location is the length of the string.
    Code:
    Dim s As String, I As Integer, J As Long, L As Long
    s="Hi"
    CopyMemory I, ByVal StrPtr(s), 2&
    CopyMemory J, ByVal StrPtr(s)+2, 2&
    CopyMemory L, ByVal StrPtr(s)-4, 4&
    Debug.print s,chr$(i);chr$(j)," Length=";L
    
    Try this:  StrPtr(vbNullString), StrPtr("")
    VarPtr. Memory location where the variables contents can be found. This may be another pointer or it could be the actual variable's value. For example... The value found at memory address: VarPtr(stringSomething)= StrPtr(stringSomething) which points to the string data.
    Code:
    Dim X as Long, V As Long, S As String
    s="Hi"
    CopyMemory V, ByVal VarPtr(s), 4&
    Debug.Print "StrPtr(s)="; StrPtr(s); " Value at VarPtr(s) is ";V
    
    X=1962&
    CopyMemory V, ByVal VarPtr(X), 4&
    Debug.Print "X=";x; " Value at VarPtr(x) is "; V
    ObjPtr relates to stuff associated with Set commands, forms, controls, classes, etc, etc. It won't do you much good unless you are very familiar with the structure of what it is pointing too. But one neat test that can be performed.
    Code:
    Dim tPic As stdPicture
    Debug.Print "tPic is Nothing & ObjPtr(tPic)="; ObjPtr(tPic) ' should be zero
    Set tPic = New stdPicture
    Debug.Print "tPic is not Nothing & ObjPtr(tPic)="; ObjPtr(tPic) ' should NOT be zero
    
    ' Note: ObjPtr is often used to create unreferenced copies of objects from a 
    ' previously cached ObjPtr() value... 
    ' When using Set tPic = oldPic, the reference count on oldPic is incremented 
    ' and then when Set tPic = Nothing the count is decremented & when the 
    ' count = 0& the object is destroyed.  By using a statment like:
    Dim tmpObj As Object, oPtr As Long
    Set tPic = Me.Icon
    oPtr = ObjPtr(tPic)
    CopyMemory tmpObj, oPtr, 4&
    Debug.Print "tPic Pointer & Handle is "; ObjPtr(tPic); tPic.Handle; "  tmpPic Pointer & Type is "; ObjPtr(tmpObj); tmpObj.Handle
    ' tmpObj is same as tPic but the reference count wasn't incremented.
    ' This has the nasty effect of crashing if the tmpObj isn't cleared before
    ' tPic is destroyed: Cleared  as shown below
    CopyMemory tmpObj, 0&, 4&
    One can go on & on having fun. Careful though, CopyMemory if using invalid values or using ByRef/ByVal incorrectly can crash your app & possibly windows. Here is the declaration I used:
    Code:
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
    Last edited by LaVolpe; Dec 13th, 2007 at 10:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width