Solved

How to start and stop a program in VB

Posted on 2002-03-18
2
939 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

756 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