Search Tools Links Login

How to Control a Shelled Application


This module will allow you to launch a shelled task, and monitor if it is running or not.

Module

Option Explicit

'Structures declaration
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

'Function declaration
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32.dll" (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

'Constants
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Public Function ExecuteTask(Path As String, Optional Parameters As String = "") As Long
    Dim proc As PROCESS_INFORMATION
    Dim startup As STARTUPINFO
    Dim ret As Long
    Dim cmdline As String

    cmdline = Path
    If Trim(Parameters) <> "" Then
        cmdline = cmdline & " """ & Parameters & """"
    End If
    ' Initialize the STARTUPINFO structure:
    startup.cb = Len(startup)
    ' Start the application:
    ret = CreateProcessA(0&, cmdline, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, startup, proc)
    ExecuteTask = proc.hProcess
End Function

Public Function IsTaskRunning(taskID As Long) As Boolean
    Dim ret As Long

    If taskID = 0 Then Exit Function
    ret = WaitForSingleObject(taskID, 0)
    IsTaskRunning = (ret <> 0)
    If Not IsTaskRunning Then
        ret = CloseHandle(taskID)
        taskID = 0
    End If
End Function

Usage

Public taskID As Long

Private Sub Command1_Click()
    taskID = ExecuteTask("Notepad")
End Sub

Private Sub Command2_Click()
    If IsTaskRunning(taskID) Then
        MsgBox "Task running", vbInformation
    Else
        MsgBox "Task not running", vbExclamation
    End If
End Sub

About this post

Posted: 2019-10-07
By: AndreaTincani
Viewed: 299 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

This code originally appeared on AndreaVB.com, and has been republished here with the permission of Andrea Tincani.


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.