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
Solved

How to start and stop a program in VB

Posted on 2002-03-18
2
934 Views
Last Modified: 2012-08-14
I am writing a program to start and stop an external program in VB. All I know is the path to the program I wish to start and stop.

Does anyone have any ideas how I could control this external program for starting and stopping?

I think I got the shell(path) command working right to start, but what about stopping it?

Thanks!
0
Comment
Question by:magnakuz
2 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 100 total points
ID: 6877949
Terminate process started by Shell

1 form,2 command button
1 standard module bas
form code:
'--------------------------------------
Option Explicit
Dim lProcHand As Long

Private Sub Form_Load()
Command1.Enabled = True
Command2.Enabled = False
End Sub

Private Sub Command1_Click()
Command1.Enabled = False
Command2.Enabled = True
lProcHand = MShellAndClose.MyHandle
End Sub

Private Sub Command2_Click()
Command2.Enabled = False
Command1.Enabled = True
Me.Caption = Module1.Terminate(lProcHand)
End Sub
'--------------------------------------
'Module bas code
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

public lProcHand As Long

Public Function MyHandle() As Long
Dim lngExecOK As Long
lngExecOK = Shell("c:\windows\calc.exe", vbNormalFocus)
lProcessHandle = OpenProcess(&H100000, True, lngExecOK)
MyHandle = lProcessHandle
End Function

Public Function Terminate(ByVal lPhandle As Long) As Long
Terminate = TerminateProcess(lPhandle, 0)
End Function

0
 
LVL 20

Expert Comment

by:hes
ID: 6877969
'replace calc.exe with what you want

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&
Dim hproc As Long



Option Explicit

Private Sub Command1_Click() 'put this coce where you want to stop the created process
Dim ret As Long
TerminateProcess hproc, 0
End Sub

Private Sub Form_Load()
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long

'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)
hproc = proc.hProcess


End Sub
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 60
message box in access 4 51
MS Date Picker 64 bit 32 bit issue 12 60
Recommendation vb6 to vb.net or others 14 176
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

860 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