Solved

Regarding Shell function

Posted on 1998-08-24
2
272 Views
Last Modified: 2010-04-30
As explained in VB Help, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed.

But I would like to finish the Shell function before execute the next statement.  Currently I am using Call statement to execute Shell function, will this overcome the above problem?
0
Comment
Question by:cwchaw
2 Comments
 
LVL 1

Accepted Solution

by:
jsuper earned 50 total points
ID: 1430806
I'm not sure if the above method will solve your problem, but there are a number of ways to call a program and wait for it to complete.  I pulled the following code from the Visual Basic Developers Resource Centre.  (http://www.mvps.org/vbnet/)  There are some other methods at the site you can try as well, but this should work fine...

Add the following code to a code module...

Option Explicit

Public Const NORMAL_PRIORITY_CLASS = &H20&Public Const INFINITE = -1&

Public 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 Long
   cbReserved2 As Long
   lpReserved2 As Long
   hStdInput As Long
   hStdOutput As Long
   hStdError As Long
End Type

Public Type PROCESS_INFORMATION
   hProcess As Long
   hThread As Long
   dwProcessId As Long
   dwThreadID As Long
End Type

'Note: the number of line continuations in
'the next declare exceed VBs maximum; the API is  
'presented this way here only for clarity.
Public Declare Function CreateProcessA Lib "kernel32" _
   (ByVal lpAppName 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

Public Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long,
     ByVal dwMilliseconds As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

And add this code to whatever form or module you will be using to shell out to other applications.  In this case, replace the call to the notepad.exe with whatever program you are trying to call...  Also, the example assumes you are using a command key to trigger the calling of the external program.  Just move the
RunProcess command to whatever subroutine you desire...

Option Explicit

Private Sub Command1_Click()

    RunProcess "c:\windows\notepad.exe"

End Sub


Private Sub RunProcess (cmdline As String)

    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim r As Long

   
  'Initialize the STARTUPINFO structure by
  'passing to start the size of the STARTUPINFO
  'type. Setting the  .cb member is the only
  'item of the structure needed to launch the program
   start.cb = Len(start)
   
  'Start the application
   Call CreateProcessA(0&, cmdline, 0&, 0&, 1&, _
                       NORMAL_PRIORITY_CLASS, 0&, 0&, _
                       start, proc)
   
  'Wait for the application to finish
   Call WaitForSingleObject(proc.hProcess, INFINITE)
   
  'Close the handle to the process
   Call CloseHandle(proc.hProcess)

   MsgBox "The Shelled process " & cmdline & " has ended."

End Sub



Give that a shot, hopefully it will perform as you expected.   Good Luck.
0
 
LVL 1

Expert Comment

by:disha
ID: 1430807
Call will not solve the problem. The way is to poll for that application to finish after shell command. This can be done using getmoduleusage API call....and checking if u'r application (started with shell) is still running..
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now