Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Seems simple....close window(popup)

Posted on 2003-02-19
16
Medium Priority
?
1,220 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:bscmis
  • 11
  • 4
16 Comments
 

Author Comment

by:bscmis
ID: 7982771
Increase points to 250
0
 
LVL 1

Expert Comment

by:Munim
ID: 7983455
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--
0
 
LVL 16

Accepted Solution

by:
twalgrave earned 1000 total points
ID: 7985425
I'm changing some of the previous code:

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

needs to be changed to:

   If InStr(1, GetWindowText(app_hwnd, str, 255), "Backup") = 0 Then
       DestroyWindow (app_hwnd)
   End If

Only destroy the windows if it DOESN'T contain the work "Backup"

Also:
"Now add a timer to the form, with internal of 600000 (ten minutes), and add the in the procedure..." This cannot be done in VB.  Timer interval can only be set to 32767

What you need to do is to set it for 1 second and increment a static variable such as:

Timer1's interval is set to 1000

dim lNumTimesFired  as long
Const FIRE_SECONDS = 600

private Sub Timer1_Timer()
   lNumTimesFires = lNumTimesFires+1
   if lNumTimesFired >= FIRE_SECONDS then

      lNumTimesFired =0
      EnumWindows AddressOf EnumProc, 0

   end if

end sub

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:bscmis
ID: 7987809
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
0
 

Author Comment

by:bscmis
ID: 7987938
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
0
 
LVL 16

Expert Comment

by:twalgrave
ID: 7988422
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
0
 

Author Comment

by:bscmis
ID: 7988537
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
0
 

Author Comment

by:bscmis
ID: 7988704
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
0
 

Author Comment

by:bscmis
ID: 7988720
oops sorry
0
 

Author Comment

by:bscmis
ID: 7988900
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.
0
 

Author Comment

by:bscmis
ID: 7989253
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
0
 

Author Comment

by:bscmis
ID: 7993585
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.
0
 
LVL 16

Expert Comment

by:twalgrave
ID: 8000048
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?
0
 

Author Comment

by:bscmis
ID: 8008679
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
0
 
LVL 16

Expert Comment

by:twalgrave
ID: 8009207
lauren,
You are most welcome and thank you too.  Have a great day!
0
 

Author Comment

by:bscmis
ID: 8009235
thanks you too
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question