Link to home
Start Free TrialLog in
Avatar of bscmis
bscmis

asked on

Seems simple....close window(popup)

Seems like a simple little app but I am having trouble with it. I am not a VB programmer so please go easy with any sample code.

Here's what I would like my application to do:

1. every ten minutes loop through ALL open (visible windows).

2. if there are other windows open & visible besides one with the caption of "Backup" then close them (they are usually popup dialogs that are related to the "backup" application/window)

I am having problems finding sample code to close a window/popup without knowing its caption (which I don't)

Please provide sample code, I would definitely appreciate it.

Thanks
Avatar of bscmis
bscmis

ASKER

Increase points to 250
Here is the code that you've asked for...

Put this code in a module:

--begin of code--
Option Explicit

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
   
    Dim str As String
   
    str = String(255, Chr(0))
    If InStr(1, GetWindowText(app_hwnd, str, 255), "Backup") Then
        DestroyWindow (app_hwnd)
    End If
   
    ' Continue searching.
    EnumProc = 1
End Function

--end of code--

Now add a timer to the form, with internal of 600000 (ten minutes), and add the in the procedure...

--begin of code--

EnumWindows AddressOf EnumProc, 0

--end of code--
ASKER CERTIFIED SOLUTION
Avatar of twalgrave
twalgrave

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bscmis

ASKER

Sorry I haven't commented sooner but I am not receiving the e-mail notification for any of my questions.

Anyway, thank you both so much. I am testing it out right now.

After I finish testing it I will split the points between the both of you.
Please let me know if you disagree with splitting the points.

Thanks
Avatar of bscmis

ASKER

When I run the code it closes out Visual Basic, so I changed the follwing figuring it would solve the problem but it didn't:

If InStr(1, GetWindowText(app_hwnd, str, 255), "Project1 - Microsoft Visual Basic [design] ") = 0 Then
        DestroyWindow (app_hwnd)
    End If

I don't receive any errors it just closes, no other windows close though.

Please advise...thanks
email notifs back up.

you might want to change:
  str = String(255, Chr(0))
   If InStr(1, GetWindowText(app_hwnd, str, 255), "Backup") Then
       DestroyWindow (app_hwnd)
   End If


to:

  str = String(255, Chr(0))
  lResult = GetWindowText(app_hwnd, str, 255)
  str = trim$(str)

   If InStr(1, str , "Backup") Then
       call DestroyWindow (app_hwnd)
   End If

Let me know what happens
Avatar of bscmis

ASKER

Ok I tried that and it doesn't close out VB anymore but no windows close at all. Here is the code I have:

----------Module:

Option Explicit

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
    Dim str As String
    Dim lResult As Long
   
    str = String(255, Chr(0))
    lResult = GetWindowText(app_hwnd, str, 255)
    str = Trim$(str)

    If InStr(1, str, "Project1 - Microsoft Visual Basic [design]") Then
        Call DestroyWindow(app_hwnd)
    End If
   
   ' Continue searching.
   EnumProc = 1
End Function


----------Form w/Timer

Dim lNumTimesFired  As Long
Const FIRE_SECONDS = 6


Private Sub Timer1_Timer()
  lNumTimesFired = lNumTimesFired + 1

  If lNumTimesFired >= FIRE_SECONDS Then
     lNumTimesFired = 0
     EnumWindows AddressOf EnumProc, 0
  End If

End Sub
Avatar of bscmis

ASKER

Ok I tried that and it doesn't close out VB anymore but no windows close at all. Here is the code I have:

----------Module:

Option Explicit

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
    Dim str As String
    Dim lResult As Long
   
    str = String(255, Chr(0))
    lResult = GetWindowText(app_hwnd, str, 255)
    str = Trim$(str)

    If InStr(1, str, "Project1 - Microsoft Visual Basic [design]") Then
        Call DestroyWindow(app_hwnd)
    End If
   
   ' Continue searching.
   EnumProc = 1
End Function


----------Form w/Timer

Dim lNumTimesFired  As Long
Const FIRE_SECONDS = 6


Private Sub Timer1_Timer()
  lNumTimesFired = lNumTimesFired + 1

  If lNumTimesFired >= FIRE_SECONDS Then
     lNumTimesFired = 0
     EnumWindows AddressOf EnumProc, 0
  End If

End Sub
Avatar of bscmis

ASKER

oops sorry
Avatar of bscmis

ASKER

I modified the code to write the value of lResult to a text file during the first loop through all windows, and here is a small sample of the output I got.

50
0
0
0
0
0
0
0
0
0
8

Let me know if I can give any info to help.
Avatar of bscmis

ASKER

Ok nevermind that last post.

When I debug the code I check the str variable everytime DestroyWindow is called and I see that it contains the caption of something that should be closed but when DestroyWindow is then called the window is not closed.

But....I noticed that my "Immediate" window in VB closes.

Here are some windows that are not visible to me and I don't know if it is safe to be closing them:

CSC Notifications Window
Power Meter                                  
Connections Tray
WMS Idle
WMS Spooler
WMS ST Notif Window 000005B0 000006A4
DDE Server Window
MCI command handling window
Scan                                              
ACTION                                          
VPIPCLINK                
SYSTEM AGENT COM WINDOW                        
MM Notify Callback                          
MS_WebcheckMonitor      
Delete                                            
SysFader
Program Manager
Default IME
Avatar of bscmis

ASKER

Well I decided that closing windows without knowing their caption isn't a good idea, I can't open Visual Basic anymore. I am reinstalling it right now.

So now the code is going to check for a specific window and close it.

But I still have a problem. It finds the window, calls the Destroy function, but the window doesn't close.
D'oh.  That indeed is a problem with blindly doing something globally.

As far as the Destroy function not working...is it returning an error code (make sure to check err.LastDLLError as well.) or is it just not doing anything?  Which applications is it failing to destroy or is it all of them?
Avatar of bscmis

ASKER

I am using the following line to close the window now and it seems to be working fine:

PostMessage app_hwnd, WM_CLOSE, 0, 0&

I am awarding you with the points since you followed up with me.

Thank you so much for pointing me in the right direction, it is much appreciated!

lauren
lauren,
You are most welcome and thank you too.  Have a great day!
Avatar of bscmis

ASKER

thanks you too