Handling Dependent Processes

Question:
I need execute some commands to rename/copy files. What is the best method of performing this type of function.I do not want to fire off a batch file, but have all commands executed one after the other in VB. Is this possible?

Answer:
This was a common problem under VB 3.0 also. The solution there was to use the GetModuleUsage routine. However, under version 4.0, this doesn’t work. The following code will provide the same functionality under version 4.0.

Attribute VB_Name = “Shell” ‘************************************************************************************************************’  PROCESS STUFF’************************************************************************************************************Public Const PROCESS_QUERY_INFORMATION = &H400Public Const PROCESS_TERMINATE = &H1Public Const STILL_ACTIVE = &H103’THE REMAINING CONSTANTS FOUND IN WINNT.H Public Declare Function OpenProcess Lib “kernel32” (ByVal dwDesiredAccess&, ByVal bInheritHandle&, ByVal dwProcessId&) As LongPublic Declare Function GetExitCodeProcess Lib “kernel32” (ByVal hProcess As Long, lpExitCode As Long) As LongDeclare Function TerminateProcess Lib “kernel32″ (ByVal hProcess As Long, ByVal uExitCode As Long) As Long  Sub ShellAndWait(strApp As String)’***********************************************************************************************’ PURPOSE:      to shell to an app, wait for it to finish and then come back’ only a 32 bit app.  Do not use TerminateProcess for app that loads .dlls but works’ great for virtual dos sessons.” EFFECTS:      the app shelled to and program execution here’ INPUTS:       the path and file name to the shelled app’ RETURNS:      Nothing’ CALLED FROM:’ AUTHOR DATE:  BruceJackson 10/95’***********************************************************************************************On Error GoTo ShellAndWait_ErrDim lngShellReturn As LongDim lngOpenProcess As LongDim lngExit As LongDim lngTimer As LongDim msg As StringConst NOWINDOW = 0Const WINDOWED = 1Dim r’***********************************************************************************************    lngShellReturn = Shell(strApp, WINDOW)                 ‘ OPENS WITH WINDOW, USE NOWINDOW FOR HIDDEND    lngTimer = Timer    lngOpenProcess = OpenProcess(PROCESS_QUERY_INFORMATION + PROCESS_TERMINATE, False, lngShellReturn)    Do    Call GetExitCodeProcess(lngOpenProcess, lngExit)    If lngExit = STILL_ACTIVE Then        If Timer – lngTimer > 120 Then                                         ‘ only wait for two minutes            msg = “An application has timed out!” & vbCrLf            msg = msg & “The path and file name to the batch file is: ” & strApp            ‘ can also use ExitProcess            r = TerminateProcess(lngOpenProcess, lngExit)                      ‘ FOR DOS APPS THAT DON’T CALL DLLS ONLY            MsgBox msg, 64, “Time Out Error”            Exit Sub        End If        If lngTimer > Timer Then lngTimer = Timer                              ‘ adjust after midnight        DoEvents    Else        Exit Sub    End If    Loop’***********************************************************************************************ShellAndWait_bye:    Exit SubShellAndWait_Err:    MsgBox “ERROR: ” & Error$ & Chr$(13) & Chr$(10) & “ERR#:  ” & Err, 64, “ShellAndWait”    GoTo ShellAndWait_byeEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: