INFO: Understanding Device Names and Symbolic LinksID: Q235128
|
Devices like hard disks, serial ports, and parallel ports have an internal Windows NT device name and may optionally have an MS-DOS device name. These names are located in the Windows NT Object Manager's namespace. While Windows NT itself and kernel-mode drivers use NT device names, Win32 programs must use the MS-DOS device names. The MS-DOS device name is a symbolic link to the underlying NT device name. This article explains device names, symbolic links, and how Win32 programs can create and remove symbolic links.
char szNtDeviceName[MAX_PATH];
if (QueryDosDevice ("C:", szNtDeviceName, MAX_PATH))
{
printf ("C: is linked to %s\n", szNtDeviceName);
}
Win32 programs can create and delete symbolic links by calling
DefineDosDevice(). To create a symbolic link, call DefineDosDevice with the
DDD_RAW_TARGET_PATH flag. To remove a symbolic link, call it with the
DDD_REMOVE_DEFINITION and DDD_RAW_TARGET_PATH flags. The following sample
program demonstrates both of these operations:
/*
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);
}
}
Additional query words: DOS DEVICE
Keywords : kbKernBase kbSDKPlatform kbSDKWin32 kbDSupport
Version : WINDOWS:; winnt:4.0
Platform : WINDOWS winnt
Issue type : kbinfo
Last Reviewed: June 24, 1999