Naming Files, Paths, and Namespaces-命名文件,路径,命名空间

Naming Files, Paths, and Namespaces

All file systems supported by Windows use the concept of files and directories to access data stored on a disk or device. Windows developers working with the Windows APIs for file and device I/O should understand the various rules, conventions, and limitations of names for files and directories.

Data can be accessed from disks, devices, and network shares using file I/O APIs. Files and directories, along with namespaces, are part of the concept of a path, which is a string representation of where to get the data regardless if it’s from a disk or a device or a network connection for a specific operation.

Some file systems, such as NTFS, support linked files and directories, which also follow file naming conventions and rules just as a regular file or directory would. For additional information, see Hard Links and Junctions and Junctions and Reparse Points and File Operations.

命名文件,路径,命名空间

此文翻译了微软对windows的文件名、路径的描述文档

对在windows编程中的文件、路径的操作有重要指导作用

所有被Window所支持的文件系统都使用了文件和目录的概念来存取存储于磁盘或者设备上的数据,Windows开发者使用针对文件或者设备I/O的Windows API应该理解各种规则,约定,和文件以及目录的名称限制。

使用文件I/O API可以从磁盘、设备、网络共享中存取数据。文件和目录以及命名空间,皆为路径概念的一部分,路径即为用来表示对于特定操作从哪里获取数据的一个字符串,无论是从磁盘或者设备又或者从网络连接获取。

有些文件系统,例如NTFS,支持文件和目录的链接,也遵循文件命名的约定和普通文件和目录的规则。更多信息可以参考Hard Links and JunctionsReparse Points and File Operations

File and Directory Names

All file systems follow the same general naming conventions for an individual file: a base file name and an optional extension, separated by a period. However, each file system, such as NTFS, CDFS, exFAT, UDFS, FAT, and FAT32, can have specific and differing rules about the formation of the individual components in the path to a directory or file. Note that a directory is simply a file with a special attribute designating it as a directory, but otherwise must follow all the same naming rules as a regular file. Because the term directory simply refers to a special type of file as far as the file system is concerned, some reference material will use the general term file to encompass both concepts of directories and data files as such. Because of this, unless otherwise specified, any naming or usage rules or examples for a file should also apply to a directory. The term path refers to one or more directories, backslashes, and possibly a volume name. For more information, see the Paths section.

Character count limitations can also be different and can vary depending on the file system and path name prefix format used. This is further complicated by support for backward compatibility mechanisms. For example, the older MS-DOS FAT file system supports a maximum of 8 characters for the base file name and 3 characters for the extension, for a total of 12 characters including the dot separator. This is commonly known as an 8.3 file name. The Windows FAT and NTFS file systems are not limited to 8.3 file names, because they have long file name support, but they still support the 8.3 version of long file names.

文件和目录名称

所有文件系统对于单个文件遵循相同的通用命名约定:一个基础文件名和一个可选的扩展名,使用.分隔。然而,每种文件系统,例如NTFS CDFS exFATUDFSFAT, 和 FAT32,对于目录或文件路径中单独部分的组成,可以有特定且不同的规则。注意目录是一个具有特殊属性的文件,该属性将其指定为目录,除此区别外,依然需要遵守普通文件的命名规则。因为就文件系统而言,术语目录只简单泛指一种特殊类型的文件,一些参考资料将使用通用术语文件来包含目录和数据文件的概念。因此,除非特别指明,否者文件的任何命名或使用规则或示例也应适用于目录。术语
路径指的是一个或多个目录、反斜杠,可能还有卷名。更多信息请参考Paths

根据不同的文件系统和使用不同的路径名称前缀格式,路径字符长度的限制也不同。这使得向后兼容变得更加复杂。例如,旧的MS-DOS FAT文件系统支持最多8个字符的基础文件名个3个字符的扩展名,包含.分隔符总共12个字符。 这就是通常说的8.3文件名,Windows FATNTFS文件系统不限制于8.3文件名,因为它们虽然有长文件名支持,但是依然支持8.3版本的长文件名。

Naming Conventions

The following fundamental rules enable applications to create and process valid names for files and directories, regardless of the file system:

Use a period to separate the base file name from the extension in the name of a directory or file.

Use a backslash () to separate the components of a path. The backslash divides the file name from the path to it, and one directory name from another directory name in a path. You cannot use a backslash in the name for the actual file or directory because it is a reserved character that separates the names into components.

Use a backslash as required as part of volume names, for example, the “C:” in “C:\path\file” or the “\server\share” in “\server\share\path\file” for Universal Naming Convention (UNC) names. For more information about UNC names, see the Maximum Path Length Limitation section.

Do not assume case sensitivity. For example, consider the names OSCAR, Oscar, and oscar to be the same, even though some file systems (such as a POSIX-compliant file system) may consider them as different. Note that NTFS supports POSIX semantics for case sensitivity but this is not the default behavior. For more information, see CreateFile.

Volume designators (drive letters) are similarly case-insensitive. For example, “D:” and “d:” refer to the same volume.

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

The following reserved characters:

< (less than)

> (greater than)

: (colon)

" (double quote)

/ (forward slash)

\ (backslash)

| (vertical bar or pipe)

? (question mark)

* (asterisk)

Integer value zero, sometimes referred to as the ASCII NUL character.

Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.

Any other character that the target file system does not allow.

Use a period as a directory component in a path to represent the current directory, for example “.\temp.txt”. For more information, see Paths.

Use two consecutive periods (…) as a directory component in a path to represent the parent of the current directory, for example “…\temp.txt”. For more information, see Paths.

Do not use the following reserved names for the name of a file:

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended. For more information, see Namespaces.

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, “.temp”.

命名约定

不论在哪种文件系统上,下面的基本规则在应用程序创建和处理有效的文件和目录名称时起作用:

  • 使用一个.将文件和目录名中的基础文件名和扩展名分隔。

  • 使用一个反斜杠(\)分隔路径中的各个部分。反斜杠将文件名与其路径分开,并将路径中的一个目录名与另一个目录名分开。你不能在一个真实的文件或者目录名中使用一个反斜杠,因为反斜杠是用来分隔组件名称的保留字符。

  • 卷名中必须使用一个反斜杠。例如,C:\C:\path\file中或者对于通用命名规则(UNC),\\server\share\\server\share\path\file中。UNC的更多信息,请参考Maximum Path Length Limitation。

  • 不要假定区分大小写,例如,我们认为名称OSCAR, Oscar,oscar是相同的,即使一些文件系统(如POSIX兼容的文件系统)可能认为它们是不同的。注意,NTFS支持区分大小写的POSIX语义,但这不是默认行为。更多信息可以参考CreateFile

  • 卷标识符(驱动器字符)同样不区分大小写,例如,D:\d:\ 描述同样的卷。

  • 在名称中可以使用当前代码页中的任何一个字符,包括Unicode字符和扩展字符集(128–255)中的字符,除了以下这些字符:

    • 下面的都是保留字符:
      • <(小于号)
      • >(大于号)
      • :(冒号)
      • "(双引号)
      • /(斜杠)
      • \(反斜杠)
      • |(竖线符或者管道)
      • ?(问号)
      • *(星号)
    • 整数值0,有时表示ASCII的NUL字符
    • 整数表示在1到31之间的字符,除了在交换数据流中允许使用的字符,更多信息请参考File Streams
    • 剩余的目标文件系统中不允许使用的字符。
  • 在路径中使用一个.作为目录部分表示当前目录,例如,.\temp.txt,更多信息请参考Path。

  • 使用两个连续的.作为目录部分表示当前目录的父目录,例如,..\temp.txt,更多信息请参考Path。

  • 不要使用下面的保留名称作为文件名:

    CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, 和 LPT9。同样也避免这些名称紧跟着后缀名,例如NUL.txt是不被建议使用的,更多信息参考Namespaces。

  • 不要使用.和空格作为文件和目录名的结尾,虽然底层文件系统可能支持这样的名称,但是Windows Shell和用户接口不支持。不过,可以明确指出的是,.可以作为名称的第一个字符,例如.temp

Short vs. Long Names

A long file name is considered to be any file name that exceeds the short MS-DOS (also called 8.3) style naming convention. When you create a long file name, Windows may also create a short 8.3 form of the name, called the 8.3 alias or short name, and store it on disk also. This 8.3 aliasing can be disabled for performance reasons either systemwide or for a specified volume, depending on the particular file system.

Windows Server 2008, Windows Vista, Windows Server 2003 and Windows XP: 8.3 aliasing cannot be disabled for specified volumes until Windows 7 and Windows Server 2008 R2.

On many file systems, a file name will contain a tilde (~) within each component of the name that is too long to comply with 8.3 naming rules.

Note

Not all file systems follow the tilde substitution convention, and systems can be configured to disable 8.3 alias generation even if they normally support it. Therefore, do not make the assumption that the 8.3 alias already exists on-disk.

To request 8.3 file names, long file names, or the full path of a file from the system, consider the following options:

To get the 8.3 form of a long file name, use the GetShortPathName function.
To get the long file name version of a short name, use the GetLongPathName function.
To get the full path to a file, use the GetFullPathName function.
On newer file systems, such as NTFS, exFAT, UDFS, and FAT32, Windows stores the long file names on disk in Unicode, which means that the original long file name is always preserved. This is true even if a long file name contains extended characters, regardless of the code page that is active during a disk read or write operation.

Files using long file names can be copied between NTFS file system partitions and Windows FAT file system partitions without losing any file name information. This may not be true for the older MS-DOS FAT and some types of CDFS (CD-ROM) file systems, depending on the actual file name. In this case, the short file name is substituted if possible.

短名 Vs 长名

长文件名被认为是超出短MS-DOS(也称为8.3)样式命名约定的任何文件名。当你创建一个长文件名,Windows可能会从名称中创建一个短8.3名称,名为8.3别名或者短名,也存储在磁盘上。根据特定的文件系统,可以由于性能原因在整个系统或者指定卷上禁用这个8.3别名。。

Windows Server 2008, Windows Vista, Windows Server 2003 and Windows XP: 在Windows 7 和Windows Server 2008 R2之前,都不可以对指定的卷禁用8.3别名。

在许多文件系统上,名称中的每个文件名太长以至于不符合8.3文件命名规则的部分将包含一个波浪字符(~)。

注意

不是所有的文件系统都遵循波浪符替代约定,而且即使系统支持也可以通过配置禁用。因为不要假设磁盘上一定存在8.3别名。

要请求8.3文件名,长文件名,或者系统上的文件的完整路径,有以下选择:

在新的文件系统上,例如NTFS, exFAT, UDFS, and FAT32,Widows,在磁盘上用Unicode编码存储长文件名,这意味着无论磁盘读写时正在使用的代码页是什么,又或者长文件名包含扩展字符,原始的长文件名一定会被存储下来。

使用长文件名的文件可以在NTFS文件系统分区和Windows FAT文件系统分区之间拷贝而不丢失任何文件名信息。但是在旧的MS-DOS FAT和一些CDFS (CD-ROM)文件系统之间就不行了,这取决于实际的文件名。在这种情况下,如果可能的话,将替换为短文件名

Paths

The path to a specified file consists of one or more components, separated by a special character (a backslash), with each component usually being a directory name or file name, but with some notable exceptions discussed below. It is often critical to the system’s interpretation of a path what the beginning, or prefix, of the path looks like. This prefix determines the namespace the path is using, and additionally what special characters are used in which position within the path, including the last character.

If a component of a path is a file name, it must be the last component.

Each component of a path will also be constrained by the maximum length specified for a particular file system. In general, these rules fall into two categories: short and long. Note that directory names are stored by the file system as a special type of file, but naming rules for files also apply to directory names. To summarize, a path is simply the string representation of the hierarchy between all of the directories that exist for a particular file or directory name.

路径

特定文件的路径有一个或者多个部分组成,通过特殊的字符(一个反斜杠)分隔,每个组件通常是一个目录名或者文件名,但是一些明显的异常情况接下来需要讨论。对于系统对路径的解析来说,路径的开头或前缀是什么通常是至关重要的。
此前缀确定路径使用的命名空间,以及在路径中的哪个位置使用特殊字符,包括最后一个字符。

如果路径的一个组成部分是文件名,那么这部分必须是最后一个组成部分。

路径的每个组成部分也将受到特定文件系统指定的最大长度的限制。通常来说,这些规则分为两类:短和长。请注意,文件系统将目录名存储为一种特殊类型的文件,但文件的命名规则也适用于目录名。总而言之,路径只是一个表示特定文件或目录名的所有目录之间层次结构的简单字符串。

Fully Qualified vs. Relative Paths

完全限定 vs. 相对路径

For Windows API functions that manipulate files, file names can often be relative to the current directory, while some APIs require a fully qualified path. A file name is relative to the current directory if it does not begin with one of the following:

  • A UNC name of any format, which always start with two backslash characters (“\”). For more information, see the next section.
  • A disk designator with a backslash, for example “C:” or “d:”.
  • A single backslash, for example, “\directory” or “\file.txt”. This is also referred to as an absolute path.

If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter. Note that the current directory may or may not be the root directory depending on what it was set to during the most recent “change directory” operation on that disk. Examples of this format are as follows:

  • “C:tmp.txt” refers to a file named “tmp.txt” in the current directory on drive C.
  • “C:tempdir\tmp.txt” refers to a file in a subdirectory to the current directory on drive C.

A path is also said to be relative if it contains “double-dots”; that is, two periods together in one component of the path. This special specifier is used to denote the directory above the current directory, otherwise known as the “parent directory”. Examples of this format are as follows:

  • “…\tmp.txt” specifies a file named tmp.txt located in the parent of the current directory.
  • “…\tmp.txt” specifies a file that is two directories above the current directory.
  • “…\tempdir\tmp.txt” specifies a file named tmp.txt located in a directory named tempdir that is a peer directory to the current directory.

Relative paths can combine both example types, for example “C:…\tmp.txt”. This is useful because, although the system keeps track of the current drive along with the current directory of that drive, it also keeps track of the current directories in each of the different drive letters (if your system has more than one), regardless of which drive designator is set as the current drive.

对于操作文件的Windows API函数,文件名通常可以相对于当前目录,然而某些API需要完全限定的路径。一个文件名如果不是以下面中的一个开头那么就是相对于当前目录:

  • 总是以两个反斜杠字符(\\)开头的一个任意格式的UNC名称。
  • 盘符和一个反斜杠字符,例如C:\或者d:\
  • 一个单独的反斜杠,例如\directory或者\file.txt。这种依然被解析为一个绝对路径

如果一个文件名只是以盘符开头,冒号后面没有反斜杠,则会将其解释为指向此盘符驱动器上的当前目录的的相对路径。请注意,当前目录可能是根目录,也可能不是根目录,这取决于它在该磁盘上最后一次的“更改目录”操作。该格式的示例如下:

  • C:tmp.txt表示在驱动器C上当前目录中名为tmp.txt的文件。
  • C:tempdir\tmp.txt表示驱动器C上当前目录中的tempdir子目录下名为tmp.txt的文件。

如果路径包含..,则也称为相对路径;也就是说,路径的一个组件中有两个.。此特殊指示符用于表示当前目录的上级的目录,也就是所谓的“父目录”。此格式的示例如下所示:

  • ..\tmp.txt特指一个位于当前目录的父目录中名为tmp.txt文件。
  • ..\..\tmp.txt特指当前目录的上两级目录中名为tmp.txt文件。
  • ..\tempdir\tmp.txt特指当前目录同级名为tempdir的目录中名为tmp.txt文件。

相对路径可以是上面两种示例的组合,例如C:..\tmp.txt。这一点很有用,因为尽管系统会跟踪当前驱动器以及该驱动器的当前目录,但无论将哪个驱动器标识符设置为当前驱动器,它依旧会跟踪每个不同驱动器号中的当前目录(如果系统有多个驱动器号)。

Maximum Path Length Limitation

In editions of Windows before Windows 10 version 1607, the maximum length for a path is MAX_PATH, which is defined as 260 characters. In later versions of Windows, changing a registry key or using the Group Policy tool is required to remove the limit. See Maximum Path Length Limitation for full details.

路径最大长度限制

在Windows 10 version 1607版本之前,路径的最大长度是MAX_PATH,即260个字符。在后来的Windows版本中,需要更改注册表项或使用组策略工具才能取消限制。具体信息见Maximum Path Length Limitation

Namespaces

There are two main categories of namespace conventions used in the Windows APIs, commonly referred to as NT namespaces and the Win32 namespaces. The NT namespace was designed to be the lowest level namespace on which other subsystems and namespaces could exist, including the Win32 subsystem and, by extension, the Win32 namespaces. POSIX is another example of a subsystem in Windows that is built on top of the NT namespace. Early versions of Windows also defined several predefined, or reserved, names for certain special devices such as communications (serial and parallel) ports and the default display console as part of what is now called the NT device namespace, and are still supported in current versions of Windows for backward compatibility.

命名空间

在Windows API中使用的命名空间约定有两大类,通常称为NT命名空间和Win32命名空间。NT命名空间。NT命名空间是被设计用来存放其他子系统和命名空间的最低级别命名空间,包括Win32子系统和Win32命名空间。POSIX是 Windows构建在NT命名空间之上的子系统的另一个示例。早期版本的Windows 还为某些特殊设备定义了几个预定义的或保留的名称,例如通信(串行和并行)端口和默认显示控制台,作为现在称为NT设备命名空间的一部分,并且在当前版本的Windows中仍受支持以实现向后兼容性。

Win32 File Namespaces

The Win32 namespace prefixing and conventions are summarized in this section and the following section, with descriptions of how they are used. Note that these examples are intended for use with the Windows API functions and do not all necessarily work with Windows shell applications such as Windows Explorer. For this reason there is a wider range of possible paths than is usually available from Windows shell applications, and Windows applications that take advantage of this can be developed using these namespace conventions.

For file I/O, the “\?” prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system. For example, if the file system supports large paths and file names, you can exceed the MAX_PATH limits that are otherwise enforced by the Windows APIs. For more information about the normal maximum path limitation, see the previous section Maximum Path Length Limitation.

Because it turns off automatic expansion of the path string, the “\?” prefix also allows the use of “…” and “.” in the path names, which can be useful if you are attempting to perform operations on a file with these otherwise reserved relative path specifiers as part of the fully qualified path.

Many but not all file I/O APIs support “\?”; you should look at the reference topic for each API to be sure.

Note that Unicode APIs should be used to make sure the “\?” prefix allows you to exceed the MAX_PATH

Win32文件命名空间

本节和下一节总结了Win32命名空间前缀和约定,并说明了它们的使用方法。请注意,这些示例只适用于Windows API函数,并不一定适用于 Windows shell应用程序,例如Windows资源管理器。由于这个原因,实际可用的路径范围要比Windows shell 应用程序通常可用的路径范围更广,并且可以利用这一点开发使用这些命名空间约定的Windows应用程序。

对于文件I/O而言,路径字符串的前缀\\?\告诉Windows API禁用所有字符串解析并且直接将后面的字符串发送给文件系统。例如,如果一个文件系统支持长路径、文件名,你可以超出Windows API强制规定的MAX_PATH限制。有关正常最大路径限制的更多信息,请参阅上一节路径最大长度限制

因为这关闭了路径字符串的自动扩展,\\?\前缀也允许在路径名中使用...,这对于在进行文件操作的完全限定路径中使用保留的相对路径说明符来说是非常有用的。

很多的文件I/O API支持\\?\,但并不是所有的API都支持。你应该查看每个API的参考文档来确认这一点。

注意,应该使用Unicode API来确保\\?\前缀可以用来超出MAX_PATH限制。

Win32 Device Namespaces

The “\.” prefix will access the Win32 device namespace instead of the Win32 file namespace. This is how access to physical disks and volumes is accomplished directly, without going through the file system, if the API supports this type of access. You can access many devices other than disks this way (using the CreateFile and DefineDosDevice functions, for example).

For example, if you want to open the system’s serial communications port 1, you can use “COM1” in the call to the CreateFile function. This works because COM1–COM9 are part of the reserved names in the NT namespace, although using the “\.” prefix will also work with these device names. By comparison, if you have a 100 port serial expansion board installed and want to open COM56, you cannot open it using “COM56” because there is no predefined NT namespace for COM56. You will need to open it using “\.\COM56” because “\.” goes directly to the device namespace without attempting to locate a predefined alias.

Another example of using the Win32 device namespace is using the CreateFile function with “\.\PhysicalDriveX” (where X is a valid integer value) or “\.\CdRomX”. This allows you to access those devices directly, bypassing the file system. This works because these device names are created by the system as these devices are enumerated, and some drivers will also create other aliases in the system. For example, the device driver that implements the name “C:” has its own namespace that also happens to be the file system.

APIs that go through the CreateFile function generally work with the “\.” prefix because CreateFile is the function used to open both files and devices, depending on the parameters you use.

If you’re working with Windows API functions, you should use the “\.” prefix to access devices only and not files.

Most APIs won’t support “\.”; only those that are designed to work with the device namespace will recognize it. Always check the reference topic for each API to be sure.

Win32设备命名空间

\\.\前缀将访问Win32设备命名空间而不是Win32文件命名空间。对于支持这种类型的访问的API ,这是无需通过文件系统直接访问物理磁盘和卷的方式。你可以通过这种方式(例如使用CreateFileDefineDosDevice函数)访问除磁盘之外的许多设备。

例如,如果你想访问系统的串行通讯端口1,你可以在调用CreateFile函数时使用COM1。这种方法之所以有效,是因为 COM1-COM9 是NT命名空间中保留名称的一部分,同时\\.\前缀也适用于这些设备名称。相对而言,如果你安装了一个100口的串口扩展板,想打开COM56,就不能用COM56打开,因为COM56没有在NT命名空间中预定义。你只能使用\\.\COM56打开它,因为\\.\会直接进入命名空间而不会尝试去寻找预定义的别名。

另一个使用Win32设备命名空间的例子是调用CreateFile函数时使用\\.\PhysicalDiskX(X是一个有效的整数)或者\\.\CdRomX。这样可以绕过文件系统直接访问这些设备。这种方式之所以有效,是因为系统在枚举这些设备时创建了这些设备名,有些驱动也会在系统中创建其他的别名。例如文件系统的设备驱动就拥有自己的命名空间但又创建了C:\

通过内部使用CreateFile函数的API通常使用\\.\前缀,因为CreateFile 是同时用于打开文件和设备的函数,具体用途取决于使用的参数。

如果你是用Windows API函数,当访问设备而非文件时应该使用\\.\前缀。

大多数API不支持\\.\,只有那些被设计用来运行在设备命名空间的API会识别它。记得每次使用API前检查它的文档说明。

NT Namespaces

There are also APIs that allow the use of the NT namespace convention, but the Windows Object Manager makes that unnecessary in most cases. To illustrate, it is useful to browse the Windows namespaces in the system object browser using the Windows Sysinternals WinObj tool. When you run this tool, what you see is the NT namespace beginning at the root, or “”. The subfolder called “Global??” is where the Win32 namespace resides. Named device objects reside in the NT namespace within the “Device” subdirectory. Here you may also find Serial0 and Serial1, the device objects representing the first two COM ports if present on your system. A device object representing a volume would be something like “HarddiskVolume1”, although the numeric suffix may vary. The name “DR0” under subdirectory “Harddisk0” is an example of the device object representing a disk, and so on.

To make these device objects accessible by Windows applications, the device drivers create a symbolic link (symlink) in the Win32 namespace, “Global??”, to their respective device objects. For example, COM0 and COM1 under the “Global??” subdirectory are simply symlinks to Serial0 and Serial1, “C:” is a symlink to HarddiskVolume1, “Physicaldrive0” is a symlink to DR0, and so on. Without a symlink, a specified device “Xxx” will not be available to any Windows application using Win32 namespace conventions as described previously. However, a handle could be opened to that device using any APIs that support the NT namespace absolute path of the format “\Device\Xxx”.

With the addition of multi-user support via Terminal Services and virtual machines, it has further become necessary to virtualize the system-wide root device within the Win32 namespace. This was accomplished by adding the symlink named “GLOBALROOT” to the Win32 namespace, which you can see in the “Global??” subdirectory of the WinObj browser tool previously discussed, and can access via the path “\?\GLOBALROOT”. This prefix ensures that the path following it looks in the true root path of the system object manager and not a session-dependent path.

NT 命名空间

这里也有一些允许使用NT命名空间约定的API,但在大多数情况下,Windows 对象管理器可以替代它。为了方便展示,我们使用Windows Sysinternals WinObj工具在系统对象浏览器中浏览 Windows命名空间。当运行此工具时,你会发现NT命名空间从根节点或者\.开始。Win32命名空间在名为Global??的子目录里。命名的设备对象在NT命名空间中名为Device的子目录里。这里你或许能找到系统中表示第一和第二个串口的设备对象Serial0Serial1。类似HarddiskVolume1这样的设备对象表示卷,只是数字后缀有所不同。例如Harddisk0子目录下的DR0是一个表示磁盘的设备对象,其他诸如此类。

为了让这些设备对象可以被Windows程序访问,设备驱动在Win32命名空间Global??中创建了一个符号链接到它们各自的设备对象。例如,Global??子目录下的COM1COM1只是简单的符号连接到了Serial0Serial1C:HarddiskVolume1的符号连接,Physicaldrive0DR0的符号连接,其他诸如此类。一个指定的设备Xxx如果没有符号链接是不能被任何Windows程序使用上文描述的Win32命名空间约定使用的。但是,可以使用任何支持\Device\Xxx格式的NT命名空间绝对路径的API打开该设备的句柄。

为了通过终端服务和虚拟机添加多用户支持,需要进一步在 Win32 命名空间内虚拟化系统范围的根设备。这是通过将名为GLOBALROOT的符号链接添加到Win32命名空间来完成的。你可以在之前讨论过的WinObj工具的Global??子目录中看到它。并且通过\\?\GLOBALROOT访问它。此前缀确保在系统对象管理器的真正根路径中查找它后面的路径,而不是从与会话相关的路径中查找。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值