LoadLibraryW和POSIX路径分隔符 [英] LoadLibraryW and POSIX path separator

查看:150
本文介绍了LoadLibraryW和POSIX路径分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要这样做使用普通的Unix反斜杠 /:

The MSDN documentation for LoadLibrary warns not to use normal Unix slash "/":


指定路径时,请务必使用反斜杠(\),而不是正斜杠(/ )。

When specifying a path, be sure to use backslashes (\), not forward slashes (/).

在调用此API时,使用正斜杠或反斜杠(或同时使用两者)都找不到任何问题。我尝试了以下路径名:

I couldn't find any problems using either forward slashes or backslashes (or both) when calling this API. I tried the following pathnames:

c:/foo/bar/baz.dll
/foo/bar/baz.dll
c:/foo/bar\\baz.dll
/foo/bar\\baz.dll
./bar/baz.dll

所有组合均按预期工作。 MSDN为什么建议不使用正斜杠作为路径分隔符?

All combinations worked as expected. Why does the MSDN recommend against using forward slashes as path separators?

编辑:

关于UNC名称,它也可以与 //127.0.0.1/c$/foo/bar/baz.dll完美配合。

Regarding UNC Names, it works perfectly well with "//127.0.0.1/c$/foo/bar/baz.dll" as well.

添加 \?\不会加载库,但是_wfopen也无法打开文件。 LoadLibraryW与其他Windows API接受/不接受正斜杠有何不同?为什么会有关于LoadLibraryW而不是CreateFileW的明确警告。

And yes if you add "\?\" it does not load the library but _wfopen would also fail to open file. How LoadLibraryW is different from any other Windows API accepting/not-accepting forward slashes? Why there are explicit warning regarding LoadLibraryW and not CreateFileW.

CreateFile: https://msdn.microsoft.com/zh-CN/library/windows/desktop/aa363858(v = vs.85).aspx

CreateFile: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx


要创建或打开的文件或设备的名称。您可以在此名称中使用正斜杠(/)或反斜杠()。

The name of the file or device to be created or opened. You may use either forward slashes (/) or backslashes () in this name.

LoadLibrary: https://msdn.microsoft.com/zh-CN/library/ Windows / desktop / ms684175(v = vs.85).aspx

LoadLibrary: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspx


指定路径时,请务必使用反斜杠(),而不是正斜杠(/)。

When specifying a path, be sure to use backslashes (), not forward slashes (/).

在使用正斜杠(/)时是否存在其他一些可能导致LoadLibrary失败的pitfaail,而其他Windows API会接受正斜杠

Is there some other pitfails that may cause LoadLibrary to fail when forward slashes (/) are used, while other Windows API would accept forward slashes without any issue?

推荐答案

Windows API中的文件I / O将正斜杠( /)转换为反斜杠( \),然后将请求传递给本机API。本机API需要使用NT样式的名称。当路径名带有 \\?\前缀时,不执行任何转换。有时您无法控制此前缀是否存在(例如,使用Windows API检索基本路径时)。

File I/O in the Windows API converts forward slashes ("/") to backslashes ("\"), before passing the request to the native API. The native API expects NT-style names. No conversion is performed when the pathname is prefixed with "\\?\". Sometimes you do not have control over whether or not this prefix is present (e.g. when retrieving the base path using the Windows API).

简而言之:使用本机路径分隔符是安全的解决方案。违反合同并使用正斜杠可能会突然停止工作。

In short: Using the native path separator is the safe solution. Violating the contract and using a forward slash may suddenly stop working.

完整信息记录在MSDN上的命名文件,路径和命名空间

Complete information is documented in the MSDN at Naming Files, Paths, and Namespaces.

这篇关于LoadLibraryW和POSIX路径分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆