How can I calculate the complete buffer size for GetModuleFileName?

getmodulefilename, winapi, windows

Solution

Implement some reasonable strategy for growing the buffer like start with MAX_PATH, then make each successive size 1,5 times (or 2 times for less iterations) bigger then the previous one. Iterate until the function succeeds.

Problem

The `GetModuleFileName()` takes a buffer and size of buffer as input; however its return value can only tell us how many characters is has copied, and if the size is not enough (`ERROR_INSUFFICIENT_BUFFER`). How do I determine the real required buffer size to hold entire file name for `GetModuleFileName()`? Most people use `MAX_PATH` but I remember the path can exceed that (260 by default definition)... (The trick of using zero as size of buffer does not work for this API - I've already tried before)

Original source