Solved

Shell function

Posted on 2002-04-24
8
195 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
[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
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

615 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