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!

Calling the ShellExecute command 1

Status
Not open for further replies.

psnead

Programmer
Jul 5, 2000
41
DK
I am using the ShellExecute command to run an executable file several times using a while loop. However, many times the executable isn't finished running before the next one is called. How can I keep this from happening?
 
You can write your own ShellExecute with CreateProcess and synchronize the call with WaitForSingleObjectEx on the process handle. The attached sample procedure uses this technique:
[tt]
BOOL SpawnProc(int CreationFlags, BOOL Sync, int argc, char *argv[])
{
// va_list argptr;
// char *parm = Param, CmdLine[256];
char CmdLine[256];
int i = 0;
PROCESS_INFORMATION ProcessInformation;
// LPSECURITY_ATTRIBUTES lpProcessAttributes, lpThreadAttributes;
STARTUPINFO si;

ZeroMemory (&si, sizeof(STARTUPINFO));
si.cb = sizeof(si);
si.wShowWindow = SW_MINIMIZE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

CmdLine[0] = '\0';
// va_start( argptr, Param ); /* Initialize variable arguments. */
for (i = 1; argv != NULL && i < 256; i++) {
strcat (CmdLine, &quot;\&quot;&quot;);
strcat (CmdLine, argv);
strcat (CmdLine, &quot;\&quot; &quot;);
// parm = va_arg( argptr, char *);
}
CmdLine = '\0';
printf (&quot;Exe: %s - CmdLine: %s\n&quot;, argv[0], CmdLine);
// va_end( argptr ); /* Reset variable arguments. */

// LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_STARTING_SERVICE, &quot;CreateProcess &quot;, CmdLine);
if (CreateProcess(
argv[0], // pointer to name of executable module
CmdLine, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
FALSE, // handle inheritance flag
CreationFlags, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&si, // pointer to STARTUPINFO
&ProcessInformation // pointer to PROCESS_INFORMATION
))
{
if (Sync) {
::WaitForSingleObjectEx(
ProcessInformation.hProcess, // handle of object to wait for
INFINITE, // time-out interval in milliseconds
FALSE // return to execute I/O completion routine if TRUE
);
}
return TRUE;
} else {
// LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_PROC_START_FAIL, CmdLine);
return FALSE;
};
}

[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top