• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Waiting till called program finishes

I am writing a C++ program which will call another program using CreateProcess function. I want to halt my first program till the called program finishes and want to execute the next line after that.

How can I do that?

Eg.

CreateProcess("NotePad.exe");
MessageBox("NotePad Closed.");

As soon as Notepad launched my program will be inactive and when the user closes the notepad I want to run the messagebox code?
0
ysivaram
Asked:
ysivaram
  • 6
  • 5
  • 4
  • +1
1 Solution
 
jhanceCommented:
You have to use the information returned by CreateProcess() in the lpProcessInformation struct.  

typedef struct _PROCESS_INFORMATION {
    HANDLE hProcess;
    HANDLE hThread;
    DWORD dwProcessId;
    DWORD dwThreadId;
} PROCESS_INFORMATION;

the hProcess member of the struct give you the HANDLE of the newly created process.


Now you can use the WaitForSingleObject()
function to keep your creating application waiting until the new process terminates.
0
 
nietodCommented:
That seems like that should be an answer.

(Assuming of course jhance isn't joking. :-) )
0
 
behjatCommented:
Note to obtain full information of what is the signiture to CreateProcess and its parameters see MSDN
what you have shown is not correct
any how
This is the

BOOL CreateProcess(  LPCTSTR lpApplicationName,
                         // pointer to name of executable module
  LPTSTR lpCommandLine,  // pointer to command line string
  LPSECURITY_ATTRIBUTES lpProcessAttributes,  // process security attributes
  LPSECURITY_ATTRIBUTES lpThreadAttributes,   // thread security attributes
  BOOL bInheritHandles,  // handle inheritance flag
  DWORD dwCreationFlags, // creation flags
  LPVOID lpEnvironment,  // pointer to new environment block
  LPCTSTR lpCurrentDirectory,   // pointer to current directory name
  LPSTARTUPINFO lpStartupInfo,  // pointer to STARTUPINFO
  LPPROCESS_INFORMATION lpProcessInformation  // pointer to PROCESS_INFORMATION
);

using CreateProcess
then
get the handle of excuted process from
"hProcesInfo" of CreateProcess parameter
that you called
which is a structure with members as follows

typedef struct _PROCESS_INFORMATION { HANDLE hProcess;
HANDLE hThread;    
DWORD dwProcessId;    
DWORD dwThreadId;
} PROCESS_INFORMATION

that is "hProcess"

now use this pic of code

bool  bFlag
DWORD *dwExitCode;
do
{
 bFlag = GetExitCodeProcess(hProcesInfo->hProcess,dwExitCode);
}while(fFlag == true && *dwExitCode != STILL_ACTIVE);

if (fFlag == false)
{
 //Use GetLastError to Obtain the ERROR CODE.
}
else
{
 // Your code that you want to excute after goes here
}

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
behjatCommented:
Note: to obtain full information of what is the signiture to CreateProcess and its parameters see MSDN
what you have shown is not correct
any how
This is the

BOOL CreateProcess(  LPCTSTR lpApplicationName,
                         // pointer to name of executable module
  LPTSTR lpCommandLine,  // pointer to command line string
  LPSECURITY_ATTRIBUTES lpProcessAttributes,  // process security attributes
  LPSECURITY_ATTRIBUTES lpThreadAttributes,   // thread security attributes
  BOOL bInheritHandles,  // handle inheritance flag
  DWORD dwCreationFlags, // creation flags
  LPVOID lpEnvironment,  // pointer to new environment block
  LPCTSTR lpCurrentDirectory,   // pointer to current directory name
  LPSTARTUPINFO lpStartupInfo,  // pointer to STARTUPINFO
  LPPROCESS_INFORMATION lpProcessInformation  // pointer to PROCESS_INFORMATION
);

using CreateProcess
then
get the handle of excuted process from
"hProcesInfo" of CreateProcess parameter
that you called
which is a structure with members as follows

typedef struct _PROCESS_INFORMATION { HANDLE hProcess;
HANDLE hThread;      
DWORD dwProcessId;      
DWORD dwThreadId;
} PROCESS_INFORMATION

that is "hProcess"

now use this pice of code
************************
bool  bFlag
DWORD *dwExitCode;
do
{
 bFlag = GetExitCodeProcess(hProcesInfo->hProcess,dwExitCode);
}while(fFlag == true && *dwExitCode != STILL_ACTIVE);

if (fFlag == false)
{
 //Use GetLastError to Obtain the ERROR CODE.
}
else
{
 // Your code that you want to excute after goes here
}
*******************


 

 
0
 
nietodCommented:
behjat, did you read the question history?  

The question was already answered and with a better answer--Yours wastes CPU time.
0
 
jhanceCommented:
No I'm not joking this time but unlike that other question, this one does have an answer.

It is clear, however, that behjat either cannot read or is playing answer stealing tricks....

It seems like whenever I try to be courteous to a questioner by not locking the question some jerk like this stomps on the answer with a bogus or duplicate answer.....
0
 
nietodCommented:
>> I try to be courteous to a questioner by
>> not locking the question some jerk like
>> this stomps on the answer with a bogus
>> or duplicate answe
Not allways.  Only about 75% of the time.  So if you keep doing it, who do you blame?
0
 
jhanceCommented:
I'm about to give up on Experts Exchange "courtesy".  There are too many "piss-ant" expert "wannabes" anymore.
0
 
nietodCommented:
yeah, and people that post bogus answers too!  :-)
0
 
ysivaramAuthor Commented:
Yes I prefer Jhance answer.
0
 
ysivaramAuthor Commented:
Thank you jhance.

It works fine.
0
 
behjatCommented:
I do not like time wasters.

I advise you strongly:
firstely
go to school and then read msdn

what I give you as code came from workink soiftware.

Just matter of intrest what do you want to do with your accepted answer
for your information CreateProcess is a nonblocking function
0
 
ysivaramAuthor Commented:
nAppHandle = RunProcess(strcat(cLocation, "Notepad.exe"));
      DWORD dRetVal = WaitForSingleObject(nAppHandle, INFINITE);

These two linew as Jhance suggested works fine. Do you see anything wrong there?

In school if they teach us to use pages of code instead of two lines do we need to follow that?
0
 
ysivaramAuthor Commented:
Sorry this is the RunProcess function

HANDLE RunProcess(char* cProcess)
{
      STARTUPINFO si;
      PROCESS_INFORMATION pi;
      ZeroMemory( &si, sizeof(STARTUPINFO) );
      ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) );
      si.cb=sizeof( STARTUPINFO );
      si.dwFlags = STARTF_USESHOWWINDOW;
      si.wShowWindow = SW_MAXIMIZE;
      CreateProcess(cProcess, "", NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi );
      return pi.hProcess;
}
0
 
behjatCommented:
to nietod
When I saw your question the answer from (jhence) was already there

my assumption was, you were not familiar with synchronization
since you did not accepted the answer thus, I gave you the pice of code that came from bug free software which has been running from at list last 3-years

some idites like the one above does not want to accept there are othere pepole who may know these things as well this is not called stealing the answer.
0
 
nietodCommented:
>> you were not familiar with synchronization
I'm not sure what your answer has anything to do with syncrhonization.  The best way to synch would be with one of the wait for object functions.

My point was that jhance had posted the answer in terms of content, if not as an actual answer, and that he should post it as an answer.  The line about the joke was because jhance has just posted a bogus function that was to good to be true as an "answer" (in a comment) to a question, and I fell for it.

>> pice of code that came from bug free software
It may work, but it is very very innefficient. There are often many ways to solve a problem, but often one or two ways are far superior to the others.

>> this is not called stealing the answer.
Around here, this is called stealing.  Soem experts post their answers in comments, sometimes because they may be unsure of them and sometimes because they don't want to seem greedy.  But if you see a question that has been answered in terms of content, regardless of whethor or not it has been actually locked, I recommend you treat it as answered.  If you have something to add post it is a comment.  There is no rule currently enforcing this, but if you don't you will have to put up with a lot of "flak" like this.  Its not in your interest.  EE is called a knowledge _community_ and it really works like a community.  The good experts tend to work together to solve problems and to see that things are handled fairly.  Someday you may run in to problems where an client rejects your answer unfairly or where another expert "steals" your points.  You will want support from other experts then...



ysivaram,
   CreateProcess()   opens two handles for you, the process handle, which you return and the thread handle.  It is very important that these handles both be closed when they are no longer needed.  As long as they are open the OS maintains a lot of resources about the process that consume a lot of memory and possibly CPU time at times.  This continues even after the process ends, so you must close these handles.  As the code stands at the moment, you should close the thread handle inside RunProcess() and let the caller close the process handle.
0
 
ysivaramAuthor Commented:
nietod

Thanks for your comment I will include that code as well.
0
 
ysivaramAuthor Commented:
nietod

Thanks for your comment I will include that code as well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now