Thread: Starting a new program

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

    Question Starting a new program

    In a console based application, how do you run a new program without closing the original. For example I open a console version of microsofts "run". The program opens the new program you type in.

  2. #2
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    You mean like the system() call?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    25
    No, I mean, in a program you could have something like

    Code:
    open(a_different_program.exe);
    and the other program would appear on the screen.

  4. #4
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Correct me if I'm wrong, but I'm pretty sure that's what the system() call does... if you want a console application to open in a new window, you'll have to do something like pass cmd.exe the name of the console application as an argument.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    25
    Could you give me an example.

    I thought system could only be used toaccess things like "ping" that are commonly used in command prompt. I also read elsewere on this forum that system(); isn't a good command to use.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    I think you may be wanting ShellExecute(), which to my knowledge is similar to system()? I don't quite know, however I do know you can do this:

    Code:
    #include <windows.h>  // ShellExecute()
    
    ...
    	// The below line will open your homepage in Firefox if it's installed
    	ShellExecute(NULL, "open", "C:\\Program Files\\Mozilla Firefox\\firefox.exe", NULL, NULL, NULL);
    ...
    To my knowledge, again which may be wrong, system() echos as if you went to start->run->cmd and you typed the text in. ( Which can probably open a new process too, if that's what you are wanting )

    Also, if you are wanting to create a process and control it, that's a whooooooooole nother story.
    Last edited by tjpanda; 10-12-2007 at 12:16 AM. Reason: Fixed "...\firefox.exe" to "...\\firefox.exe"

  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
    Whilst system() is the most portable (although not very portable), it is also the least safe.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    25
    I didn't get an error, but I also didn't get any effect from the shell execute(); command.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Can you post the code you used the ShellExecute with?

  10. #10
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    >I also read elsewere on this forum that system(); isn't a good command to use.
    system() is a perfectly good function to use. It's bad when it's misused (for example, using it to call things like "pause" when you can simply use getchar() ).

    >To my knowledge, again which may be wrong, system() echos as if you went to start->run->cmd and you typed the text in.
    It's been awhile since I used Windows, but I'm pretty sure that typing a Win32 program at the prompt will open it the way it's supposed to execute, but then again, I could be wrong. I know this works on Mac and Linux.

  11. #11
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Quote Originally Posted by histevenk View Post
    I didn't get an error, but I also didn't get any effect from the shell execute(); command.
    Did you remember to escape backslashes in the path?

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    25
    Code:
     
    ShellExecute(NULL, "open", "1.exe", NULL, NULL, NULL);

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    You should change "1.exe" to the path of 1.exe. For example, if 1.exe resided in C:\ you would need to use:

    Code:
    ShellExecute(NULL, "open", "C:\\1.exe", NULL, NULL, NULL);

  14. #14
    Registered User
    Join Date
    Jun 2007
    Posts
    25
    Give me a second to read through this. Thanks

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    25
    Code:
     
    ShellExecute(NULL, "open", "C:\\Dev-Cpp\\me\\1.exe", NULL, NULL, NULL);
    The program is still not opening and I get the following error
    196
    C:\Dev-Cpp\Me\dos.cpp
    [Warning] passing NULL used for non-pointer converting 6 of `HINSTANCE__* ShellExecuteA(HWND__*, const CHAR*, const CHAR*, const CHAR*, const CHAR*, INT)'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting C++;First Program
    By Caliban in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2009, 01:41 PM
  2. Starting a program
    By mcgeady in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2006, 12:52 PM
  3. starting program
    By Ideswa in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2006, 02:36 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM