?
Solved

How to start and stop a program in VB

Posted on 2002-03-18
2
Medium Priority
?
946 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
[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
2 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Suggested Courses

765 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