Ashley Gullen Profile picture
Sep 22 6 tweets 2 min read Twitter logo Read on Twitter
If you thought JavaScript was a mess, here's what it takes to pass a file path to a Windows API in C++: 🧵
- Windows uses UTF-16, but most modern software uses UTF-8
- Converting UTF-8 to UTF-16 requires calling MultiByteToWideChar twice (once for size, second to convert)
(1/5)
- Alternatively you can set the process code page to UTF-8 and call the 'A' variant API directly, but only sometimes, and only with Windows 10 v1903+, and you might still have to change the system locale setting and reboot
- Now you can pass a file path! But...
(2/5)
- The path is limited to MAX_PATH (260 chars)
- Unless you prefix the path with \\?\
- However using \\?\ disables automatic conversion of / to \ in the path (and some other normalization)
- Alternatively set a reg key LongPathsEnabled, but only with Windows 10 v1607+...
(3/5)
...and the app needs to opt in to longPathAware, and changing the registry might need a reboot
- Even then not all Windows APIs support longer paths, e.g. PathCanonicalize. Swap for PathCchCanonicalizeEx with PATHCCH_ALLOW_LONG_PATHS, etc. Check docs per API.
(4/5)
- Congratulations, your app can pass a long file path to Windows! But the shell still doesn't support long paths. So your app can write to long paths, but you can't access them with Windows Explorer. Even in Windows 11.
- If the API returns a path, do all that in reverse.
(5/5)
I probably missed a few other details and quirks.
References:


See also individual APIs e.g. learn.microsoft.com/en-us/windows/…
learn.microsoft.com/en-us/windows/…
learn.microsoft.com/en-us/windows/…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Ashley Gullen

Ashley Gullen Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(