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

Halt until ShellExecuted app is done? 1

Status
Not open for further replies.

BeanDog

Programmer
Jul 15, 2000
60
US
I have an application that needs to run two different
executables, one after the other. When I do 2
ShellExecutes, I get both running at once. I need to make
sure the first finishes before the second begins. Is
there a blocking version of ShellExecuteEx?



~BenDilts( void );
 
Just Shell() Craig, mailto:sander@cogeco.ca

"Procrastination is the art of keeping up with yesterday."

I hope my post was helpful!!!
 
If the hProcess member of the SHELLEXECUTEINFO structure is not NULL after ShellExecuteEx returns, you could use WaitForSingleObject to suspend the calling program until the called program is finished.

The code needed would be something like this:

SHELLEXECUTEINFO shexinf;

shexinf.fMask = SEE_MASK_NOCLOSEPROCESS; // Required to get back the process handle

<Fill the rest of shexinf>

if ( ShellExecuteEx ( &shexinf ))
{ if ( shexinf.hProcess != NULL )
{ WaitForSingleObject ( shexinf.hProcess, INFINITE );
CloseHandle ( shexinf.hProcess ); } }



Marcel



 
I am not using VB. I need a WinAPI way to do this.
 
Also Craig, as has been previously pointed out, Shell is asynchronous.
 
#include <windows.h>
#include <stdio.h>

BOOL ShellExeWait( LPSHELLEXECUTEINFO shellProg,
char* sPath )
{

shellProg->cbSize=sizeof( SHELLEXECUTEINFO );
shellProg->fMask =SEE_MASK_NOCLOSEPROCESS;
shellProg->lpVerb=&quot;open&quot;;
shellProg->lpFile=sPath;
shellProg->nShow =SW_SHOWNORMAL;

if( ShellExecuteEx( shellProg ) )
WaitForSingleObject( shellProg->hProcess,INFINITE );
else
return FALSE;

return TRUE;
}



int main( void )
{
SHELLEXECUTEINFO shellProg={ 0 };

printf( &quot;Notepad is now executing...\n&quot; );
ShellExeWait( &shellProg,&quot;notepad.exe&quot; );
printf( &quot;Calculator is now executing...\n&quot; );
ShellExeWait( &shellProg,&quot;calc.exe&quot; );

printf( &quot;<DONE>\n&quot; );

return 0;
} Mike L.G.
mlg400@linuxmail.org
 
Relevant structures and declares are :

Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESIZE = &H2
Private Const STARTF_USEPOSITION = &H4
Private Const STARTF_USECOUNTCHARS = &H8
Private Const STARTF_USEFILLATTRIBUTE = &H10
Private Const STARTF_RUNFULLSCREEN = &H20
Private Const STARTF_FORCEONFEEDBACK = &H40
Private Const STARTF_FORCEOFFFEEDBACK = &H80
Private Const STARTF_USESTDHANDLES = &H100

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 PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

Private Declare Function CreateProcess Lib &quot;kernel32&quot; _
Alias &quot;CreateProcessA&quot; _
(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 &quot;kernel32&quot; _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

There's lots on MSDN and elsewhere about using these.

You can start your app with :

Dim l_uStartInfo As STARTUPINFO
Dim l_uProcInfo As PROCESS_INFORMATION
Dim l_uSecurity As SECURITY_ATTRIBUTES
Dim l_lReturnValue As Long

With l_uStartInfo
.lpDesktop = vbNullString
.lpTitle = vbNullString
.lpReserved = vbNullString
.cbReserved2 = 0
.dwX = 0
.dwY = 0
.dwXSize = <a value in pixels>
.dwYSize = <a value in pixels>
.dwFillAttribute = 0
.dwFlags = STARTF_USESIZE + STARTF_USEPOSITION
.cb = Len(l_uStartInfo)
End With

l_uSecurity.nLength = Len(l_uSecurity)
l_lReturnValue = CreateProcess(vbNullString, <CommandString>, l_uSecurity, l_uSecurity, 0, 0, 0, vbNullString, l_uStartInfo, l_uProcInfo)

l_lReturnValue = WaitForSingleObject(l_uProcInfo.hProcess, 1000) ' Wait up to 5 secs for process to start

This always returns a process id and handle.

If the other apps are yours, you could get them to send you a message on exiting. Otherwise, find the main window of the app using EnumWindows and then poll it (eg with SendMessage) to detect when it disappears!
 
i thought it was Shell that would wait until the process was complete. Or maybe I'm thinking of SendMessage vs. SendMessageEX. Craig, mailto:sander@cogeco.ca

&quot;Procrastination is the art of keeping up with yesterday.&quot;

I hope my post was helpful!!!
 
Hey, I happen to need an app that does the same thing.
My problem is I don't really know how to use VB6 well
enough to substitute my own file names.

Can anyone help? Maybe annotate where I insert my file
names or maybe something with a user-modifiable INI?

I could probably bumble my way through the VB6 compiler
if someone has the code handy.

Damn I hate being this ignorant when all the info is
probably right in front of me. lol

Maj

majestyk@knac.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top