Solved

How to start and stop a program in VB

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

20 Experts available now in Live!

Get 1:1 Help Now