system() command-line no window

This is a discussion on system() command-line no window within the C++ Programming forums, part of the General Programming Boards category; How do you use the system() function to run another exe from the command line without the window showing?...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    system() command-line no window

    How do you use the system() function to run another exe from the command line without the window showing?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I don't think you do. Look into the exec..() series of calls. Or the spawn..() series.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    So, for example, if I were calling:
    Code:
    system("pngout fname.png res.png -f3 -s0 -y -q");
    , I would need to call:
    Code:
    spawnvp(P_WAIT, "pngout fname.png res.png -f3 -s0 -y -q", argv);
    ?

    I'm a little unsure about the argv. I don't know if the parameters should be in there or attached. I couldn't really find anything on google.
    Last edited by yahn; 05-01-2008 at 04:15 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Not sure, but I think it would look more like this:
    Code:
    const char * const argv[] = {"fname.png","res.png","-f3","-s0","-y","-q",NULL};
    spawnvp(P_WAIT, "pngout", argv);
    Use [code][/code] when posting code, not [source][/source].
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    I tried it both ways and neither way worked. system() does work, though. I don't see how else to try it.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Didn't work how? Is it possible it can't find pngout as it's not in the path? Try specifying the full path to the executable.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you might have to do ./program too.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    I tried .\\ \\ ./ / and non of them worked. I have to be vague when I say not working, because I'm not exactly sure what is wrong. I just know that when I call it with system() it is doing exactly what it is supposed to and when I call it with spawnvp() it is not working at all.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    What is the spawnvp call returning?

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    _spawnvp() is returning 1 with "pngout." That means the process is running, but an error occurred, correct? So, something is wrong with the parameters, perhaps? They work with system(). I'm not sure what is going on.

  11. #11
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Looking at the MSDN page for spawnvp (I've made the assumption you're on Windows, but I could be wrong), we find this:

    Quote Originally Posted by MSDN
    Return Value

    The return value from a synchronous _spawnvp or _wspawnvp (_P_WAIT specified for mode) is the exit status of the new process.
    so you're correct; the spawnvp call is successful (it would've returned -1 on error) but the spawned process (pngout) is returning 1. Can't tell you what that means, but I would suspect it's either path or permissions related.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    I don't get it either. I'm calling it in, what appears to me, exactly the same way. Is there any thing else I can do? I can't really find any documentation on this function to help me.

  13. #13
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    This worked for me when all the files are in the same directory, but only in the command prompt, outside of the Visual Studio IDE:
    Code:
    #include <stdio.h>
    #include <process.h>
    
    int main(void)
    {
    	const char * const args[] = 
    	{
    		"in.png", 
    		"out.png",
    		"-f3", 
    		"-s0", 
    		"-y", 
    		"-q", 
    		NULL 
    	};
    	
    	int rc = (int) spawnvp(P_WAIT, "pngout.exe", args);
    	printf("spawnvp returned %d\n", rc);
    	return rc;
    }

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    The problem appears to be that I have to put /i in front of the input file even though it actually clearly states that it works without /i. That isn't a very good explanation for me, but it is working with /i. I thought I would share that in case anyone else ever encounters this problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Window scrollbar
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2005, 01:31 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  4. dont want to use all params
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 02-18-2003, 08:10 AM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 05:08 PM

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