syntax for launching another application?

This is a discussion on syntax for launching another application? within the C Programming forums, part of the General Programming Boards category; I know this is probably painfully simple, but I need to know how to launch another app from within my ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    syntax for launching another application?

    I know this is probably painfully simple, but I need to know how to launch another app from within my program.

    p.s. Is there a way to launch an EXE and grab the handle at the same time?

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, depending on how much control you want, you could either
    Code:
    system("application-path");
    or, since you are on Windows
    Code:
    CreateProcess(...);
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Gah. I'm not getting this. CreateProcess has too many bloody arguments, and system() isn't working as desired. When I tried it by itself, it launched the program but the CMD window stayed and locked up the app that called it. I tried adding a /C to the system() call, but it's not working. Bottom line: I'm trying to start another program (just as if I went and double clicked on it, nothing special) and grab a handle to it for later (with PROCESS_ALL_ACCESS), preferably without having to enumerate all processes and look for it.

    Code:
    //                    sprintf(tmpFileName, "/C %s", lFileName);
                        sprintf(tmpFileName, "/C \"%s\"", lFileName);
                        system(tmpFileName);

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't think CreateProcess() gives you a handle to the process though, so you'd also need to call OpenProcess().

    If you're having problems with system() & CreateProcess(), then maybe you should try one of the _spawn functions.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Before you pass it to system, try printing it out, like say
    printf( "The command is -->&#37;s<--\n", tmpFileName );

    And then post your results here.
    Yes, the %s is in the middle, so we can easily see if there's say an embedded newline or anything.

    We are of course assuming at this point that tmpFileName is an array with enough space.


    > CreateProcess has too many bloody arguments
    Better get used to it, many functions do.
    There's an example in the FAQ.
    Many of the args can be NULL for default behaviour.

    > p.s. Is there a way to launch an EXE and grab the handle at the same time?
    Only if you use the Win32 API functions for creating a process.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Quote Originally Posted by Salem View Post
    Before you pass it to system, try printing it out, like say
    printf( "The command is -->%s<--\n", tmpFileName );

    And then post your results here.
    Yes, the %s is in the middle, so we can easily see if there's say an embedded newline or anything.

    We are of course assuming at this point that tmpFileName is an array with enough space.


    > CreateProcess has too many bloody arguments
    Better get used to it, many functions do.
    There's an example in the FAQ.
    Many of the args can be NULL for default behaviour.

    > p.s. Is there a way to launch an EXE and grab the handle at the same time?
    Only if you use the Win32 API functions for creating a process.
    Shows perfectly normal. I did sprintf(tmpFileName, "cmd /C \"%s\"", lFileName); and it still kept the damn CMD window open and froze my app as long as that window was open. I thought /C was supposed to execute the command and close the damn CMD window.

    What FAQ? Is there one here or just that MSDN example? Searching the FAQ forum didn't turn up anything useful.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Try start.
    Code:
    system("start notepad");
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Viper187 View Post
    Shows perfectly normal. I did sprintf(tmpFileName, "cmd /C \"%s\"", lFileName); and it still kept the damn CMD window open and froze my app as long as that window was open. I thought /C was supposed to execute the command and close the damn CMD window.

    What FAQ? Is there one here or just that MSDN example? Searching the FAQ forum didn't turn up anything useful.
    /C means that the command window closes when the command you passed in is finished. Since you don't tell us what that is, we can't really tell you how long that can be expected to take.

    system() is synchronous, so it waits for the child process to finish before it returns.

    As dwks suggests, a command to system like "start xxx" will start a new process running the "xxx" process, and almost immediately return to your current code.

    Or you can use CreateProcess and have full control over the newly created process - yes it adds a bit more code (more arguments, but as stated above, most of theose are "NULL" or 0 for normal use).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by cpjust View Post
    I don't think CreateProcess() gives you a handle to the process though, so you'd also need to call OpenProcess().

    If you're having problems with system() & CreateProcess(), then maybe you should try one of the _spawn functions.
    CreateProcess does give you a handle to the newly created process. The last argument of the function is a pointer to a PROCESS_INFORMATION structure.
    I hate real numbers.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    CreateProcess() is handy if you are trying to manipulate the child process. It is certainly safer than calling something like system("NOTEPAD.EXE"), which introduces potential security holes.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Ah. I think I have CreateProcess working now. I'll test it more once I get some other stuff sorted out.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It has a nasty number of parameters, however most of them can be zeroed. ShellExecute() isn't much better, but I have needed to use it a time or two before.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by foxman View Post
    CreateProcess does give you a handle to the newly created process. The last argument of the function is a pointer to a PROCESS_INFORMATION structure.
    Oh is that what that is? I just took a quick look at the function and didn't see a HANDLE parameter or return value. I guess it helps if you're not in a hurry when reading the MSDN.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21