Solved

How to stop an application running from another application

Posted on 2002-05-22
8
136 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now