Solved

Regarding Shell function

Posted on 1998-08-24
2
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month4 days, 23 hours left to enroll

635 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