Solved

How to stop an application running from another application

Posted on 2002-05-22
8
141 Views
Last Modified: 2010-05-02
I've got a VB app which uses a shell command to start another vb app. I would also like to add the ability to then close this second app down. Can anyone help?

Many thanks,

Bryn.





 
0
Comment
Question by:dustybryn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7027454
If it is not a DOS windows, you could use FindWindow Api to locate the main window of other program and PostMessage API with parameter got from findwindow and WM_CLOSE constant:
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 50 total points
ID: 7027458
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10

sub CloseWin(sCaption as string)
dim ret as long
ret =findwindow(vbnullstring,scaption)
if ret <>0 then
   postmessage ret, WM_CLOSE
else
   msgbox "Unable to locate window",vbexclamation.app.exe.name
end if
end sub

Hope it helps
0
 
LVL 20

Expert Comment

by:hes
ID: 7027540
Try using this

Declare:

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type

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

Private Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, ByVal lpCommandLine As _
String, ByVal lpProcessAttributes As Long, ByVal _
lpThreadAttributes As Long, ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, lpStartupInfo As _
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) _
As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO


'Code in form in this case it starts Calc.exe then closes
'it when you click on the command button

Private Sub Command1_Click()
  TerminateProcess proc.hProcess, 0
End Sub

Private Sub Form_Load()



  'Initialize the STARTUPINFO structure:
  start.cb = Len(start)

  'Start the shelled application:
  ret& = CreateProcessA(0&, "calc.exe", 0&, 0&, 1&, _
  NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)



End Sub
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7027611
Hes,
don't use terminateprocess API whitout calling CloseHandle  first!
I thint it's like clossing app with a bomb.
0
 
LVL 20

Expert Comment

by:hes
ID: 7027645
Richie,
LOL I had the declare for CloseHandle but didn't cut it and paste the call :)

Private Sub Command1_Click()
 TerminateProcess proc.hProcess, 0
End Sub

should be

Private Sub Command1_Click()
 ret& = CloseHandle(proc.hProcess)
 TerminateProcess proc.hProcess, 0
End Sub
0
 

Author Comment

by:dustybryn
ID: 7029157
Richie,
       I've tried using your code but when it gets to
'PostMessage ret, WM_CLOSE' it stops with the message "Argument not optional" is this just a missing parameter and if so do you know how i can get it going?
0
 

Author Comment

by:dustybryn
ID: 7029205
Richie,
       The postmessage call just needed two extra parameters of 0,0 to run.

Cheers,
        Dust.


0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7029529
oops!
i wrote it from memory and did not read my own declaration of PostMessage function. Sorry.
Thanks for "A" grade.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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