How do I close an external program with a Command button

Just an honest, possibly impossible, question...

Page 1 of 1

14 Replies - 8119 Views - Last Post: 21 November 2008 - 01:29 PM Rate Topic: -----

#1 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

How do I close an external program with a Command button

Posted 20 November 2008 - 03:10 PM

Is there a way to close an external program with a VB Command button? I am using VB6 and I have my program running an external editor for one of it's secondary functions. I hope someone knows a way to do this.


P.S. Hopefully I posted this one in the right forum.... :blink:

Private Sub cmdExit_Click()
	Unload Me
	Unload frmComCalc
	Unload frmTreas
	Unload frmEncTable
	
'   Close PCProfiler.exe
End Sub


The PCProfiler is the external program I'm trying to close. I can't figure it out to save my life.

This post has been edited by Jaalenn: 20 November 2008 - 08:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I close an external program with a Command button

#2 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 08:04 PM

Try:

Call Shell("C:\WHEREVERITIS\APPS\PCProfiler.exe /CLOSE FILENAME.EXTENSION")

I think you have to call the shell to start the app and to close. I might be stearing you in the wrong direction!

This post has been edited by absynthe: 20 November 2008 - 08:24 PM

Was This Post Helpful? 1
  • +
  • -

#3 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 08:28 PM

Oh well... It didn't work but it's all good. People will just have to close the app manually. Thanks though!
Was This Post Helpful? 0
  • +
  • -

#4 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 08:30 PM

View PostJaalenn, on 20 Nov, 2008 - 09:28 PM, said:

Oh well... It didn't work but it's all good. People will just have to close the app manually. Thanks though!



Hang on..look at these two sites for what you're trying to do :)

http://support.micro...kb;en-us;176391

http://www.codenewsg.../topic2719.aspx
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5346
  • View blog
  • Posts: 27,302
  • Joined: 10-May 07

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 08:32 PM

View PostJaalenn, on 20 Nov, 2008 - 04:10 PM, said:

Is there a way to close an external program with a VB Command button?

I'm no VB programmer, but you should be able to find the process id & kill it using Windows API calls.
Was This Post Helpful? 0
  • +
  • -

#6 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 08:40 PM

I've got the master coming to look at this for you :)

Jaaleen post that chunk of code you sent me.


I'm learning from this too so this helps me and you! Good post :P
Thank you Richard!

This post has been edited by absynthe: 20 November 2008 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#7 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 08:51 PM

Here it is, in all of its Naked Glory!!

Private Sub
End Sub



That really was too much to keep up....

This post has been edited by Jaalenn: 20 November 2008 - 09:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5346
  • View blog
  • Posts: 27,302
  • Joined: 10-May 07

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 09:00 PM

Instead of all of the if/else if statements, why don't use you use a switch (select?) case statement. It's pretty much what it's designed for. :^:
Was This Post Helpful? 1
  • +
  • -

#9 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 09:02 PM

I actually never thought about that... We didn't cover those in class when I was in college. I'll look into it. Thanks for the idea man!!!
Was This Post Helpful? 0
  • +
  • -

#10 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 09:06 PM

I wonder if you could use an enum as well?
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 09:12 PM

No2Pencil is correct, you can accomplish this using Win32 API calls (I'm sure there's other ways but this is the way I know. Create a Code Module and put this code into it. When using it call EndApplication, passing it the caption of the application you're looking for and it will end it for you.

NOTE: Be careful with this, it will end all instances of applications with the caption your provide

First the Win32 API declarations
Private Const PROCESS_ALL_ACCESS = &H1F0FFF

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

Private Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long

Private Declare Function GetNextWindow Lib "user32" _
Alias "GetWindow" (ByVal hWnd As Long, ByVal wFlag As Long) _
As Long

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long


Private Declare Function GetExitCodeProcess Lib "kernel32" _
   (ByVal process As Long, lpExitCode As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" _
  (ByVal process As Long, ByVal uExitCode As Long) As Long



The next two functions are called by EndApplication:

FindWindowByTitle:
Private Function FindWindowByTitle(title As String) As Long
	Dim handle As Long
	Dim caption As String
	Dim windowTitle As String
	
	handle = Me.hWnd
	windowTitle = LCase(str)
	
	Do	
	   DoEvents
	      If handle = 0 Then Exit Do
	        caption = LCase$(GetWindowCaption(handle))
	             
	
	       If InStr(caption, windowTitle) Then	
	          FindWindowByTitle = handle
	          Exit Do
	       Else
	         FindWindowByTitle = 0
	       End If
	
	       handle = GetNextWindow(handle, 2)	
	Loop
End Function



GetWindowCaption:
Private Function GetWindowCaption(handle As Long) As String
	Dim str As String
	Dim length As Long
	
    length& = GetWindowTextLength(handle)
    str = String(length, 0)
    Call GetWindowText(handle, str, length + 1)
    GetWindowCaption = str
End Function



Now the function that does all the work
Public Function EndApplication(ByRef caption As String) As Boolean
	Dim hwnd As Long
	Dim appInstance As Long
	Dim process As Long
	Dim processID
	Dim result As Boolean
	Dim exitCode As Long
	Dim returnValue As Long
	
	On Error GoTo ErrorHandler
	
	If Trim(caption) = "" Then Exit Function
	Do
		hwnd = FindWindowByTitle(caption)
		If hwnd = 0 Then Exit Do
		appInstance = GetWindowThreadProcessId(hwnd, processID)
		'Get handle to process
		process = OpenProcess(PROCESS_ALL_ACCESS, 0&, processID)
		If process <> 0 Then
		    'get exit code
		    GetExitCodeProcess process, exitCode
		        If exitCode <> 0 Then
		                'bye-bye
		            returnValue = TerminateProcess(process, exitCode)
		            If result = False Then result = returnValue > 0
		        End If
		End If
	Loop	
	EndApplication = result
	ErrorHandler:
            MsgBox(err.Description)
End Function



I know this seems rather complicated, but it's the only way I personally know of to accomplish this.

Hope this helps :)
Was This Post Helpful? 1
  • +
  • -

#12 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 09:15 PM

Wow! I just learned something! Wonder if this could be used in VB.NET? I've never used anything but..
Was This Post Helpful? 0
  • +
  • -

#13 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 09:20 PM

Wow! Thanks a lot Psycho. Sadly the forum will not allow me to thank you properly (with the "This Post Was Helpful, Thanks" button). I wonder why.....
Was This Post Helpful? 0
  • +
  • -

#14 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How do I close an external program with a Command button

Posted 20 November 2008 - 11:26 PM

Found some logic errors, cannot use Me in a Module. ere is the updated (tested code)

Private Const PROCESS_ALL_ACCESS = &H1F0FFF

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

Private Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Private Declare Function GetNextWindow Lib "user32" _
Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) _
As Long

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, lpdwProcessId As Long) As Long


Private Declare Function GetExitCodeProcess Lib "kernel32" _
   (ByVal process As Long, lpExitCode As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" _
  (ByVal process As Long, ByVal uExitCode As Long) As Long
  
Public Function EndApplication(ByRef caption As String, ByRef frm As Form) As Boolean
    Dim hwnd As Long
    Dim appInstance As Long
    Dim process As Long
    Dim processID
    Dim result As Boolean
    Dim exitCode As Long
    Dim returnValue As Long
    
    On Error GoTo Error
    
    If Trim(caption) = "" Then Exit Function
    Do
        hwnd = FindWindowByTitle(caption, frm)
        If hwnd = 0 Then Exit Do
        appInstance = GetWindowThreadProcessId(hwnd, processID)
        'Get a handle for the process we're looking for
        process = OpenProcess(PROCESS_ALL_ACCESS, 0&, processID)
        If process <> 0 Then
            'Next get our exit code (for use later)
            GetExitCodeProcess process, exitCode
                'Check for an exit code of 9 (zero)
                If exitCode <> 0 Then
                     'It's not zero so close the window
                    returnValue = TerminateProcess(process, exitCode)
                    If result = False Then result = returnValue > 0
                End If
        End If
    Loop
    EndApplication = result
Error:
    MsgBox (Err.Number & ": " & Err.Description)
End Function

Private Function FindWindowByTitle(ByRef str As String, ByRef frm As Form) As Long
    Dim handle As Long
    Dim caption As String
    Dim sTitle As String
    
    handle = frm.hwnd
    sTitle = LCase(str)
    Do
       DoEvents
          If handle = 0 Then Exit Do
            caption = LCase$(GetWindowCaption(handle))
    
           If InStr(caption, sTitle) Then
              FindWindowByTitle = handle
              Exit Do
           Else
             FindWindowByTitle = 0
           End If
           handle = GetNextWindow(handle, 2)
    Loop
End Function

Private Function GetWindowCaption(ByRef handle As Long) As String
    Dim str As String
    Dim length As Long
    
    length& = GetWindowTextLength(handle)
    str = String(length, 0)
    Call GetWindowText(handle, str, length + 1)
    GetWindowCaption = str
End Function



And sample usage

Private Sub Command1_Click()
    Shell "notepad", vbNormalFocus
End Sub

Private Sub Command2_Click()
    EndApplication "Notepad", Me
End Sub



Sorry for the confusion and hope this helps :)

@absynthe: I'm sure I could work up a VB.NET version :)
Was This Post Helpful? 1
  • +
  • -

#15 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: How do I close an external program with a Command button

Posted 21 November 2008 - 01:29 PM

That amazes me how quickly you did all of that!! I see you got it into the tutorials so that is great!! It's awesome when a post like this can help more than just one :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1