Friday, June 8, 2012

VB6 Code - Mengambil URL Dari Address Bar IE

Mengenai cara mengambil URL dari adress bar yang terdapat pada IE menggunakan fungsi API, dengan melakukan spy terhadap HWND induk dan turunannya. Selain dengan API di bawah ini, kita pun bisa mengambil URL yang terdapat pada adress bar IE atau Firefox dengan menggunakan DDE.
Option Explicit 
Private Declare Function
FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function
SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const
WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE

Private Sub
FindIt(ByVal sClassName As String)
On Error GoTo CallErrorA
lhWnd = FindWindowEx(lhWnd, 0, sClassName, vbNullString)
End Sub
Private Function
GetAddressText() As String
On Error GoTo
CallErrorA
Dim usText() As Byte
Dim
iPos As Integer
lhWnd = 0
Call FindIt("IEFrame")
Call FindIt("WorkerA")
Call FindIt("ReBarWindow32")
Call FindIt("ComboBoxEx32")
Call FindIt("ComboBox")
Call FindIt("Edit")
ReDim usText(0 To SendMessage(lhWnd, WM_GETTEXTLENGTH, 0, ByVal 0&) + 1)
If
UBound(usText) = 1 Then
GetAddressText = ""
Else
usText(0) = UBound(usText) And 255
usText(1) = UBound(usText) 256
Call SendMessage(lhWnd, WM_GETTEXT, UBound(usText), usText(0))
GetAddressText = StrConv(usText, vbUnicode)
iPos = InStr(GetAddressText, vbNullChar)
If iPos > 0 Then GetAddressText = Left(GetAddressText, iPos - 1)
End If
End Function