Help - Search - Members - Calendar
Full Version: Using system() to call application with parameters
Hydrogenaudio Forums > Hosted Forums > foobar2000 > Development - (fb2k)
Neural_Overload
I am trying to mute foobar2000 from the command line using system(). I have tried using
CODE
system("\"C:\\Program Files\\Foobar2000\\foobar2000.exe\" \"/command:volume mute\"");
but that does not seem to be working correctly as the command line says
CODE
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
when my program is run. I have a feeling it's just a syntax error, but because I'm a noob to C++ I can't see it...
Nickoladze
you probably need to escape the spaces
2E7AH
well first your backslashes looks very messy.

that error only occures when you use quatation marks incorectly.

you should pass something like:

"c:\program files\foobar2000\foobar2000.exe" /command:volume mute
Neural_Overload
QUOTE(2E7AH @ Jun 9 2008, 19:22) *

well first your backslashes looks very messy.

that error only occures when you use quatation marks incorectly.

you should pass something like:

"c:\program files\foobar2000\foobar2000.exe" /command:volume mute
Do you mean something like?
CODE
system("\"C:\\Program Files\\Foobar2000\\foobar2000.exe\" /command:volume mute");
I tried that and it doesn't work, foobar says:
CODE
Unable to open item for playback (Unsupported file format):
"C:\Documents and Settings\Neural_Overload\My Documents\Visual Studio 2008\Projects\FooControl\FooControl\mute"
2E7AH
sorry, man i don't know the C syntax, but the problem is obviously in the quatation marks.
i was trying to show, only how the parse should look like:

"c:\program files\foobar2000\foobar2000.exe" "/command:volume mute"

and not

"c:\program files\foobar2000\foobar2000.exe" /command:volume mute

my mistake
Neural_Overload
QUOTE(2E7AH @ Jun 9 2008, 19:49) *

sorry, man i don't know the C syntax, but the problem is obviously in the quatation marks.

i was trayng to show, only how the parse should look like:

"c:\program files\foobar2000\foobar2000.exe" "/command:volume mute"

and not

"c:\program files\foobar2000\foobar2000.exe" /command:volume mute

my mistake

CODE
system("c:\program files\foobar2000\foobar2000.exe" "/command:volume mute");


is this working?
You see, I'm pretty sure you have to escape the double quotes and the backslashes in the path. Kinda like this:
CODE
system("\"c:\\program files\\foobar2000\foobar2000.exe\" \"/command:volume mute\"");
Which as I said before, does not work.
2E7AH
you are right, the parse should be fine that way, but...

maybe with 8.3 style:

CODE
system("c:\\progra~1\\foobar~1\\foobar~1.exe \"/command:volume mute\"");
Neural_Overload
QUOTE(2E7AH @ Jun 9 2008, 20:44) *

you are right, the parse should be fine that way, but...

maybe with 8.3 style:

CODE
system("c:\\progra~1\\foobar~1\\foobar~1.exe \"/command:volume mute\"");

It works if I do that, but now I have to figure out how to convert from normal to 8.3
2E7AH
i'm glad that finally something worked.

i don't understand what you have to figure out!?
why do you need that?
what are you trying to do?

Neural_Overload
QUOTE(2E7AH @ Jun 9 2008, 21:59) *

i'm glad that finally something worked.

i don't understand what you have to figure out!?
why do you need that?
what are you trying to do?
I'm trying to make a program similar to this program HERE however I want to expand upon the idea and so I don't want to use hard coded paths for the reason that people might have foobar in directories other than C:\Program Files\Foobar2000
Yirkha
Using the CreateProcess API directly won't suffer from this kind of problems.
foosion
QUOTE(Yirkha @ Jun 10 2008, 17:40) *
Using the CreateProcess API directly won't suffer from this kind of problems.
Neither would ShellExecute or _exec, and the number of their parameters is less intimidating. Decisions, decisions...
Yirkha
The problem with exec(2) is that it does never return.
CODE
Each of the  functions  in  the  exec  family  replaces  the
current  process  image  with  a  new process image. The new
image is constructed from a regular, executable file  called
the  new  process image file. This file is either an execut-
able object file or a file of data for an interpreter. There
is  no  return  from a successful call to one of these func-
tions because the calling process image is overlaid  by  the
new process image.
I think you meant _spawn.

(Yes, ShellExecute would work too, I'm just used to use it only for URLs, documents, etc.)
foosion
Oops, indeed.
Neural_Overload
QUOTE(foosion @ Jun 10 2008, 09:55) *

QUOTE(Yirkha @ Jun 10 2008, 17:40) *
Using the CreateProcess API directly won't suffer from this kind of problems.
Neither would ShellExecute or _exec, and the number of their parameters is less intimidating. Decisions, decisions...
Well, that was odd. I tried using shellapi.h and it gave me a ton of compile errors. So I googled and came across someone with similar errors. I used windows.h instead of shellapi.h and my program works, but only if I compile it in DEV-C++. Visual Studio gives me the error
CODE
1>.\foobar.cpp(25) : error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Edit: Problem solved, had to switch to a Multi-byte Character Set instead of Unicode

PS. Thanks for all the help guys! I really appreciate it biggrin.gif
kode54
QUOTE(Neural_Overload @ Jun 10 2008, 14:35) *

QUOTE(foosion @ Jun 10 2008, 09:55) *

QUOTE(Yirkha @ Jun 10 2008, 17:40) *
Using the CreateProcess API directly won't suffer from this kind of problems.
Neither would ShellExecute or _exec, and the number of their parameters is less intimidating. Decisions, decisions...
Well, that was odd. I tried using shellapi.h and it gave me a ton of compile errors. So I googled and came across someone with similar errors. I used windows.h instead of shellapi.h and my program works, but only if I compile it in DEV-C++. Visual Studio gives me the error
CODE
1>.\foobar.cpp(25) : error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Edit: Problem solved, had to switch to a Multi-byte Character Set instead of Unicode

PS. Thanks for all the help guys! I really appreciate it biggrin.gif

Actually, you should use the _T macro to convert the string constants to TCHAR. May as well use Unicode.
Neural_Overload
QUOTE(kode54 @ Jun 10 2008, 15:14) *

Actually, you should use the _T macro to convert the string constants to TCHAR. May as well use Unicode.
Thanks, that works wonderful!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.