Results 1 to 8 of 8

Thread: [RESOLVED] How would it be possible to...

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] How would it be possible to...

    How would it be possible to close all instances of my program at the same time?


    Say if someone clicks "exit" on one instance of my program, how could i make it also close all other instances?

    The real question here is it possible to do this directly? Without writing to a file or the registry & have the other program constantly reading that location?

    Would it be possible to have a program "listen" for a command, or when somthing is called in one program it's also called in all the others, i think this is possible with classes but it's been a while.

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: How would it be possible to...

    I suppose you could attempt to use the FindWindow() API function to search for any instances of the application and close them. Ill try figure some other things out to

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: How would it be possible to...

    Yeah, ok will do.

    What's VB's class name? ThunderRT6Main?

    I'm thinking something like this:

    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Sub Close_All()
    Dim Index As Long
    Dim Closer_hWnd As Long
    Dim Closer_Hdle As Long
    Dim sOldCaption As String
    
    sOldCaption = Me.Caption
    
    Me.Caption = "Closing..."
    
    For Index = 0 To 500
    
    Closer_hWnd = FindWindow("ThunderRT6Main", Me.Caption)
    
    If Closer_hWnd <= 1 Then GoTo Final_Close
    
    Closer_Hdle = GetWindow(Closer_hWnd, GW_HWNDPREV)
    PostMessage Closer_Hdle, WM_CLOSE, 0, 0
    PostMessage Closer_Hdle, WM_QUIT, 0, 0
    
    Next Index
    
    Final_Close:
    Me.Caption = sOldCaption
    Closer_hWnd = FindWindow("ThunderRT6Main", Me.Caption)
    
    If Closer_hWnd <= 1 Then MsgBox "Error, some instances may still be open"
    
    Closer_Hdle = GetWindow(Closer_hWnd, GW_HWNDPREV)
    PostMessage Closer_Hdle, WM_CLOSE, 0, 0
    PostMessage Closer_Hdle, WM_QUIT, 0, 0
    
    Unload Me
    End
    
    End Sub
    The thing is however, it might close itself before all the other ones are closed (since it might not be the last instance that opened up calling it)?

    *** Edit: Actually, i think i fixed that by changing the caption.
    Looks good?
    Last edited by Slyke; May 3rd, 2007 at 01:12 AM.

  4. #4
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: How would it be possible to...

    I would be cheap, disgusting and hacky in this one myself.
    vb Code:
    1. AppActivate Me.Caption
    2. SendKeys "%{F4}" 'Gag @ SendKeys

    Then just trap the error that pops up when the window isn't found, means no more exist.

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: How would it be possible to...

    SendKeys is very inefficient i am completely against it. And won't that shut down all applications ?

  6. #6
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: How would it be possible to...

    Nah, just all instances of his application... Unless all programs have the same window title as his.
    Yes, I hate SendKeys, hence the comment in that code.

    But my god, I am so lazy sometimes.

  7. #7
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: How would it be possible to...

    Atleast it works

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: How would it be possible to...

    Lol!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width