Solved

How to stop an application running from another application

Posted on 2002-05-22
8
135 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 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

16 Experts available now in Live!

Get 1:1 Help Now