Solved

How to stop an application running from another application

Posted on 2002-05-22
8
138 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

733 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