Solved

Starting programs

Posted on 1999-01-23
29
218 Views
Last Modified: 2010-04-02
I'd like to know how to start a program and have my code not continue until the called program exits. When I use CreateProcess or WinExec they spawn threads and execute along side my app. Any suggestions?
0
Comment
Question by:forloop
  • 10
  • 9
  • 7
  • +3
29 Comments
 
LVL 1

Expert Comment

by:arbitrary
ID: 1184929
you can use a While loop on the HANDLE you receive from CreateProcess with a sleep(dwMSeconds) inside.

0
 
LVL 86

Expert Comment

by:jkr
ID: 1184930
>>you can use a While loop on the HANDLE you receive from
>>CreateProcess with a sleep(dwMSeconds) inside.

OUCH OUCH OUCH!!!

The proper way is to use the process handle returned in the PROCESS_INFORMATION structure filled in by 'CreateProcess()' in a call to 'WaitForSingleObject()'...

arbitrary, what you're suggesting means _polling_ for an object's state in a multiprocess environment - that's not what the whole thing was designed for!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184931
I agree with jkr, except I don't think "ouch" is adequate.
0
 

Author Comment

by:forloop
ID: 1184932
jkr, you deserve the points here if you want them.
0
 

Author Comment

by:forloop
ID: 1184933
When I use the WaitForSingleObject w/ INFINITE as the wait value, the installation program I'm calling (ISYS) locks up at the point where it creates it's program groups. If I kill my program, ISYS continues to install. Here's my code:

// BEGIN CODE

STARTUPINFO infoStart;
PROCESS_INFORMATION infoProcess;
CString Temp;

memset(&infoStart,0,sizeof(infoStart));
infoStart.cb=sizeof(STARTUPINFO);
Temp.Format("%s\\isysrun\\setup.exe",m_Drive);
CreateProcess(NULL,(char *)(LPCTSTR)Temp,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&infoStart,&infoProcess);
WaitForSingleObject(infoProcess.hProcess,INFINITE);

// END CODE

I can set the wait value to something other than INFINITE, but that defeats the purpose (Waiting for the setup to end before continuing). I'd rather use a Sleep statement.

This could just be an issue with this particular program, but I thought I'd bring out the code and see if anyone notices any problems.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184934
I don't see any problems there.  What is the "context" of this code?  i.e. when is it called.  Could the OS be locked up waiting for this to return ion some way?  For example, if you were to do this inside the DLLMain procedure of a DLL it would cause a lock up.

Also note that the two handles returned in InfoProcess must be closed eventually.  
0
 
LVL 86

Expert Comment

by:jkr
ID: 1184935
I agree with nietod - this code certainly doesn't cause the problem - maybe that the setup program you're calling is opening a dialog in the background?
Another thing - perhaps you should set the working directory of the setup program to the one it is started from. This would prevent errors that result of the program being unable to locate files it searches for.
Try:
/ BEGIN CODE

STARTUPINFO infoStart;
PROCESS_INFORMATION infoProcess;
CString Temp;
CString strDir;

memset(&infoStart,0,sizeof(infoStart));
infoStart.cb=sizeof(STARTUPINFO);
Temp.Format("%s\\isysrun\\setup.exe",m_Drive);
strDir.Format("%s\\isysrun\\",m_Drive);
CreateProcess(NULL,(char *)(LPCTSTR)Temp,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,(char*) (LPCTSTR) strDir,&infoStart,&infoProcess);
WaitForSingleObject(infoProcess.hProcess,INFINITE);

// END CODE

BTW:
nietod - 'ouch' may not be adequate, but that's simply how i felt ;-)
0
 
LVL 86

Expert Comment

by:jkr
ID: 1184936
>>jkr, you deserve the points here if you want them.

Thanks, but let's get this app running first ;-)
0
 

Author Comment

by:forloop
ID: 1184937
The code is ran in an MFC executable (not a DLL). The CreateProcess call is fine (with or without a working dir). I can change the word INFINITE to 30000 in the above code and ISYS never locks up, but after 30000 ms my program begins executing more code. ISYS only freezes when I set it to INFINITE. It must just be some weird problem with this particular program.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184938
??? When you specify INFINITE, Is the program that you are using CreateProcess() for running?  If so does it end running?  This use of WaitForSingleObject() is to hold up your program until the program it starts is done.  So does that other program get done?  
0
 

Expert Comment

by:erick1217
ID: 1184939
try one of the _wspawn function with the _P_WAIT argument
0
 

Author Comment

by:forloop
ID: 1184940
nietod: yes.. the called program is running.. in fact it and my program show "not responding" in the task manager.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1184941
'Not responding' is because the task manager uses a call to 'SendMessageTimeout()' to determine whether a program can handle requests within a predefined perion (5s). As your program is blocked at 'WaitForSingleObject()', this isn't alarming at all - that's normal ;-)
I'd suggest starting a simple test application that does nothig but calling 'Sleep()'...
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184942
That expains why the waiting program is not responding.  Why isn' the applicatin that is started, responding?  And most importantly, does this program ever get done?  Your first program SHOULD be locked up until the program its runs gets finished.  So if the program it runs doesn't finish, it will stary locked up forever.   (And remember--this is what you say you want.  It is not a bad thing, as long as that other program finishes quickly.)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:jkr
ID: 1184943
Oops, i didn't realize that the spawned program is 'not responding', too - does it run when started 'standalone'?
Note that when this program never finishes, your application will block at 'WaitForSingleObject()' at the predefined timeout...
0
 

Author Comment

by:forloop
ID: 1184944
The called program runs totally fine if:

1. Called standalone
2. Called by CreateProcess and WaitForSingleObject is set it a value other than INFINITE.

Otherwise, if any thing is waiting (INFINITELY) for the task to finish, the program will freeze at a particular point.

This goes for a simple FindWindow loop on the called process.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1184945
Which program is freezing?  The on that did the CreateProcess or the other one?  

These two processes don't have to communicate in any way do they?  They don't send messages back and forth or share mutexes or other OS objects do they?
0
 

Author Comment

by:forloop
ID: 1184946
The program being called by CreateProcess from my program. My program is basically a simple installer that calls another installer when required. They do not communicate with each other at all.. My installer basically copies a few files, calls CreateProcess on the 2nd installer (the one that freezes) and then runs the installed program after its done.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184947
Is it freezing at the same place every time or different places?  If the same place, what is it doing there?
0
 

Author Comment

by:forloop
ID: 1184948
same place. it has a message box up that's saying "creating program groups"
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184949
It sounds like this program is not one you wrote, Is that the case?  If so, that makes things considerably tougher.  If you did write it, what place IN THE SOURCE CODE does it stop at.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1184950
Hmm - just an (intermediate) idea to check for the reasons:

try

while ( WAIT_TIMEOUT == WaitForSingleObject(infoProcess.hProcess,10000));

This is ugly and shouldn't be the final solution, and to be honest, i'm stumped if this works...
0
 

Expert Comment

by:superole2
ID: 1184951
Have you tried using the "system()" fuction?

Syntax

#include <stdlib.h>
int system(const char *command);

Description

Issues an operating system command.
system invokes the operating system command processor to execute an operating system command, batch
file, or other program named by the string command, from inside an executing C program.
To be located and executed, the program must be in the current directory or in one of the directories listed in
the PATH string in the environment.
The COMSPEC environment variable is used to find the command processor program file, so that file need not
be in the current directory.

Return Value

If command is a NULL pointer, system returns nonzero if a command processor is available.
If command is not a NULL pointer, system returns 0 if the command processor was successfully started.
If an error occurred, a -1 is returned and errno is set to one of the following:

ENOENT      Path or file function not found
ENOEXEC      Exec format error
ENOMEM      Not enough memory


0
 
LVL 86

Expert Comment

by:jkr
ID: 1184952
superole2 - did you read the question history?????
You're far away from that what we already discussed, and your answer isn't matching the requirements at all :-(

faster - BTW, what's going on? ;-)
0
 

Author Comment

by:forloop
ID: 1184953
Well, it's just an isolated thing.. I've called many other programs using the techniques you guys have suggested and they all work fine. This particular app does like it.. So.. Thanks for your help everyone and I'll have to live with my work-around on this project.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1184954
So - do you think i should lock the Q? ;-)
0
 

Author Comment

by:forloop
ID: 1184955
yeah.. lock it.
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1184956
Thanx ;-) !
(let's see that we get that DCOM thing running <s>
0
 

Author Comment

by:forloop
ID: 1184957
Thanks again for the help on this one!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

22 Experts available now in Live!

Get 1:1 Help Now