Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Process ended??? 2

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I used this in VB4 or something a long time ago<br>------------------<br>&nbsp;&nbsp;&nbsp;&nbsp;Runit = &quot;c:\mtl\MTL Photo Grabber.exe &quot; & Me![ID]<br>&nbsp;&nbsp;&nbsp;&nbsp;x = Shell(Runit, 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;While GetModuleUsage(x) &gt; 0&nbsp;&nbsp;&nbsp;&nbsp;' Has Shelled program finished?<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z = DoEvents()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' If not, yield to Windows.<br>&nbsp;&nbsp;&nbsp;&nbsp;Wend<br>------------------------<br>The GetModuleUsage call no longer exists in VB6<br><br>Does anyone know of an updated API that might work?
 
Doug,<br>You can use WaitForSingleObject, however when I use it, I'm using it not to notify me that the program has ended as much as to lock the calling app (VB or Access) until it ends.&nbsp;&nbsp;If this will work for you, here are the specifics...<br>Declare Function OpenProcess Lib &quot;kernel32&quot; (ByVal dwAccess As Long, ByVal fInherit As Integer, ByVal hObject As Long) As Long<br>Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long<br>Declare Function WaitForSingleObject Lib &quot;kernel32&quot; (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long<br>Const SYNCHRONIZE = 1048576<br>Const INFINITE = &HFFFF<br>Dim ProcessId As Long, ProcessHandle As Long<br><br>ProcessId = Shell(PgmName , vbNormalFocus)<br>ProcessHandle = OpenProcess(SYNCHRONIZE,true,ProcessId)<br>WaitForSingleObject ProcessHandle,INFINITE <br>CloseHandle ProcessHandle '&lt;--code returns here when shelled pgm is done<br>--Jim<br>
 
I think the WaitForSystemObject Function will run into problems in NT, If you dont want the calling program to be frozen waiting for the Shelled program to end, you can use a loop like such<br>Adding to Jims code above<br><br>Const SYNCHRONIZE = 1048576<br>Const INFINITE = &HFFFF<br>Dim ProcessId As Long, ProcessHandle As Long, <b> rtnVal as Long </b><br><b>Const WAIT_TIMEOUT = &H102& </b><br><br>ProcessId = Shell(PgmName , vbNormalFocus)<br>ProcessHandle = OpenProcess(SYNCHRONIZE,true,ProcessId)<br><br><b>Do<br>&nbsp;&nbsp;&nbsp;rtnVal = WaitForSingleObject (ProcessHandle,0) <br>&nbsp;&nbsp;&nbsp;If rtnValue &lt;&gt; WAIT_TIMEOUT Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Do<br>&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;DoEvents<br>Loop While True </b><br>CloseHandle ProcessHandle <br><br>Useing the DoEvent is supposed to be a No No and I guess you could make some kind of State Machine with a timer. There are better examples in Dan Appleman book on the Win32 API, but I'm to lazy to look them up for you :]<br><br>Collin<br>
 
Thanks guys<br>I grabed both of your examples and put them in my goodie bag<br>I'm on a couple of other Hot projects now.<br>So I'll test it later.<br>We are barcoding everything in sight (documents that is)
 
Collin, I tried your method shelling to an MS-DOS batch file but ended up in a continuous loop waiting for the &quot;Finished - MyBatch.bat&quot; window to close. I had to do a SendMessage to the window containing &quot;Finished&quot; in order to jump out of the Do-Loop. Otherwise, it worked fine.<br><br>But what am I missing here? <p> <br><a href=mailto: > </a><br><a href= temporary Vorpalcom home page</a><br>Send me suggestions or comments on my current software project.
 
alt255,<br>The DOS box will stay open unless the Properties box has 'Close on Exit' checked, so even though the .bat file may be done, the acutal process, which was command.com, is still active.<br>--Jim
 
This has become an age old problem. The reality here is that the Shell() function does not return a handle to the newly created application window. It returns a value (a long) letting us know that the app was launched or not, thats all.<br><br>To have control over and interact with the executing application we need to create an instance of the application or get a handle to the window and monitor the messages from that window. <br><br>The question was, &quot;how do I know when the process has finished execution&quot;. <br><br>This one is subject to so many possabilities, Like what happens if the called application encounters a problem opening the file that has been passed, does this (error condition) constitute a 'finished execution' situation?<br><br>Writing this kind of code calls for a re-visting of the philosophy and thinking behind the COM Model. Althought the API Provides a lot we programmers are still saddled with creating workable solutions to real world problems<br><br><br> <p>Amiel<br><a href=mailto:amielzz@netscape.net>amielzz@netscape.net</a><br><a href= > </a><br>
 
Jim, I didn't want to walk around and check everybody's property boxes &lt;grin&gt;.<br>Amiel, thanks for confirming that I may not have been that far off-base: shell out, wait for evidence that the shell has finished (and, in my instance, send a message to close the &quot;Finished&quot; window), then use the results of the shell with <i>some</i> confidence (noting your point about error conditions and the limitless possiblities that can create a SNAFU).<br>And you're right about the API. If it provided every answer I wouldn't have to shell to batch files. <p> <br><a href=mailto: > </a><br><a href= temporary Vorpalcom home page</a><br>Send me suggestions or comments on my current software project.
 
What about doing the same only with the ShellExecute Command(since I cant tell Shell() what directory to go in etc) <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Alt255,<br>You can overcome the problem about the command window not closing by calling the command processor instead of the batch file. For example, instead of<br>Shell (&quot;myfile.bat&quot;)<br>use<br>Shell (&quot;cmd /c myfile.bat&quot;) on NT/W2K and<br>Shell (&quot;command /c myfile.bat&quot;) on Win95/98<br> <p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
this may help as an alternative, I copied it from MSDN<br><br><FONT FACE=monospace><br>HOWTO: 32-Bit App Can Determine When a Shelled Process Ends <br><br>--------------------------------------------------------------------------------<br>The information in this article applies to:<br><br>Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0 <br>Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0<br><br>--------------------------------------------------------------------------------<br><br><br>SUMMARY<br>Executing the Shell() function in a Visual Basic for Windows program starts another executable program asynchronously and returns control to the Visual Basic application. This shelled program continues to run independently of your application until the user closes it. <br><br>However, if your Visual Basic application needs to wait for the shelled process to terminate, you could use the Windows API to poll the status of the application, but this is not a very efficient technique. The example in this article demonstrates a better way. <br><br>A 16-bit application would use a completely different technique to accomplish the same effect. <br><br>For additional information, click the article number on the 16-bit process below to view the article on the 16-bit process in the Microsoft Knowledge Base: <br>Q96844 HOWTO: Determine When a Shelled Process Has Terminated <br><br><br><br>MORE INFORMATION<br>The Win32 API has integrated functionality that enables an application to wait until a shelled process has completed. To use these functions, you need a handle to the shelled process. The easiest way to achieve this is to use the CreateProcess() API function to launch your shelled program rather than Visual Basic's Shell() function. <br><br>Creating the Shelled Process<br>In a 32-bit application, you need to create an addressable process. To do this, use the CreateProcess() function to start your shelled application. The CreateProcess() function gives your program the process handle of the shelled process through one of its passed parameters. <br>Waiting for the Shelled Process to Terminate<br>Having used CreateProcess() to get a process handle, pass that handle to the WaitForSingleObject() function. This causes your Visual Basic application to suspend execution until the shelled process terminates. <br>Getting the Exit Code from the Shelled Application<br>It was common for a DOS application to return an exit code indicating the status of the completed application. While Windows provides other ways to convey the same information, some applications only provide exit codes. Passing the process handle to the GetExitCodeProcess() API allows you to retrieve this information. <br><br>Following are the steps necessary to build a Visual Basic for Windows program that uses the CreateProcess() function to execute the Windows Notepad (Notepad.exe) application. This code demonstrates how to use the Windows API CreateProcess() and WaitForSingleObject() functions to wait until a shelled process terminates before resuming execution. It also uses the GetExitCodeProcess() function to retrieve the exit code of the shelled process, if any. The syntax of the CreateProcess() function is extremely complicated, so in the example code, it is encapsulated into a function called ExecCmd(). ExecCmd() takes one parameter, the command line of the application to execute. <br>Step-by-Step Example<br>Start a new project in Visual Basic. Form1 is created by default. <br><br><br>Add the following code to the General Declarations section of Form1:<br><br><br>&nbsp;&nbsp;&nbsp;Private Type STARTUPINFO<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cb As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpReserved As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpDesktop As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpTitle As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwX As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwY As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwXSize As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwYSize As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwXCountChars As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwYCountChars As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwFillAttribute As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwFlags As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wShowWindow As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cbReserved2 As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpReserved2 As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hStdInput As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hStdOutput As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hStdError As Long<br>&nbsp;&nbsp;&nbsp;End Type<br><br>&nbsp;&nbsp;&nbsp;Private Type PROCESS_INFORMATION<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hProcess As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hThread As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwProcessID As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwThreadID As Long<br>&nbsp;&nbsp;&nbsp;End Type<br><br>&nbsp;&nbsp;&nbsp;Private Declare Function WaitForSingleObject Lib &quot;kernel32&quot; (ByVal _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hHandle As Long, ByVal dwMilliseconds As Long) As Long<br><br>&nbsp;&nbsp;&nbsp;Private Declare Function CreateProcessA Lib &quot;kernel32&quot; (ByVal _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpStartupInfo As STARTUPINFO, lpProcessInformation As _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PROCESS_INFORMATION) As Long<br><br>&nbsp;&nbsp;&nbsp;Private Declare Function CloseHandle Lib &quot;kernel32&quot; _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ByVal hObject As Long) As Long<br><br>&nbsp;&nbsp;&nbsp;Private Declare Function GetExitCodeProcess Lib &quot;kernel32&quot; _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ByVal hProcess As Long, lpExitCode As Long) As Long<br><br>&nbsp;&nbsp;&nbsp;Private Const NORMAL_PRIORITY_CLASS = &H20&<br>&nbsp;&nbsp;&nbsp;Private Const INFINITE = -1&<br><br>&nbsp;&nbsp;&nbsp;Public Function ExecCmd(cmdline$)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim proc As PROCESS_INFORMATION<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim start As STARTUPINFO<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Initialize the STARTUPINFO structure:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start.cb = Len(start)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Start the shelled application:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Wait for the shelled application to finish:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret& = WaitForSingleObject(proc.hProcess, INFINITE)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Call GetExitCodeProcess(proc.hProcess, ret&)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Call CloseHandle(proc.hThread)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Call CloseHandle(proc.hProcess)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExecCmd = ret&<br>&nbsp;&nbsp;&nbsp;End Function<br><br>&nbsp;&nbsp;&nbsp;Sub Form_Click()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim retval As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retval = ExecCmd(&quot;notepad.exe&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Process Finished, Exit Code &quot; & retval<br>&nbsp;&nbsp;&nbsp;End Sub <br>Press the F5 key to run the application.<br><br><br>Using the mouse, click the Form1 window. At this point the NotePad application is started.<br><br><br>Terminate NotePad. A MsgBox appears indicating termination of the NotePad application and an exit code of 0. To test this sample with an application that returns an exit code, implement the following Knowledge Base article and change the parameter passed to ExecCmd to &quot;project1.exe&quot;: <br>Q178357 HOWTO: Set an Error Level from a Visual Basic Application <br>NOTE: The MsgBox statement following the ExecCmd() function is not executed because the WaitForSingleObject() function prevents it. The message box does not appear until Notepad is closed when the user chooses Exit from Notepad's File menu (ALT, F, X).<br><br><br><br>Additional query words: GetModuleUsage <br><br>Keywords : kbVBp kbVBp400 kbVBp600 kbDSupport <br>Version : WINDOWS:4.0,6.0 <br>Platform : WINDOWS <br>Issue type : kbhowto <br>Technology : <br>&nbsp;<br><br><br>Last Reviewed: October 13, 1999<br>© 2000 Microsoft Corporation. All rights reserved. Terms of Use.<br></font><br><br>URL is <A HREF=" TARGET="_new"> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Karl - this is an excellent answer - should be a FAQ <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top