Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Using system() to call application with parameters (Read 10010 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Using system() to call application with parameters

I am trying to mute foobar2000 from the command line using system(). I have tried using
Code: [Select]
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: [Select]
'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...

Using system() to call application with parameters

Reply #1
you probably need to escape the spaces

Using system() to call application with parameters

Reply #2
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

Using system() to call application with parameters

Reply #3
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: [Select]
system("\"C:\\Program Files\\Foobar2000\\foobar2000.exe\" /command:volume mute");
I tried that and it doesn't work, foobar says:
Code: [Select]
Unable to open item for playback (Unsupported file format):
"C:\Documents and Settings\Neural_Overload\My Documents\Visual Studio 2008\Projects\FooControl\FooControl\mute"

Using system() to call application with parameters

Reply #4
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

 

Using system() to call application with parameters

Reply #5
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: [Select]
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: [Select]
system("\"c:\\program files\\foobar2000\foobar2000.exe\" \"/command:volume mute\"");
Which as I said before, does not work.

Using system() to call application with parameters

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

maybe with 8.3 style:

Code: [Select]
system("c:\\progra~1\\foobar~1\\foobar~1.exe \"/command:volume mute\"");

Using system() to call application with parameters

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

maybe with 8.3 style:

Code: [Select]
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

Using system() to call application with parameters

Reply #8
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?

Using system() to call application with parameters

Reply #9
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

Using system() to call application with parameters

Reply #10
Using the CreateProcess API directly won't suffer from this kind of problems.
Full-quoting makes you scroll past the same junk over and over.


Using system() to call application with parameters

Reply #12
The problem with exec(2) is that it does never return.
Code: [Select]
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.)
Full-quoting makes you scroll past the same junk over and over.


Using system() to call application with parameters

Reply #14
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: [Select]
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

Using system() to call application with parameters

Reply #15

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: [Select]
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

Actually, you should use the _T macro to convert the string constants to TCHAR. May as well use Unicode.