How do you use the system() function to run another exe from the command line without the window showing?
How do you use the system() function to run another exe from the command line without the window showing?
I don't think you do. Look into the exec..() series of calls. Or the spawn..() series.
Mainframe assembler programmer by trade. C coder when I can.
So, for example, if I were calling:, I would need to call:Code:system("pngout fname.png res.png -f3 -s0 -y -q");?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 03:15 PM.
Not sure, but I think it would look more like this:
Use [code][/code] when posting code, not [source][/source].Code:const char * const argv[] = {"fname.png","res.png","-f3","-s0","-y","-q",NULL}; spawnvp(P_WAIT, "pngout", argv);
"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
I tried it both ways and neither way worked. system() does work, though. I don't see how else to try it.
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.
you might have to do ./program too.
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.
What is the spawnvp call returning?
_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.
Looking at the MSDN page for spawnvp (I've made the assumption you're on Windows, but I could be wrong), we find this:
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.Originally Posted by MSDN
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.
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; }
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.