Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1701073
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2011-04-13 11:50:19

The code example in this topic shows you how to add or remove persistent drive letter assignments. These drive letter assignments persist through system shutdown. For more information, see .

The code example uses the following functions: , , , and .


 

  1. /* DLEDIT -- Drive Letter Assignment Editor Platforms: This program requires Windows 2000 or later. Command-line syntax: DLEDIT <drive letter> <device name> -- Adds a drive letter DLEDIT -r <drive letter> -- Removes a drive letter Command-line examples: If E: refers to the CD-ROM drive, use the following commands to make F: point to the CD-ROM drive instead. DLEDIT -r E:\ DLEDIT F:\ \Device\CdRom0 *****************************************************************
  2.         WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  3.            This program will change drive letter assignments, and the
  4.            changes persist through reboots. Do not remove drive letters
  5.            of your hard disks if you do not have this program on floppy
  6.            disk or you might not be able to access your hard disks
  7.         *****************************************************************
  8.         */
  9.         #ifdef _WIN32_WINNT
  10.         #undef _WIN32_WINNT
  11.         #define _WIN32_WINNT 0x0500
  12.         #endif
  13.         #ifdef NTDDI_VERSION
  14.         #undef NTDDI_VERSION
  15.         #define NTDDI_VERSION 0x05000000
  16.         #endif
  17.         #include <Windows.h>
  18.         #include <stdio.h>
  19.         #include <tchar.h>
  20.         #if defined (DEBUG)
  21.            static void DebugPrint (LPCTSTR pszMsg, DWORD dwErr);
  22.            #define DEBUG_PRINT(pszMsg, dwErr) DebugPrint(pszMsg, dwErr)
  23.         #else
  24.            #define DEBUG_PRINT(pszMsg, dwErr) NULL
  25.         #endif
  26.         #pragma warning (disable : 4800)
  27.         void PrintHelp (LPCTSTR pszAppName);
  28.         /*--------------------------------------------------------------------
  29.         The main function is the main routine. It parses the command-line
  30.         arguments and either removes or adds a drive letter.
  31.         Parameters
  32.            argc
  33.               Count of the command-line arguments
  34.            argv
  35.               Array of pointers to the individual command-line arguments
  36.         --------------------------------------------------------------------*/
  37.         void _tmain (int argc, TCHAR *argv[])
  38.         {
  39.            TCHAR * pszDriveLetter,
  40.                 * pszNTDevice,
  41.                 * pszOptions;
  42.            TCHAR szUniqueVolumeName[MAX_PATH];
  43.            TCHAR szDriveLetterAndSlash[4];
  44.            TCHAR szDriveLetter[3];
  45.            BOOL fRemoveDriveLetter;
  46.            BOOL fResult;
  47.            if (argc != 3)
  48.            {
  49.               PrintHelp(argv[0]);
  50.               return;
  51.            }
  52.            // Use the command line to see if user wants to add or remove the
  53.            // drive letter. Do this by looking for the -r option.
  54.            fRemoveDriveLetter = !lstrcmpi (argv[1], TEXT("-r"));
  55.            if (fRemoveDriveLetter)
  56.            {
  57.               // User wants to remove the drive letter. Command line should
  58.               // be: dl -r <drive letter>
  59.               pszOptions = argv[1];
  60.               pszDriveLetter = argv[2];
  61.               pszNTDevice = NULL;
  62.            }
  63.            else
  64.            {
  65.               // User wants to add a drive letter. Command line should be:
  66.               // dl <drive letter> <NT device name>
  67.               pszOptions = NULL;
  68.               pszDriveLetter = argv[1];
  69.               pszNTDevice = argv[2];
  70.            }
  71.            // GetVolumeNameForVolumeMountPoint, SetVolumeMountPoint, and
  72.            // DeleteVolumeMountPoint require drive letters to have a trailing
  73.            // backslash. However, DefineDosDevice requires that the trailing
  74.            // backslash be absent. So, use:
  75.            //
  76.            // szDriveLetterAndSlash for the mounted folder functions
  77.            // szDriveLetter for DefineDosDevice
  78.            //
  79.            // This way, command lines that use a: or a:\
  80.            // for drive letters can be accepted without writing back to the original command-
  81.            // line argument.
  82.            szDriveLetter[0] = pszDriveLetter[0];
  83.            szDriveLetter[1] = TEXT(':');
  84.            szDriveLetter[2] = TEXT('\0');
  85.            szDriveLetterAndSlash[0] = pszDriveLetter[0];
  86.            szDriveLetterAndSlash[1] = TEXT(':');
  87.            szDriveLetterAndSlash[2] = TEXT('\\');
  88.            szDriveLetterAndSlash[3] = TEXT('\0');
  89.            // Now add or remove the drive letter.
  90.            if (fRemoveDriveLetter)
  91.            {
  92.               fResult = DeleteVolumeMountPoint (szDriveLetterAndSlash);
  93.               if (!fResult)
  94.                  _tprintf(TEXT("error %lu: couldn't remove %s\n"),
  95.                         GetLastError(), szDriveLetterAndSlash);
  96.            }
  97.            else
  98.            {
  99.               // To add a drive letter that persists through reboots, use
  100.               // SetVolumeMountPoint. This requires the volume GUID path
  101.               // of the device to which the new drive letter will refer.
  102.               // To get the volume GUID path, use
  103.               // GetVolumeNameForVolumeMountPoint; it requires the drive
  104.               // letter to already exist. So, first define the drive
  105.               // letter as a symbolic link to the device name. After
  106.               // you have the volume GUID path the new drive letter will
  107.               // point to, you must delete the symbolic link because the
  108.               // mount manager allows only one reference to a device at a
  109.               // time (the new one to be added).
  110.               fResult = DefineDosDevice (DDD_RAW_TARGET_PATH, szDriveLetter,
  111.                                          pszNTDevice);
  112.               if (fResult)
  113.               {
  114.                   // If GetVolumeNameForVolumeMountPoint fails, then
  115.                   // SetVolumeMountPoint will also fail. However,
  116.                   // DefineDosDevice must be called to remove the temporary symbolic link.
  117.                   // Therefore, set szUniqueVolume to a known empty string.
  118.                  if (!GetVolumeNameForVolumeMountPoint (szDriveLetterAndSlash,
  119.                           szUniqueVolumeName,
  120.                           MAX_PATH))
  121.                  {
  122.                     DEBUG_PRINT(TEXT("GetVolumeNameForVolumeMountPoint failed"),
  123.                                 GetLastError());
  124.                     szUniqueVolumeName[0] = '\0';
  125.                  }
  126.                  fResult = DefineDosDevice (
  127.                               DDD_RAW_TARGET_PATH|DDD_REMOVE_DEFINITION|
  128.                               DDD_EXACT_MATCH_ON_REMOVE, szDriveLetter,
  129.                               pszNTDevice);
  130.                  if (!fResult)
  131.                     DEBUG_PRINT(TEXT("DefineDosDevice failed"),
  132.                                 GetLastError());
  133.                  fResult = SetVolumeMountPoint (szDriveLetterAndSlash,
  134.                                 szUniqueVolumeName);
  135.                  if (!fResult)
  136.                     _tprintf (TEXT("error %lu: could not add %s\n"),
  137.                               GetLastError(),
  138.                             szDriveLetterAndSlash);
  139.               }
  140.            }
  141.         }
  142.         /*-------------------------------------------------------------------
  143.         The PrintHelp function prints the command-line usage help.
  144.         Parameters
  145.            pszAppName
  146.               The name of the executable. Used in displaying the help.
  147.         -------------------------------------------------------------------*/
  148.         void PrintHelp (LPCTSTR pszAppName)
  149.         {
  150.            _tprintf(
  151.                TEXT("Adds/removes a drive letter assignment for a device.\n\n"));
  152.            _tprintf(
  153.                TEXT("Usage: %s add a drive letter\n"), pszAppName);
  154.            _tprintf(
  155.                TEXT(" %s -r remove a drive letter\n\n"), pszAppName);
  156.            _tprintf(
  157.                TEXT("Example: %s e:\\ \\Device\\CdRom0\n"), pszAppName);
  158.            _tprintf(
  159.                TEXT(" %s -r e:\\\n"), pszAppName);
  160.         }
  161.         #if defined (DEBUG)
  162.         /*--------------------------------------------------------------------
  163.         The DebugPrint function prints a string to STDOUT.
  164.         Parameters
  165.            pszMsg
  166.               The string to be printed to STDOUT.
  167.            dwErr
  168.               The error code; usually obtained from GetLastError. If dwErr is
  169.               zero, no error code is added to the error string. If dwErr is
  170.               nonzero, the error code will be printed in the error string.
  171.         --------------------------------------------------------------------*/
  172.         void DebugPrint (LPCTSTR pszMsg, DWORD dwErr)
  173.         {
  174.            if (dwErr)
  175.               _tprintf(TEXT("%s: %lu\n"), pszMsg, dwErr);
  176.            else
  177.               _tprintf(TEXT("%s\n"), pszMsg);
  178.         }
  179.         #endif
阅读(913) | 评论(0) | 转发(1) |
0

上一篇:Displaying Volume Paths

下一篇:qt QSetting

给主人留下些什么吧!~~