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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SHellexecute

Status
Not open for further replies.

BSchaff

Programmer
Apr 10, 2001
4
US
I'm using the Shellexecute command to print an adobe file. I am executing a loop that modifies the file and then I print it. The problem I am running into is that after looping 3 or 4 times, the program errors out because I am trying to modify the file before the print file is spooled out. Is there any way I can make my program wait for the successful completion of the Shellexecute command.
 
I pulled this off of the MSDN CD's, I tried it and it should work for you to....

"In many situations, you may want to launch an application program and force the operating system to wait until that application has been terminated before allowing any other action to be performed. Unfortunately, you cannot use the ShellExecute function to do this!

You must use several other Windows API functions to launch and wait for the program to be terminated. The ExecuteAndWait method, shown below, can address this problem quite nicely. All you have to do is provide the full pathname and its required arguments to this method. The application you have just launched will retain the focus until it is subsequently terminated.

Public Sub ExecuteAndWait(cmdline As String)
Dim NameOfProc As PROCESS_INFORMATION
Dim NameStart As STARTUPINFO
Dim X As Long
NameStart.cb = Len(NameStart)
X = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
X = CloseHandle(NameOfProc.hProcess)
End Sub

As you can see from the code above, the ExecuteAndWait method uses several functions—CreateProcessA, WaitForSingleObject, and CloseHandle. We have already seen that a Windows application program can be executed by calling the ShellExecute function. You can also use the CreateProcessA function to launch applications and to force that application to retain the focus until it is terminated.

When an application program is launched, a process is created by the operating system. In other words, a process is an application program loaded into memory. The operating system gives each application, or process, a unique 32-bit identifier. When the process is terminated, the identifier becomes invalid—it is no longer in use.

Therefore, to execute a program and wait until it is terminated, you must first call the CreateProcessA function to load and execute your application program. Next, you call the WaitForSingleObject function which forces the operating system to wait until this application has been terminated. Finally, when the application has been terminated by the user, you call the CloseHandle function to release the application’s 32-bit identifier to the system pool. The bibliography at the end of this article sites several articles that discuss this concept in greater detail.

Bibliography
“How to Launch App Based on File Extension Using ShellExecute,” Q147807 (MSDN Library, Knowledge Base)

“Using Associations to Find and Start Applications” (MSDN Library, Platform, SDK, and DDK Documentation)

“How to Determine When a Shelled 32-bit Process Has Terminated,” Q129796 (MSDN Library, Knowledge Base)

Tip 168: Using the ShellExecute Function to Print Files (MSDN Library, Technical Articles)

WaitForSingleObject, QuickInfo.

“How to Launch a Win32 Application from Visual Basic”, Q129797 (MSDN Library, Knowledge Base)

“Tip 173: Launching Applications in Visual Basic” (MSDN Library, Technical Articles)"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top