|
2#

楼主 |
发表于 2007-3-30 11:47:57
|
只看该作者
INFO: 了解设备名和符号链接
INFO: 了解设备名和符号链接
察看本文应用于的产品
注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。
文章编号 : 235128
最后修改 : 2006年11月21日
修订 : 4.1
本页
概要
更多信息
内部 NT 设备名称
MS-DOS 设备名
符号链接
概要
像硬盘、 串行端口, 和并行端口有内部 Windows NT 设备名称并且可能选择设备有一个 MS-DOS 设备名。 这些名称位于 WindowsNT 对象管理器的空间。 虽然 WindowsNT 本身和内核模式驱动程序使用 NT 设备名称, Win 32 程序必须使用 MS-DOS 设备名。 MS-DOS 设备名称是符号链接到基础 NT 设备名称。 本文介绍设备名称、 符号链接, 和 Win 32 程序如何创建和删除符号链接。
回到顶端
更多信息
内部 NT 设备名称
与 WindowsNT 的对象管理器, 内核模式设备驱动程序加载时其初始化例程注册设备名称。 此名称被称为 WindowsNT 设备名。 WindowsNT 内核模式驱动程序和内核模式组件通过其 Windows NT 设备名称引用设备。 这些名称格式, 如 \Device\CDRom0 和 \Device\Serial0, 也是对象管理器空间 \device 目录中存在。
Win 32 程序无法使用内部 Windows NT 设备名称来访问设备因为 Win 32 子系统和 Win32API 需要更熟悉驱动器号和 MS-DOS 设备名 (如 COM 答, C, 1,:, 和 LPT 1: 虽然 Win 32 程序可能不使用 Windows NT 设备名称, 但是, 它们可定义和删除 MS-DOS 设备名来访问设备。 它们可还检索内部 Windows NT 设备名称与 MS-DOS 设备名称。 描述如何执行此于本文 " SymbolicLinks " 部分中示例代码。
PlatformSDK 中以获得更好地理解是如何对象管理跟踪名称对设备和其他系统对象, 您可以查看用 WinObj.exe 工具对象管理器空间
回到顶端
MS-DOS 设备名
Win 32 程序使用设备 (如驱动器、 串行端口, 和并行端口通过其 MS-DOS 设备名。 对于磁盘驱动器, 这些是如答和 C 驱动器号。 串行和并行端口对于这些是像 COM 1 名称:, COM 2 和 LPT 1,:: 像 Windows NT 设备名称, 这些名称位于对象管理器空间, 但中 ? \ 目录, 对 Win 32 用户模式程序这是可见。
什么使 MS-DOS 设备名不同于 Windows NT 设备名称是由 WindowsNT 内核或内核模式驱动程序不使用它们。 其目的是为了以便 Win 32 程序可以使用熟悉驱动器号和设备名 ; Windows NT 设备名称引用:, 访问 WindowsNT 设备例如, COM 1。 因此, MS-DOS 设备名被称为 " 符号链接 " 到 Windows NT 设备名称。 也就是说, 一个 MS-DOS 设备名称存在对象管理器中的 \ ? 目录和指向对象管理器 \device 目录中 WindowsNT 设备名称。
每个 MS-DOS 设备名可以指向至多个内部 WindowsNT 设备。 但是, 多个 MS-DOS 设备名可以指向单个设备。 例如, C 可只指向单个分区, 时它是可以让两个或多个驱动器号, 如 D: 和 Z:, E, 指向同一分区。
回到顶端
符号链接
在重新启动计算机保持因为它们是存储在注册表中由系统创建符号链接。 程序可能也创建符号链接以 DefineDosDevice()。 这些链接仅到计算机启动或关闭除非更新注册表项包含链接信息是有效。
在可以有多个符号链接到 Windows NT 设备名称。 但是, 仅 MS-DOS 设备名称, 跨系统最初分配给内部 WindowsNT 设备保持重新启动。 因此, 是要创建其他符号链接, 分配到一个 CD-ROM 驱动器, 多个驱动器号可能但是其他驱动器号只保留, 直到计算机被关闭或重新。
Win 32 程序可检索 Windows NT 设备名称与特定 MS-DOS 设备名称相关联通过调用 QueryDosDevice() 与 MS-DOS 设备名称。 下面是该如何执行此代码: char szNtDeviceName[MAX_PATH];
if (QueryDosDevice ("C:", szNtDeviceName, MAX_PATH))
{
printf ("C: is linked to %s\n", szNtDeviceName);
}
Win 32 程序可以创建并通过调用 DefineDosDevice() 删除符号链接。 若要创建符号链接, DefineDosDevice 调用带有 DDD_RAW_TARGET_PATH 标志。 要删除符号链接, 以 DDD_REMOVE_DEFINITION 和 DDD_RAW_TARGET_PATH 调用。 下列示例程序说明两个操作: /*
DDD
This sample shows how to associate an MS-DOS device name with a
Windows NT device name. The association is a symbolic link between
device names stored in the Object Manager's namespace. Applications
use the MS-DOS device name, but Windows NT and kernel-mode drivers
use the Windows NT device name.
Usage:
ddd <MS-DOS Device Name> <NT Device Name>
ddd -r <MS-DOS Device Name>
NOTE: If the MS-DOS device name is a driver letter, the trailing
backlash is not accepted by DefineDosDevice or QueryDosDevice.
NOTE: The MS-DOS device name is defined only until the computer is
restarted.
To make the drive letter associations permanent on Window NT 4.0, you
have to update HKEY_LOCAL_MACHINE\SYSTEM\DISK\Information. However,
the contents of the value are undocumented.
On Windows 2000, you have to use the Volume Mount Point APIs.
*/
#define WIN32_LEAN_AND_MEAN /* Reduce number of system headers parsed */
/* during build. */
#include <windows.h>
#include <stdio.h>
void main (int argc, char **argv)
{
char * pszDosDeviceName,
* pszNtDeviceName;
bool fRemoveDeviceName = false;
bool fResult;
/*
Command-line parsing.
1) Make sure correct number of arguments are supplied.
2) See if you should add or remove the MS-DOS Device Name.
*/
if (argc != 3)
{
printf("\nusage: %s <DOS device name> <NT device name> to add\n",
argv[0]);
printf("usage: %s [-r] <DOS device name> to remove\n",
argv[0]);
printf("\n\texample: %s d: \\device\\cdrom0\n", argv[0]);
return;
}
fRemoveDeviceName = !lstrcmpi (argv[1], "-r");
/* Now, add/remove the DOS device name. */
if (fRemoveDeviceName)
{
/*
Remove the MS-DOS device name. First, get the name of the Windows
NT device from the symbolic link, then delete the symbolic link.
*/
pszDosDeviceName = argv[2];
pszNtDeviceName = (char *)LocalAlloc (LPTR, MAX_PATH);
fResult = QueryDosDevice (pszDosDeviceName, pszNtDeviceName,
MAX_PATH);
if (fResult)
{
fResult = DefineDosDevice (DDD_RAW_TARGET_PATH|
DDD_REMOVE_DEFINITION|
DDD_EXACT_MATCH_ON_REMOVE,
pszDosDeviceName, pszNtDeviceName);
}
if (!fResult)
printf("error %lu: could not remove %s\n",
GetLastError(), pszDosDeviceName);
LocalFree (pszNtDeviceName);
}
else
{
/* Add the DOS device name */
pszDosDeviceName = argv[1];
pszNtDeviceName = argv[2];
fResult = DefineDosDevice (DDD_RAW_TARGET_PATH, pszDosDeviceName,
pszNtDeviceName);
if (!fResult)
printf("error %lu: could not link %s to %s\n",
GetLastError(), pszDosDeviceName, pszNtDeviceName);
}
} |
|