Thread: Removing windows command line after execution

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    Removing windows command line after execution

    Is there any way to remove the command line after execution. For example, if you use the system function to invoke another program, the cmd is left in the background blank. Is there a function like cmdclose() or something, I've had a search and I failed to come up with anything. Thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    My understanding is this goes away automatically, unless you have a pause, or, the cmd window was open to start with.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    My understanding is this goes away automatically, unless you have a pause, or, the cmd window was open to start with.
    But if you use system() as he used in his example, your program waits for the other to finish before it closes it's command window.

    Instead of system(), I would use the spawn() family of functions - they spawn a separate process and once it's loaded your program continues as normal - closing the command window if that's when it ends.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by sean View Post
    But if you use system() as he used in his example, your program waits for the other to finish before it closes it's command window.
    Yes, certainly. I was assuming that.

    I use this same scenario from C to call Notepad and I reformat a file to < 72 chars per line. When I close Notepad, the cmd window goes away.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Thanks, spawn seems to be what I'm looking for. I wrote this and i'm recieving error code -1, the file paths are relative, and all the files are in the correct location. Any ideas why it's giving that error?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <process.h>
    #include <windows.h>
    
    
    int main(void)
    {   
        int spawn_error_check;
        char eclipse[150];
        char *eclipse_ptr;
        eclipse_ptr = eclipse;
        /*Change these directories to match your setup*/
        sprintf(eclipse, "eclipse\\eclipse.exe -vm \\WINDOWS\\system32\\javaw.exe");
        spawn_error_check = spawnl(_P_NOWAIT, eclipse_ptr, NULL);
        if (spawn_error_check != 0)
        {
            fprintf(stderr,"Spawn error code: %d\n", spawn_error_check);
            system("pause");
            return 1;
        }             
        return 0;
    }
    Last edited by redruby147; 08-28-2009 at 05:48 AM.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    sprintf(eclipse, "eclipse\\eclipse.exe -vm \\WINDOWS\\system32\\javaw.exe");
    spawn_error_check = spawnl(_P_NOWAIT, eclipse_ptr, NULL);
    The executable itself is one argument, and all the command line arguments are separate arguments after that. To signal the end of the arguments, you provide NULL as an argument, like so:

    Code:
    #include <process.h>
    
    int spawnl( int mode,
                const char * path, 
                const char * arg0, 
                const char * arg1..., 
                const char * argn, 
                NULL );
    So you would call:

    Code:
    spawnl(_P_NOWAIT, "eclipse\\eclipse\.exe", "-vm", "\\WINDOWS\\system32\\javaw.exe", "NULL);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone using Windows 7?
    By Sharke in forum General Discussions
    Replies: 60
    Last Post: 07-12-2009, 08:05 AM
  2. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  3. Windows 2k Dos- Program Execution Time
    By maththeorylvr in forum C Programming
    Replies: 1
    Last Post: 03-17-2005, 10:03 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM