hWnds Again...

MD9547
07-04-2005, 10:23 AM
The following is supposed to check if Internet Explorer Is Running, but my get_win_title function always seems to return an empty string, except when the active window is my app, then my apps title is retrieved correctly by the function. Anyone know what I'm doing wrong? :confused:


Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Declare Function GetWindowText Lib "user32" Alias _
"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
ByVal cch As Long) As Long


Private Function get_win_title() As String
Dim buf As String * 1024
Dim length As Long
Dim ahWnd As Long

ahWnd = GetActiveWindow()

length = GetWindowText(ahWnd, buf, Len(buf))
get_win_title = Left$(buf, length)

End Function


Private Sub Form_Load()
Me.Visible = False
Timer1.Interval = 5000
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
If Right(get_win_title, 29) = "- Microsoft Internet Explorer" Then
MsgBox "Uh oh!"
End If
End Sub

DubbleClick
07-04-2005, 10:53 AM
I always prefer to check the running processes myself.

Give this a try. I just pasted some stuff together to do this. You can clean it up as you want to.

Option Explicit

Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)

Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260

Const IEXPLORER As String = "IEXPLORE.EXE"

Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type

Private Sub Form_Load()

Dim lng As Long
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim strProcess As String

'Takes a snapshot of the processes and the heaps, modules, and threads used by the processes
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
'set the length of our ProcessEntry-type
uProcess.dwSize = Len(uProcess)
'Retrieve information about the first process encountered in our system snapshot
lng = Process32First(hSnapShot, uProcess)
Do While lng
strProcess = Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))
'Retrieve information about the next process recorded in our system snapshot
If UCase(strProcess) = IEXPLORER Then
MsgBox "IE Is running"
Exit Do
End If
lng = Process32Next(hSnapShot, uProcess)
Loop
'close our snapshot handle
CloseHandle hSnapShot
End Sub

Mathimagics
07-04-2005, 11:15 AM
You are using the wrong API. GetActiveWindow only returns a window handle within your own process. If the target IE window is indeed the (system) active window, it's the foreground window, and you can retrieve its handle with GetForegroundWindow

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum