We help IT Professionals succeed at work.

regarding WinExec

adinarayanak
adinarayanak asked
on
  We are using WinExec(exename,0) to execute our external program. Our external program writes the result into some file. we need to access that file immediately after the WinExec call has been made.
   Control is coming back to caller program immediately even though the external program is still running.
    Is there any system call which waits for the completion of the external program ?
   
   
   
Comment
Watch Question

Commented:
Presumably the called program is a windows program as well.
In that case if you use the CreateProcess() API to launch the second application then you will specify a PROCESS_INFORMATION structure as part of the call. The handle to the new process will be placed in that structure and this can then be used in a GetExitCodeProcess() call. This will either return the exit code for the process if it has terminated or STILL_ACTIVE if it is still running.

WinExec and the CreateProcess APIs are all asynchronous - they spawn the new process and return immediately as you have already discovered.

Cheers - Gavin

Author

Commented:
Hi gavin,
   We were facing some memory related problems while using CreateProcess API. So we replaced CreateProcess API by WinExec. This Solved our memory related problems.
   More Information is at http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=progsoftgen&qid=20199633

Commented:
Oh well. There is a kludge way to do this if you can't use CreateProcess - it's ugly but does work, just put comments in your code to absolve you of any blame for using the technique in case any one else looks at the code!.

1) Delete the file that your called process is going to create prior to calling it. (Gets rid of any old copy)
2) Launch your called application using WinExec.
3) Then loop while checking for the existance of the file as created by the called application. Once it appears you could assume that your new application has done its work and exit the loop and continue processing.

Another way of doing it is, if your called program creates a window have it use a distinctive window title. You could then loop until the FindWindow() function fails to find the window - assuming that this means the called application has terminated and destroyed its window. You would have to be careful that your loop did not check for the window title before the called application had a chance to create its window otherwise you would get a false exit state.

Hope these ideas help.

Commented:
Workaround:

1. Create a named event in your main app.
2. Call the 2nd app.
3. Wait on the event.
---- 4. In the 2nd app, do whatever you need to do...
---- 5. Open the named event.
---- 6. Signal the event.
---- 7. Exit.
8. Jump for joy.


Better solution:

WinExec() is not the way to go.  Better to find out what was the problem with CreateProcess() and fix it.  Did you close the thread and process handles?

Commented:

Commented:
Yep, I vote for CreateProcess, too. After that you can call WaitForSingleObject(processInfo.hProcess) to wait for the termination of the newly started process. There should not be any memory problem (more than WinExec), if you close the handles correctly.

Regards, Madshi.

Commented:
adinarayanak, since the original CreateProcess() problem is solved, what do you want to do with this question?

Commented:
As others have said...use CreateProcess, then WaitForSingleObject, then close the handles.

Rob
As the others have said you should use CreateProcess, because you can monitor the thread of another program and wait for it's destruction,  when you said that you got memory problem, what were they?

If you would like to view the results of a second prog after using WinExec, you try executing a .bat file which can use commandline parameters.

Something to keep in mind, using WinExec in Win98/2000 blah the execution'WinExec' will remain in memory every time you execute the command.

I have created a class which uses CreateProcess and allows you to review results, check status of a new thread ....

If you would like it just ask.

later

_Allanon__

Author

Commented:
I have replaced WinExec call by CreateProcess. It is working fine.

Commented:
I'm just wondering why you accepted Allanon's answer? He was the *last* one talking about CreateProcess, not the first one. That's not fair. The points should have gone to newmang...

Author

Commented:
Yeah,I should have given the points to newmang. when Allanon came with proposed Solution I thought that I have to accept his answer.anyway Is there any way that we can transfer the points from Allanon's account to newmang ? Sorry for my mistake.

Commented:
>> I thought that I have to accept his answer

You don't _have_ to do anything, EE is a self-regulating community.  Of course, there are several guidelines of acceptable conduct.  One of them, which Allanon was not aware of, is not to lock questions but to leave comments instead.  An often cited reason is that locked question usually don't receive much attention from other experts.

Since this is Allanon's first answer on EE he can be forgiven for not knowing the ground rules but such errors should be corrected, not encouraged.

Another issue, unfortunately too common, is accepting a comment as an answer with a 'C' grade.  This is outright rude as most experts prefer not to get points at all than to have a 'C' grade on record.  (Yes, it's a pet peeve of mine since I got 2 such "presents" lately).

Commented:
Normally you could ask the customer service to transfer the points, but if you ask me: Leave it like it is for this question, since EE is currently short of employees. My comment was not meant to be unfriendly or whatever, I just hoped that you might do it better next time...   :-)
To all concern, I'm sorry for any probs I may have caused, when I clicked Answer I didn't know it would lock the question... so if you want the points to go to someone else I have no problems in doing so.

I'll also only do comments from now on...SORRY

Commented:
>> SORRY

No sweat, we all make mistakes.

Welcome to Experts-Exchange.

Commented:
Yep, welcome...   :-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.