Solved

Regarding Shell function

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 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

26 Experts available now in Live!

Get 1:1 Help Now