Solved

Shell function

Posted on 2002-04-24
8
177 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now