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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

close a program within a program

Status
Not open for further replies.

blaise4714

Programmer
Feb 10, 2005
1
DE
hello
how can i close a Program within a Program in VC++? i run a Program with using CreateProcess commands! the program have to run for 5 mins. how can i close it? which methods can i use? please send me a example!
thank you!
 
Child process code:
Code:
void Ticker(int n = 10)
{
  if (n <= 0 || n > 100)
     n = 10;
  while (n--)
  {
     cout << " " << n;
     Sleep(1000); // in milliseconds
  }
  cout << endl;
}

int main(int argc, char* argv[])
{
    Ticker();
    return 0;
}
Parent process sceleton (start as new console app):
Code:
void Test()
{
  STARTUPINFO sinfo;

  memset(&sinfo,0,sizeof sinfo);
  sinfo.cb = sizeof sinfo;
  sinfo.lpTitle = "Child";

  PROCESS_INFORMATION pinfo;
  memset(&pinfo,0,sizeof pinfo);

  if (!CreateProcess("exechild.exe",
		0,
		0,
		0,
		FALSE,
		CREATE_NEW_CONSOLE,
		0,
		0,
		&sinfo,
		&pinfo))
  {
    cout << "*** Can\'t create process..." << endl;
    return;
  }
	
  cout << "I\'m waiting..." << endl;
  Sleep(5000); // watch child console window...

  cout << "Let\'s kill him..." << endl;
  if (TerminateProcess(pinfo.hProcess,1)) // brutal force
     cout << "RIP" << endl;
}

int main(int argc, char* argv[])
{
    Test();
    return 0;
}
Try this snippet then adopt it...
Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top