473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

stop the c++ program and start another one

Hi all,

OS: Windows XP
C++: Microsoft Visual C++ 2005

I am wondering if it is possible to kill the C++ program after calling another program (i.e. .bat). I did a simple test and check the "Windows Task Manager". After the exe run the .bat the exe is still there until the .bat finished running. Is that possible to kill the .exe after the .bat is called.

Expand|Select|Wrap|Line Numbers
  1. // Filename:test.cpp
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6.     std::cout << "Calling run.bat";
  7.     std::cout << "\n";
  8.     system ("stage\\run.bat");
  9.  
  10.     return 0;
  11. }
Apr 18 '07 #1
6 3472
JosAH
11,448 Expert 8TB
The system(" ... ") call is a synchronous call, i.e. it won't return until the called
shell has finished. Have a look at threads but I'm afraid (from the top of my head)
that all child processed (including the system(" ..." ) call) will be stopped as
soon as the main thread finishes; check it out.

Maybe a system(" ... &") call can help out (Unix specific) or a
system("start ...") call (Windows specific).

kind regards,

Jos
Apr 18 '07 #2
Thank you for the pointer.

I did some research on the net. And I come across function called "execlp". But I don't have any success using this function. I really appreciate if someone can point me to the right direction. Thank you.
Apr 18 '07 #3
Banfa
9,065 Expert Mod 8TB
Try using CreateProcess.

That is the WIN32 API function for starting applications.
Apr 19 '07 #4
Savage
1,764 Expert 1GB
Thank you for the pointer.

I did some research on the net. And I come across function called "execlp". But I don't have any success using this function. I really appreciate if someone can point me to the right direction. Thank you.

Function execlp(and spawnlp which is simmiliar) is used to run a *.exe programm and it can't run *.bat.If *.bat is specifed as a program name and perror is used, it will submit error like:

exec error:Exec format error.

But it does what u recuire with *.exe files.

Function CreateProcess,which admin Banfa specifed, does what u want,but I'm
not sure that it can run *.bat(Never tryed that).

I hope that it will work!!!

:D

Savage
Apr 19 '07 #5
Banfa
9,065 Expert Mod 8TB
Function execlp(and spawnlp which is simmiliar) is used to run a *.exe programm and it can't run *.bat.If *.bat is specifed as a program name and perror is used, it will submit error like:

Function CreateProcess,which admin Banfa specifed, does what u want,but I'm
not sure that it can run *.bat(Never tryed that).
Remember you can always "run" a batch file by actually running a command line processor (cmd.exe) with the batch file on its command line or using the start command Jos mentioned earlier.
Apr 19 '07 #6
Savage
1,764 Expert 1GB
Remember you can always "run" a batch file by actually running a command line processor (cmd.exe) with the batch file on its command line or using the start command Jos mentioned earlier.
Yes,and that would be the simplest solution for our OP.

So if we run cmd.exe with execlp or spawnlp it will overwrite it's parent and start
instead that *.bat file when specifed in command line.


Savage
Apr 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Harald Armin Massa | last post by:
I need to do some synchronisations like in a cron.job import time from threading import Thread class updater(Thread): def run(self): while True: do_updates() time.sleep(600)
3
by: tombat | last post by:
how i can do it? Under Xp/2000, i write a application that stop/start this serices, but under Win98, how i do it? Exists a method via SqlServer or a dos command to stop/start the MSDE program? ...
2
by: Chris Dunaway | last post by:
I need to be able to start and stop a service on another computer on the network. But I cannot connect to the other computer using my standard user credentials. I have a separate login for that...
0
by: Dirk Zimmermann | last post by:
Hi, I like to do the following: Via http I get a stream of data and I like to store this data with a python program. So what I need is to start the downloading and to stop it after a given time....
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
1
by: =?Utf-8?B?S2Vubnk=?= | last post by:
I have one bat file that contains a command to startup Java Program. Then, I would like to create a cluster job to call the bat file. In case of one computer is down, another computer can also call...
0
by: mattcfisher | last post by:
Hi, I have two windows services running together. One is the main program, and one is an "updater" wrapper for the main. The updater service starts and stops the main one (as in you should never...
3
by: shimajavar | last post by:
Hi all I have the following code: a line is growing up by clicking on "up" button, it has another button"start" which I want it to stop the process of growing the line...How can I make a button to...
0
by: =?Utf-8?B?am1hZ2FyYW0=?= | last post by:
My program needs to do X when someone 'starts using' their Windows user account, and it should do Y when they 'stop using' their Windows user account. By 'starts using' I mean they log on, unlock...
4
by: Rajneesh Chellapilla | last post by:
I wrote this program. Its kinda of strange when I make a reset function reset(){c=0} its doest reset the setTimeout. However if I directly pass c=0 to the onclick button it does reset the timer. What...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.