How can I calculate the complete buffer size for G

2019-01-26 06:57发布

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)

7条回答
欢心
2楼-- · 2019-01-26 07:27

Using

extern char* _pgmptr

might work.

From the documentation of GetModuleFileName:

The global variable _pgmptr is automatically initialized to the full path of the executable file, and can be used to retrieve the full path name of an executable file.

But if I read about _pgmptr:

When a program is not run from the command line, _pgmptr might be initialized to the program name (the file's base name without the file name extension) or to a file name, relative path, or full path.

Anyone who knows how _pgmptr is initialized? If SO had support for follow-up questions I would posted this question as a follow up.

查看更多
登录 后发表回答