Thread: executing a program help

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    24

    Unhappy executing a program help

    I am making a program that can start multiple other programs.
    I do this by
    _____________________________________
    Process ^p;
    ProcessStartInfo ^pInfo;

    pInfo = gcnew ProcessStartInfo();
    pInfo->Verb = "open";
    pInfo->FileName = "c:\\WINDOWS\\System32\\sample.exe";
    pInfo->UseShellExecute = true;

    p = Process::Start(pInfo);
    _________________________________________
    however when i do this the other program takes the place of this one is there a way to stop this from happening

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I am a noob in C++
    I donno Am I right or Not
    I think You can do it through system()
    use system("c:\\WINDOWS\\System32\\sample.exe");
    or just system("sample.exe");

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I guess you'd have to run it on a different thread. What is this library you're using; I'm not familiar (because I don't get out much).
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That looks like managed C++. (Or managed =~ s/m/d/ as Salem once said. ) I don't know anything about it, but if you can use system(), then read on.

    A simple way to run programs in the background without using spawn() or CreateProcess() or some other function from the FAQ is to do this (under Windows):
    Code:
    system("start program.exe");
    start is a program which starts another program in the background. It has several options, such as starting the new program minimized or maximized. The options vary between different versions of Windows -- stick with minimized, maximized, and restored and you should be okay for most Windowses. Open a command prompt and type "start /?" to see the possible options.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by noobcpp View Post
    I am a noob in C++
    I donno Am I right or Not
    I think You can do it through system()
    use system("c:\\WINDOWS\\System32\\sample.exe");
    or just system("sample.exe");
    Hey anybody please say am I wrong ??
    Isnt system() enough ??

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Whoops, yes, you're right.
    however when i do this the other program takes the place of this one is there a way to stop this from happening
    It sounds like the program is doing the equivalent of an exec(), which takes the new program and overwrites the old program with the new. I took it to mean that a program was executing, and took a long time, and the programmer wanted the original program to execute another program while the first one was still going (which is what my reply targeted).

    It's possible that system() wouldn't work with that compiler or whatever. I don't know how it works.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    Quote Originally Posted by dwks View Post
    It's possible that system() wouldn't work with that compiler or whatever. I don't know how it works.
    I am using visual c++ express edition 2005
    I tryed to use system but it didn't work do I need to include some libraries to make it work

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I tryed to use system but it didn't work do I need to include some libraries to make it work
    You need to include <cstdlib>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I am on g++ Here system() works just with <iostream>

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I am on g++ Here system() works just with <iostream>
    It's generally a bad idea to rely on stuff like that. For example, you might provide sample code to somebody who uses a different compiler, and it doesn't work for them. Or you might change compilers and all of a sudden it dosen't work.

    It usually doesn't take that much extra effort to #include the correct header for something you use in your program, and it is usually worth the effort in the end.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Is there any such compiler that doesnt supports system() ??
    Last edited by noobcpp; 06-30-2007 at 10:41 PM. Reason: Spelling Mistake

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Is there any such compiler that doesnt supports system()

    No, all standards compliant libraries have system. However, you have to #include <cstdlib> or <stdlib.h> in order to guarantee it will work.

    Also, what you send to system is platform dependent (which makes sense, since it is a system call). So while system is works with all compilers, what you use it for might not.

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Yes
    In win you need to use
    system("programm.exe");//Probabbly I am not sure
    and on Linux
    system("./programm");

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by noobcpp View Post
    I am on g++ Here system() works just with <iostream>
    This is not a guaranteed behavior, though, if I recall. It just so happens to work on your compiler but may not work on others. The correct library for system() is cstdlib, as stated earlier.

    Edit: You might also want to know that if someone were to replace the console executable (cmd.exe on Windows) with a malicious program, calling system() would trigger that malicious program. Thus, system() isn't safe.
    Last edited by Desolation; 07-01-2007 at 12:26 AM.

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    I used system and it works thanks everybody
    Last edited by tbca; 07-01-2007 at 09:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Executing a program
    By trancedeejay in forum C Programming
    Replies: 7
    Last Post: 03-06-2006, 08:55 AM
  4. Problem executing sample program:
    By mrabiu in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2004, 06:44 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM