Wednesday, July 3, 2013

DeRef Dalam VB6

Dengan memanfaatkan salah satu fungsi (GetMem4) yang berada dalam runtime VB6 (msvbvm60.dll).
Option Explicit 

Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
Private Declare Sub GetDWord Lib "MSVBVM60.dll" Alias "GetMem4" (ByRef inSrc As Any, ByRef inDst As Long)

Public Function DeRef(ByVal ptr As Long) As Long
If (ptr) Then
Call GetDWord(ByVal ptr, DeRef)
End If
End Function

Private Sub Command1_Click()

Dim lngTest As Long
Dim lngAddress As Long

lngTest = 100
lngAddress = VarPtr(lngTest) 'lngAddress sekarang berisi pointer lngTest
Text1.Text = DeRef(lngAddress) 'Perhatikan sekarang Text1.Text = 100 (yang berasal dari pointer lngTest)

End Sub