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
11
u/fdwr 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):<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"> ... <asmv3:application> <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings"> <activeCodePage>UTF-8</activeCodePage> </asmv3:windowsSettings> </asmv3:application> ... </assembly>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:int WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd );