Add support for raw commandlines in g_spawn (Windows only)
On UNIX, processes have access to a vector of arguments natively. Command lines are just high-level constructs of the shell, but do not exist at the OS level.
On Windows, instead, it's the other way round: processes have access to a single string which must contain all invocation options (a commandline). At the OS level there are only commandlines.
However, C and C++ require use of argument vectors, so an encoding scheme was developed for encoding a series of arguments in one string, and decoding back to a series of strings. That works much in the same way as JSON.encode(string_a, string_b, string_c)
and JSON.parse()
, in principle. The CRT at startup parses the commandline into an argv
variable that is then passed on to main()
.
Enter g_spawn functions
g_spawn
functions were designed after UNIX systems, and as such take an argument vector. The argument vector is then "transferred" as-is to the spawned program. That is, the spawned program finds, in its main()
procedure, an argv
member to member identical to the argv
given to g_spawn
. That's the expectation on UNIX, and as a consequence also on Windows, for consistency across platforms.
That's nice and all for programs looking at arguments through their argv
, or using the CommandLineToArgv helper API (the vast majority). However, there are also programs which are not based around that commandline scheme. The problem with using such programs through the g_spawn
API is that:
- It's difficult to work against the g_spawn argv-to-commandline encoding
- Sometimes it's even impossible
For example, the right commandline for cmd.exe
to execute a batch file is cmd /C ""%PROGRAMFILES%\\folder\\dowork.cmd""
(inside two double-quotes) However there's no way to produce such commandline with g_spawn
.
Finally, even for programs which use argv
or CommandLineToArgv
, if one is given a commandline that is already argv-to-commandline
encoded, how could that be used with the g_spawn
API?
Conclusion
I suggest adding a new GSpawnFlags entry to request that no argv-to-commandline encoding has to be done. That entry should be used when:
- The target program uses a custom commandline scheme (e.g cmd) or
- One has already done the argv-to-commandline encoding, and as such the encoding step is not needed / desirable.
In that case the argv
should be concatenated without any modification, just like done by the CRT _spawnv functions.