[WinAPI] API 6 [操作驱动器挂载点]

 

 

驱动器挂载点,又可以称作卷挂载点。挂载点实际上是操作系统或者用户设置的,用来进入一个逻辑驱动器或者卷的入口。在设置了卷的挂载点后,用户或者应用程序可以使用卷标或者指定的挂载点来进入卷。比如在“C:\”下设置了一个“E\”:卷的挂载点mnt,那么打开“E:\”和打开“C:\mnt”实际上都是进入“E:\”卷。
一般可以用FindFirstVolumeMountPoint系列的API来找到一个卷的所有挂载点;用GetVolumeNameForVolumeMountPoint来获取指定挂载点所指向的卷名,卷名形式为"\\?\Volume{GUID}\”;用SetVolumeMountPoint来设置新的挂载点。

 

通过系统的磁盘管理功能可以设置卷的挂载点:

◇“我的电脑”图标右键菜单中选择“管理”。
◇弹出“计算机管理”窗口,选择“磁盘管理”。
◇选中需要挂载的卷,在右键菜单中选择“更改驱动 器名和路径”。
◇在弹出的对话框中单击“添加”按钮,选择“装入 以下空白NTFS文件夹”。
◇选择需要将卷挂载入的文件夹(空白),单击“确定”按钮。
◇卷就被装入文件夹中,之后就可以和访问文件夹一个访问这个卷了。

 

(1)FindFirstVolumeMountPoint.
获取指定卷的第一个挂载点

◇参数
lpszRootPathName:输入参数,指定要查找的卷名,必须以反斜杠结尾。
lpszVolumeMountPoint:输出参数,找到的第一个挂载点。
cchBufferLength.输入参数,用来储存输出挂载点的缓存的大小。
◇返回值
返回HANDLE值,为一个查找句柄,FindNextVolumeMountPoint用该句柄查找下一个挂载点。错误时值为INVALLD HANDLE VALUE,以用GetLastError()函数获取更详细的错误信息。


(2) FindNextVolumeMountPoint
查找指定卷的后继挂载点

◇参数
hFindVolumeMountPoint:输入参数,查找句柄,由FindFirstVolumeMountPoint获取。
lpszVolumeMountPoint:输出参数,找到的后继挂载点。
cchBufferLength:输入参数,用来储存输出挂载点的缓存的大小。
◇返回值
返回BOOL值,表示查找是否成功,失败并且GetLastError函数返回ERROR NO_ MORE
FILE$代码时表示已经查找完所有挂载点。


(3)FindVolumeMountPointClose.
关闭FindVolumeMountPointClose打开的卷句柄

◇参数
hFindVolumeMountPoint:要关闭的挂载点查找句柄。
◇返回值


(4)GetVolumeNameForVolumeMountPoint。
根据指定的挂载点获取相应的卷设备名

◇参数
lpszVolumeMountPoint:输入参数,指定需要查找挂载点或者根目录,以反斜杠结束。
lpszVolumeName:输出参数,挂载点对应的卷设备名,形式为“\\?\Volume{GUID}\”。

cchBufferLength:输入参数,用来储存输出设备名的缓存大小。
◇返回值
返回BOOL值,表示函数是否成功,同样可以用GetLastError函数获取更详细的错误信息。


(5)SetVolumeMountPc
将指定卷挂载到指定挂载点处

◇参数
lpszVolumeMountPoint:输入参数,指定的挂载点,挂载点必须为一个根路径或者一个在现有卷上的路径,必须以反斜杠结束。
lpszVolumeName:输入参数,卷设备名,形式为“\\?\Volume{GUID}\”。
◇返回值
返回BOOL值表示函数是否成功,同样可以用GetLastError()函数获取更详细的错误信息。

 

  1 #define _WIN32_WINNT 0x0501
  2 #include <windows.h>
  3 #include <stdio.h>
  4 #include <tchar.h>
  5 #define BUFSIZE            MAX_PATH
  6 #define FILESYSNAMEBUFSIZE MAX_PATH
  7 
  8 /* ************************************
  9 * ProcessVolumeMountPoint
 10 * 功能 列举挂载点
 11 **************************************/
 12 BOOL ProcessVolumeMountPoint (HANDLE hPt,
 13                               TCHAR *PtBuf, DWORD dwPtBufSize,
 14                               TCHAR *Buf)
 15 {
 16     BOOL bFlag;      // 结果
 17     TCHAR Path[BUFSIZE]; // 全路径
 18     TCHAR Target[BUFSIZE];   // 挂载点设备
 19 
 20     printf ("\tVolume mount point found is \"%s\"\n", PtBuf);
 21 
 22     lstrcpy (Path, Buf);
 23     lstrcat (Path, PtBuf);
 24 
 25     bFlag = GetVolumeNameForVolumeMountPoint(Path,Target,BUFSIZE);
 26 
 27     if (!bFlag)
 28         printf ("\tAttempt to get volume name for %s failed.\n", Path);
 29     else 
 30         printf ("\tTarget of the volume mount point is %s.\n", Target);
 31 
 32     bFlag = FindNextVolumeMountPoint(hPt,PtBuf,dwPtBufSize);
 33     return (bFlag);
 34 }
 35 /* ************************************
 36 * ProcessVolume
 37 * 功能 判断卷类型,列举挂载点
 38 **************************************/
 39 BOOL ProcessVolume (HANDLE hVol, TCHAR *Buf, DWORD iBufSize)
 40 {
 41     BOOL bFlag;           // 返回标志
 42     HANDLE hPt;           // 卷句柄
 43     TCHAR PtBuf[BUFSIZE]; // 挂载点路径
 44     DWORD dwSysFlags;     // 文件系统标记
 45     TCHAR FileSysNameBuf[FILESYSNAMEBUFSIZE];
 46 
 47     printf ("Volume found is \"%s\".\n", Buf);
 48 
 49     // 是否NTFS
 50     GetVolumeInformation( Buf, NULL, 0, NULL, NULL,
 51         &dwSysFlags, FileSysNameBuf, 
 52         FILESYSNAMEBUFSIZE);
 53 
 54     if (! (dwSysFlags & FILE_SUPPORTS_REPARSE_POINTS)) 
 55     {
 56         printf ("\tThis file system does not support volume mount points.\n");
 57     } 
 58     else 
 59     {
 60         // 本卷中的挂载点
 61         hPt = FindFirstVolumeMountPoint(
 62             Buf, // 卷的跟跟踪
 63             PtBuf, // 挂载点路径
 64             BUFSIZE 
 65             );
 66 
 67         if (hPt == INVALID_HANDLE_VALUE)
 68         {
 69             printf ("\tNo volume mount points found!\n");
 70         } 
 71         else 
 72         {
 73             // 处理挂载点
 74             bFlag = ProcessVolumeMountPoint (hPt, 
 75                 PtBuf, 
 76                 BUFSIZE, 
 77                 Buf);
 78             // 循环
 79             while (bFlag) 
 80                 bFlag = 
 81                 ProcessVolumeMountPoint (hPt, PtBuf, BUFSIZE, Buf);
 82             // 结束
 83             FindVolumeMountPointClose(hPt);
 84         }
 85     }
 86 
 87     // 下一个
 88     bFlag = FindNextVolume(
 89         hVol, Buf, iBufSize);
 90 
 91     return (bFlag); 
 92 }
 93 /* ************************************
 94 * int GetMountPoint(void)
 95 * 功能 获取挂载点
 96 **************************************/
 97 int GetMountPoint(void)
 98 {
 99     TCHAR buf[BUFSIZE];   // 卷标识符
100     HANDLE hVol;      // 卷句柄
101     BOOL bFlag;      // 结果标志
102 
103     printf("Volume mount points info of this computer:\n\n");
104     // 打开卷
105     hVol = FindFirstVolume (buf, BUFSIZE );
106     if (hVol == INVALID_HANDLE_VALUE)
107     {
108         printf ("No volumes found!\n");
109         return (-1);
110     }
111 
112     bFlag = ProcessVolume (hVol, buf, BUFSIZE);
113     while (bFlag) 
114     {
115         bFlag = ProcessVolume (hVol, buf, BUFSIZE);
116     }
117 
118     bFlag = FindVolumeClose( hVol );
119     return (bFlag);
120 }
121 
122 /* ************************************
123 * void Usage (PCHAR argv)
124 * 功能 使用方法
125 **************************************/
126 void Usage (PCHAR argv)
127 {
128     printf( "\n\n\t%s, mount a volume at a mount point.\n", argv );
129     printf( "\tFor example, \"mount D:\\mnt\\drives\\ E:\\\"\n" );
130 }
131 
132 /* ************************************
133 * main
134 * 功能 入口函数
135 **************************************/
136 int main( int argc, PCHAR argv[] )
137 {
138     BOOL bFlag;
139     CHAR Buf[BUFSIZE];    
140 
141     if( argc != 3 ) 
142     {
143         GetMountPoint();
144         Usage( argv[0] );
145         return( -1 );
146     }
147 
148     bFlag = GetVolumeNameForVolumeMountPointA(
149         argv[2],   // 输入挂载点或目录
150         Buf,     // 输出卷名
151         BUFSIZE
152         );
153 
154     if (bFlag != TRUE) 
155     {
156         printf( "Retrieving volume name for %s failed.\n", argv[2] );
157         return (-2);
158     }
159 
160     printf( "Volume name of %s is %s\n", argv[2], Buf );
161     bFlag = SetVolumeMountPointA(
162         argv[1], // 挂载点
163         Buf    // 需要挂载的卷
164         );
165 
166     if (!bFlag)
167     {
168         printf ("Attempt to mount %s at %s failed. error code is\n", 
169             argv[2], argv[1], GetLastError());
170     }
171 
172     return (bFlag);
173 }

 

 

posted @ 2014-03-06 22:23  beautifulzzzz  阅读(1707)  评论(0编辑  收藏  举报