views:

53

answers:

2

I'm trying to fix an existing C-program with VS2005 that eventually calls

int system(command) //in C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\system.c)

with parameter value

start C:\Program Files\VideoLAN\VLC\vlc.exe C:\Documents and Settings\me\My Documents\My Music\09 - Track09.mp3

the program to be started and the filename paths are both formed from env variables that are got and the command start is appended to start of char buffer. The env variables are:

  • %VLCPATH% which has value C:\Program Files\VideoLAN\VLC

  • %MUSIC% which has value C:\Documents and Settings\me\My Documents\My Music

I've been trying this with XP Command Prompt and everything works fine when paths don't have spaces. Also this works:

"%VLCPATH%\vlc.exe" "%MUSIC%\09 - Track09.mp3"

So what should I do?

  1. edit env variables to have quotes? (Don't think so)
  2. check if command has file as parameter and then somehow add quotes with escape character to maybe both of them and remove the word start?
  3. do something sensible / elegant that I'm not aware of
+1  A: 

In Windows, both the program path to start and any arguments with pathnames need to be enclosed in double quotation marks ("like this") if they contain spaces.

For example:

"C:\Program Files\VideoLAN\VLC\vlc.exe" "C:\Documents and Settings\me\My Documents\My Music\09 - Track09.mp3"

Peladao
thanks. but what about the start? can this be done with start in the beginning of the "string" ??
matti
this doesn't work on XP Command Prompt: it starts 09 - Track09.mp3 with default program which is Media Player. It ignores the first parameter for start (vlc program).
matti
Sorry, removed "start".The second part in quotes (the file argument) will now be passed on to vlc.exe as a single command line argument.
Peladao
@peladao: thanks for your effort but as I state in my question I know this works. I tried it before asking...
matti
+5  A: 

I would try quoting all of the parameters, for example:

int main(int argc, char *argv[])
{
  char command[1024];
  char *title = "test vlc";
  char *executable = "vlc.exe";
  char *param = "09 - Track09.mp3";

  snprintf(command, sizeof(command), "start \"%s\" \"%s\" \"%s\"",
           title, executable, param);
  printf("%s\n", command);
  system(command);

  return EXIT_SUCCESS;
}

Obviously replace executable and param with however you determine your executable and params.

Brandon Horsley
this doesn't work on XP Command Prompt: it starts 09 - Track09.mp3 with default program which is Media Player. It ignores the first parameter for start (vlc program).
matti
sorry, but I have to make few changes to try this code. but the Command Prompt does not accept the<pre>start "C:\Program Files\VideoLAN\VLC\vlc.exe" "C:\Documents and Settings\me\My Documents\My Music\09 - Track09.mp3"</pre>
matti
@matti, The first parameter to the "start" command is a window title, not the executable. I have updated my solution, please try it and let me know if it worked.
Brandon Horsley
@brandon: thanks! it works!!!
matti