Solved

How to stop an application running from another application

Posted on 2002-05-22
8
137 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

816 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

12 Experts available now in Live!

Get 1:1 Help Now