Link to home
Start Free TrialLog in
Avatar of adinarayanak
adinarayanak

asked on

regarding WinExec

  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 ?
   
   
   
Avatar of newmang
newmang

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
Avatar of adinarayanak

ASKER

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 https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=progsoftgen&qid=20199633
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.
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?
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.
adinarayanak, since the original CreateProcess() problem is solved, what do you want to do with this question?
As others have said...use CreateProcess, then WaitForSingleObject, then close the handles.

Rob
ASKER CERTIFIED SOLUTION
Avatar of _Allanon__
_Allanon__

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have replaced WinExec call by CreateProcess. It is working fine.
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...
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.
>> 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).
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
>> SORRY

No sweat, we all make mistakes.

Welcome to Experts-Exchange.
Yep, welcome...   :-)