Thread: Open a new program from a C++ application

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    Open a new program from a C++ application

    Hi,
    I have a C++ program and I was wondering what different ways there was to open another application (such as CALC or notepad) from that C++ program. Thanks
    Amish

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    In <cstdlib>, you can use the system() function to execute system commands. It takes a char * as a parameter, so you can either put a string literal, or a pointer to the null-terminated string you wanted executed. So, for example, in Windows:
    Code:
    #include <cstdlib>
    ...
    system("notepad");
    ...
    The use of system() is often advised against for several reasons. The main reason is that you don't know the program will be there. If you're running on a different OS, or if you have user that for some reason or another has deleted that program, it will cause problems. Just keep this in mind - system is not forbidden.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Thanks for the reply. Would system() allow me to pass command line parameters to the program.
    Amish

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Yes. It's just the same as if you were typing something in at the command prompt. For example:
    Code:
    system("format a:")

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    I tired doing
    system("calc");
    but a command prompt window also opened up in the backgroud and I don't want that to happen. I did a search for system() on google and msdn and nothing came up. You would not happen to know where I could get more information on that.
    Amish

  6. #6
    Hello,

    You can find a good reference about the system() command here: stdlib reference: system(). Or for other guides you can view the following links:


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but a command prompt window also opened up in the backgroud
    Read the FAQ
    It explains the disadvantages of system(), and gives alternative methods for starting processes on various operating systems.

    system() is a barely portable catch-all way of starting a new process, but its seldom the most ideal way.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    ok I looked at the info on system but it seems that you can't monitor the process after opening it and I still haven't figured out how to make the command prompt not appear in the background.
    I have read that process() might do the job. I am looking into that now. If anybody knows about any newbie documentation on process, feel free to post it. Thanks for the help guys
    Amish

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Quote Originally Posted by Salem
    Thanks this was a very interesting page. I did not know that Cprogramming.com contained these kind of information.
    Amish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. what is required to open a MSVC++ compiled program?
    By pc2-brazil in forum Windows Programming
    Replies: 8
    Last Post: 08-05-2008, 05:20 AM
  3. Open with my program
    By elmutt in forum Windows Programming
    Replies: 12
    Last Post: 08-14-2007, 02:23 AM
  4. change this program to open a file
    By A F Army in forum C Programming
    Replies: 3
    Last Post: 04-05-2003, 05:25 PM
  5. get program to open a html file
    By task in forum C Programming
    Replies: 14
    Last Post: 11-09-2001, 12:20 PM