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
View all comments
Show parent comment
2
u/chengfeng-xie Sep 07 '26
Somewhat off-topic, but one advantage of
lpApplicationNameis that it isn't subject to theMAX_PATH(260) limitation that applies tolpCommandLine[1]:Another edge case I ran into recently is that the full path of the executable passed to
lpCommandLinewas within, but near, theMAX_PATHlimit. The call toCreateProcessWdidn't return an error, but the newly created process immediately failed with an Application Error popup containing the following message (in some terminals, there was no popup and the process simply failed, making the error harder to trace):The error code
0xc0000106corresponds toNT_STATUS_NAME_TOO_LONG. After some investigation, it turns out that, when loading DLL dependencies, if a DLL comes with a manifest, a path resulting from appending.2.Configto the full path of that DLL is created on the fly. If that path exceedsMAX_PATH(including the terminating\0), DLL loading fails with the above error, and the process fails to start. A similar issue withLoadLibraryExWis described in [2]. Ultimately, I think this is a bug that should be fixed in future OS builds. For now, however, a workaround is to prepend\\?\to the normalized full path of the executable [3] and then pass the resulting path tolpApplicationName.