Solved

Shell function

Posted on 2002-04-24
8
186 Views
Last Modified: 2010-05-02
I'm calling an application through a shell statement within a loop, but I need to wait for the previous incarnation of the application to finish before the next one is called.  I've done this before, but can't remember how to do it.  I know it's a fairly easy process once I get the syntax.

Anyone know how?

Luke
0
Comment
Question by:Luke_Stephens
8 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 6965954
Here is an example of using WaitForSingleObject api call to do this sort of thing. You just need to supply values for the "Filename" and "FileTitle" in the SuperShell call.

Const INFINITE = &HFFFF
Const STARTF_USESHOWWINDOW = &H1
Private Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
End Enum
Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type
Private 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 Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type
Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type
Private Enum enPriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
End Enum
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
    Dim pclass As Long
    Dim sinfo As STARTUPINFO
    Dim pinfo As PROCESS_INFORMATION
    'Not used, but needed
    Dim sec1 As SECURITY_ATTRIBUTES
    Dim sec2 As SECURITY_ATTRIBUTES
    'Set the structure size
    sec1.nLength = Len(sec1)
    sec2.nLength = Len(sec2)
    sinfo.cb = Len(sinfo)
    'Set the flags
    sinfo.dwFlags = STARTF_USESHOWWINDOW
    'Set the window's startup position
    sinfo.wShowWindow = start_size
    'Set the priority class
    pclass = Priority_Class
    'Start the program
    If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
    0&, WorkDir, sinfo, pinfo) Then
        'Wait
        WaitForSingleObject pinfo.hProcess, dwMilliseconds
        SuperShell = True
    Else
        SuperShell = False
    End If
End Function

Private Sub Command1_Click()
    'Execute the program
    SuperShell filename, Left$(filename, Len(filename) - Len(FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
    End
End Sub

0
 
LVL 44

Expert Comment

by:bruintje
ID: 6966442
Hi Luke_Stephens,

another way using the exitcode of a shelled process

http://www.mvps.org/vbnet/index.html?http://www.mvps.org/vbnet/faq/main/faqcde.htm

:O)Bruintje
0
 

Expert Comment

by:Death-Speak
ID: 6966925
If App.PrevInstance Then
     'Do Nothing
Else
    'Do Something
End If
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Expert Comment

by:wileecoy
ID: 6967069
Luke_Stephens,

One thing that you should consider is whether or not there is a need for responses from your calling application.  If your shelled process will take a while, it may leave an 'unclean' look if you use the WaitForSingleObject method (TimCottee method, and one link that bruintje provided).

On the other hand, it is more efficient than the GetExitCodeProcess because of the loop that is necessary (also a link at mvps provided by bruintje).

Just my opinion.

Wileecoy.
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 6967070
Luke_Stephens,

One thing that you should consider is whether or not there is a need for responses from your calling application.  If your shelled process will take a while, it may leave an 'unclean' look if you use the WaitForSingleObject method (TimCottee method, and one link that bruintje provided).

On the other hand, it is more efficient than the GetExitCodeProcess because of the loop that is necessary (also a link at mvps provided by bruintje).

Just my opinion.

Wileecoy.
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 6967072
Sorry about the double post.
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6970312
Hi!

Here's a file for you over the net:

Download...
http://www.vb-helper.com/HowTo/runwait.zip
Description: Start another program and wait until it finishes (2K)

Use this to wait for an instance of the shelled program to load completely.  You could also use the DoEvents in a loop to 'wait'.

That's it!

glass cookie : 0


0
 

Author Comment

by:Luke_Stephens
ID: 6970558
Sorry to have let this question drag on people, but the final code I used was a kind of amalgamation of everyone's input.  Tim Cottee's original response was the closest to the final result though, hence he gets the points.  

If anyone's interested, here's the code I ended up using.  It's pretty much all downloaded from somewhere, so I can't take the credit!

Cheers everyone.

Luke



---------------------------------------------------------

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

   Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
      lpApplicationName 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

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

   Private Declare Function GetExitCodeProcess Lib "kernel32" _
      (ByVal hProcess As Long, lpExitCode As Long) As Long

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

   Public Function ExecCmd(cmdline$)
      Dim proc As PROCESS_INFORMATION
      Dim start As STARTUPINFO

      ' Initialize the STARTUPINFO structure:
      start.cb = Len(start)

      ' Start the shelled application:
      ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)


      ' Wait for the shelled application to finish:
         ret& = WaitForSingleObject(proc.hProcess, INFINITE)
         Call GetExitCodeProcess(proc.hProcess, ret&)
         Call CloseHandle(proc.hThread)
         Call CloseHandle(proc.hProcess)
         ExecCmd = ret&
   End Function

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

832 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