Solved

Regarding Shell function

Posted on 1998-08-24
2
274 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating VB6 Function 19 59
Run code from text file in vb 1 64
vb6 connector to SQL Server 2 37
Sorting multiple rows and columns, and count duplicates in Excel 2013 4 79
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

776 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