r/cpp • u/holyblackcat • Sep 05 '26
Escaping `CreateProcess()` arguments on Windows
https://holyblackcat.github.io/blog/2026/09/05/escaping-createprocess-arguments.html
93
Upvotes
r/cpp • u/holyblackcat • Sep 05 '26
13
u/unaligned_access Sep 05 '26
There's `wil::ArgvToCommandLine`. It's not WinAPI, but it's Microsoft.
https://github.com/microsoft/wil/blob/81abc8e44c075eba65fe4b68ea2f08525cc701e8/include/wil/win32_helpers.h#L1244
C++, and the first comment of course starts with "Somewhat of a hack" ;)
13
u/bbm182 Sep 05 '26
It really is impossible to escape arbitrary arguments to arbitrary programs on Windows. You need to know how the program will decode the command line and there is no standard there, as demonstrated by batch file complications mentioned in the post.
The algorithm used by
CommandLineToArgvWmay seem like it's almost the standard, but that breaks down for programs that are not Unicode aware. The example program in the post is one such example. It usesmain(int argc, char **argv)(rather thanwmain(int argc, wchar_t **argv)orGetCommandLineW), which results in the native wide command line being converted to narrow before splitting. That conversion is quite nasty and depends on the code page. In particular,"(U+FF02 Fullwidth Quotation Mark) usually gets converted into"(U+0022 Quotation Mark), which will result in incorrect splitting.Many common tools have this issue, including things like curl. See the slides and paper on the Worst.Fit site for more detail.
11
u/fdwr fdwr@github 🔍 Sep 06 '26 edited Sep 06 '26
Note narrow
mainworks fine if your app .manifest file specifies UTF-8 as the default code page (ignoring whatever the system is set). It's annoying to have to include this extra snippet, but then it ensures the other parts of your program work nicely too, such asstd::filesystem::path()returning anstd::stringwith UTF-8 characters (rather than throwing an exception upon seeing untransformable characters, or needing to callu8string()and reinterpret cast it in several places):Another way to get the command line (didn't see mentioned) is using
WinMainas an alternate entry point (rather than main) for CLI apps, as it includes thelpCommandLineparameter directly:9
u/holyblackcat Sep 05 '26 edited Sep 05 '26
I've been told that
cmd /chas special quoting rules that I forgot to handle (related to the/sflag),I'll fix the post in a few hoursfixed it.12
u/tartaruga232 MSVC user, r/cpp_modules Sep 05 '26
https://holyblackcat.github.io/blog/2026/09/05/escaping-createprocess-arguments.html
This link posting will likely be deleted by a mod, so I'll repeat the link here.
4
u/alfps Sep 05 '26
I now learned that Microsoft's
_wspawnvfunction does not quote the arguments.Weird.
I was sure it would. Anyway worth mentioning in the article.
15
u/Tringi github.com/tringi Sep 05 '26
That's... ahh... quite opinionated start of the article.
Mostly everyone defines
UNICODEand uses the macros.Absolutely NOT. You should pass the full proper path to the executable you want to run. Then you can significantly simplify its name in
lpCommandLinewithout worrying about repercussions.But the rest is very well researched and thorough.
3
u/ReDr4gon5 Sep 05 '26
What about using RtlCreateUserProcess?
33
u/ICurveI Sep 05 '26
I recently reviewed a PR at work with
CreateProcess, which led me down a similar rabbit hole... This is utter insanity7
u/tomysshadow Sep 05 '26 edited Sep 05 '26
This approach is okay if you are intentionally building an argv-like string from scratch. However, taking an argv passed to your program and turning it back into a string form of the command line arguments (i.e. you just want to passthrough the arguments to a child) is a fundamentally flawed idea unless you know the program you're passing through to also uses the same argv parser.
If there's a possibility the program you are launching uses the command line string directly, you can't take the argv you received and turn it back into a string and pass it on. Because the process of converting the command line string into argv is a lossy process. There's no way to know from looking at an argv array if a specific argument had quotes around it before, or if there were multiple spaces between arguments - or other things the parsing algorithm of the executable you're passing through to might care about, that the C argv parser did not. This is presumably why WinAPI doesn't have a function to go back the other way, as multiple different strings can become the same argv.
The correct approach in such cases is to pass the command line string you got in WinMain through to the child. This is a bit of a pain if you have a command line program, because unlike in WinMain, GetCommandLine() returns the string with the executable name so you need to parse enough of it to tell where the executable name ends and get that substring. But it's a lot less of a pain than trying to reheat the argv array back into string form if you can do it, and because it's just a substring it'll perfectly preserve the exact formatting of the string.
The string is the real command line. argv is a lossy transformation of the string command line into the form C expects.
If you want some particularly nasty edge cases to see what I mean, check out how Visual Studio parses argv. Notice that different languages and implementations have slightly different parsing rules, hence what makes this impossible to do in a completely language agnostic way, as these are ultimately language details and not something the OS (who only cares about the string form) knows about: https://daviddeley.com/autohotkey/parameters/parameters.htm
C#'s Process.Start() also needs to deal with this problem because it can take in an argument list and call down to CreateProcess. The .NET reference source for it is probably the closest thing to an "official" argument list to string conversion
3
u/nevemlaci2 Sep 05 '26
A wild HolyBlackCat spotted on Reddit :D
2
u/TheChief275 Sep 05 '26
oh boy I was actually planning to dive into this for my simple C build system
2
u/jwakely libstdc++ tamer, LWG chair Sep 05 '26
Is this why
cmd.exedoesn't do word splitting or wildcard expansion, so every application has to reimplement wildcard expansion ... but doing it badly, or not at all.0
u/cleverYeti42 Sep 06 '26
This is a lot of careful and competent work.
My summary: do not attempt to use Windows command line.
0
u/Low_Ad3967 Sep 10 '26
33333333
-11
u/palapapa0201 Sep 05 '26
Fuck windows
-4
u/[deleted] Sep 05 '26
[removed] — view removed comment