r/ProgrammerTIL • • Jul 06 '21

Other Language [Any] TIL text files are binary files

0 Upvotes

7 comments sorted by

View all comments

Show parent comment

2

u/iiiinthecomputer Jul 06 '21 edited Jul 06 '21

Except on Windows where everything is UTF16LE. Except when it isn't....

Trying to call a PowerShell script with an encoded argument was ... different. You have to encode your script as UTF16LE with no BOM then base64 encode that. Gruesome.

The PowerShell encoded argument back is only necessary because the quoting rules for windows are so utterly putrid - mainly due to its lack of argument vector support. Each executable has to parse its own arguments from single a char* string... and not all of them use the MSVCRT routines to do so. cmd.exe in particular is a crime against command lines. Then you have the spectacularly weird rules the "standard" argument parsing in MSVCRT has around quotes and backslashes... and the total lack of any WIN32 api to help you encode an argument vector as a command line to round trip it through CreateProcess intact even if the recipient does use the MSVCRT art handling. Oh, and did I mention that many interfaces like spawnv() or PowerShell's start-process -ArgumentList appear to take a structured argument array of vector.... then just join it all into one string with space separators and no quoting? None of which is documented? Frothing insanity.

If you enjoy pain, read this, the authoritative guide on how to construct an argument string to pass to another program on Windows: https://docs.microsoft.com/en-gb/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way . The article is old but no newer docs appear to exist, nor has Microsoft fixed the lack of an inverse to CommandLineToArgvW() in the 10 years since this was published.

/Rant