Solved

How to start and stop a program in VB

Posted on 2002-03-18
2
924 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

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…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

831 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