gpt4 book ai didi

windows - 如何计算 GetModuleFileName 的完整缓冲区大小?

转载 作者:可可西里 更新时间:2023-11-01 12:34:34 26 4
gpt4 key购买 nike

GetModuleFileName()将缓冲区和缓冲区大小作为输入;然而它的返回值只能告诉我们复制了多少个字符,以及大小是否不够(ERROR_INSUFFICIENT_BUFFER)。

如何确定为 GetModuleFileName() 保存整个文件名所需的实际缓冲区大小?

大多数人使用 MAX_PATH 但我记得路径可以超过那个(默认定义为 260)...

(使用零作为缓冲区大小的技巧不适用于此 API - 我之前已经尝试过)

最佳答案

通常的方法是调用它,将大小设置为零,保证失败并提供分配足够缓冲区所需的大小。分配一个缓冲区(不要忘记空终止的空间)并第二次调用它。

很多情况下MAX_PATH就足够了,因为许多文件系统限制路径名的总长度。但是,可以构造出超过 MAX_PATH 的合法且有用的文件名。 , 因此查询所需缓冲区可能是个好建议。

不要忘记最终从提供缓冲区的分配器返回缓冲区。

编辑:Francis 在评论中指出,通常的配方不适用于GetModuleFileName()。不幸的是,Francis 在这一点上是绝对正确的,我唯一的借口是我在提供“常规”解决方案之前没有去查证它。

我不知道那个 API 的作者在想什么,除了有可能在引入它时,MAX_PATH确实是最大的可能路径,使正确的食谱变得容易。只需在长度不小于 MAX_PATH 的缓冲区中进行所有文件名操作即可字符。

哦,是的,不要忘记自 1995 年左右以来的路径名允许使用 Unicode 字符。因为 Unicode 占用更多空间,所以任何路径名前面都可以加上 \\?\明确要求 MAX_PATH对该名称的字节长度的限制被删除。这使问题复杂化。

MSDN 在标题为 File Names, Paths, and Namespaces 的文章中对路径长度有这样的说法:

Maximum Path Length

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\<some 256 character path
string><NUL>
" where "<NUL>" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

Note File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\\?\" prefix as detailed in the following sections.

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function. To specify an extended-length path, use the "\\?\" prefix. For example, "\\?\D:\<very long path>". (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

Note The maximum path of 32,767 characters is approximate, because the "\\?\" prefix may be expanded to a longer string by the system at run time, and this expansion applies to the total length.

The "\\?\" prefix can also be used with paths constructed according to the universal naming convention (UNC). To specify such a path using UNC, use the "\\?\UNC\" prefix. For example, "\\?\UNC\server\share", where "server" is the name of the machine and "share" is the name of the shared folder. These prefixes are not used as part of the path itself. They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory. Also, you cannot use the "\\?\" prefix with a relative path, therefore relative paths are limited to MAX_PATH characters as previously stated for paths not using the "\\?\" prefix.

When using an API to create a directory, the specified path cannot be so long that you cannot append an 8.3 file name (that is, the directory name cannot exceed MAX_PATH minus 12).

The shell and the file system have different requirements. It is possible to create a path with the Windows API that the shell user interface might not be able to handle.

所以一个简单的答案是分配一个大小为 MAX_PATH 的缓冲区, 检索名称并检查错误。如果合适,你就完成了。否则,如果它以“\\?\”开头,得到一个大小为 64KB 左右的缓冲区(上面的短语“32,767 个字符的最大路径是近似的”在这里有点麻烦所以我留下一些细节以供进一步研究)和再试一次。

溢出 MAX_PATH但不是以“\\?\”开头似乎是“不可能发生”的情况。同样,接下来要做什么是您必须处理的细节。

对于以“\\Server\Share\”开头的网络名称,路径长度限制是多少可能会有一些混淆,更不用说内核对象 namespace 中以“\\.\”开头的名称了。上面的文章没有说,我也不确定这个API能不能返回这样的路径。

关于windows - 如何计算 GetModuleFileName 的完整缓冲区大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/805814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com