/* 贴出源码 这是一个 查找 、设置挂载点的程序
// 并且通过挂载点获取卷设备名
// 运行到 GetMountPoint(); 突然报错,不知道怎么回事
// 平台: windows XP vc6.0 + windows SDK
*/
// 源代码开始
/*---------------------------------------------------*/
/*预编译声明*/
#define _WIN32_WINNT 0x0501
/*头文件*/
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#define BUFSIZE MAX_PATH
#define FILESYSNAMEBUFSIZE MAX_PATH
/******************
// processVolumeMountPoint
// 功能:列举挂载点
*******************/
BOOL ProcessVolumeMountPoint (HANDLE hPt,
TCHAR *PtBuf,
DWORD dwPtBufSize,
TCHAR *Buf)
{
BOOL bFlag; //结果
TCHAR Path[BUFSIZE]; //全路径
TCHAR Target[BUFSIZE]; //挂载点设备
printf("\t Volume mount point found is \"%s\"\n",PtBuf);
lstrcpy(Path,Buf);
lstrcat(Path,PtBuf);
bFlag = GetVolumeNameForVolumeMountPoint(
Path, Target, BUFSIZE);
if(bFlag)
{
printf("\tAttemp to get volume name for %s failed.\n",Path);
}
else
{
printf("\tTarge of the volume mount point is %s.\n",Target);
}
bFlag = FindNextVolumeMountPoint ( hPt, PtBuf, dwPtBufSize );
return (bFlag);
}
/*********************
//ProcessVolume
//功能:判断卷类型,列举挂载点
*********************/
BOOL ProcessVolume (HANDLE hVol, TCHAR *Buf, DWORD iBufSize)
{
BOOL bFlag; //返回标志
HANDLE hPt; //卷句柄
TCHAR PtBuf[BUFSIZE]; //挂载点路径
DWORD dwSysFlags; //文件系统标记
TCHAR FileSysNameBuf[FILESYSNAMEBUFSIZE];
printf("Volume found is \"%s\"\n", Buf);
//是否为NTFS
GetVolumeInformation (Buf, NULL, 0, NULL, NULL,
&dwSysFlags,
FileSysNameBuf,
FILESYSNAMEBUFSIZE);
if( !(dwSysFlags & FILE_SUPPORTS_REPARSE_POINTS) )
{
printf("\tThis file system does not support volume mount point.\n");
}
else
{
//本卷中的挂载点
hPt = FindFirstVolumeMountPoint(
Buf, //卷的路径
PtBuf, //挂载点路径
BUFSIZE
);
if( hPt == INVALID_HANDLE_VALUE)
{
printf("\t No volume mount point found !\n");
}
else
{
//处理挂载点
bFlag = ProcessVolumeMountPoint (hPt,
PtBuf,
BUFSIZE,
Buf);
//循环
while(bFlag)
bFlag = ProcessVolumeMountPoint (hPt, PtBuf, BUFSIZE, Buf);
//结束
FindVolumeMountPointClose( hPt );
}
}
//下一个
bFlag = FindNextVolume(hVol,
Buf, iBufSize);
return (bFlag);
}
/********************
// int GetMountPoint(void)
//功能:获取挂载点
*********************/
int GetMountPoint(void)
{
TCHAR buf[BUFSIZE]; //卷标识符
HANDLE hVol; //卷句柄
BOOL bFlag; //结果标志
printf("Volume mount points info of this computer:\n");
//打开卷
hVol = FindFirstVolume ( buf, BUFSIZE );
if(hVol == INVALID_HANDLE_VALUE)
{
printf("NO volume found!\n");
return (-1);
}
bFlag = ProcessVolume (hVol, buf, BUFSIZE);
while(bFlag)
{
bFlag = ProcessVolume (hVol, buf, BUFSIZE);
}
bFlag = FindVolumeClose ( hVol);
return (bFlag);
}
/*******************
// void Usage (PCHAR argv)
//功能:使用方法
********************/
void Usage(PCHAR argv)
{
printf("\n\n\t%s,mount a volume at a mount point.\n",argv);
printf("\tFor exaple,\"mount D:\\mnt\\drives\\ E:\\\"\n");
}
/*******************
//main
//功能:入口函数
*******************/
int main(int argc, PCHAR argv[])
{
BOOL bFlag;
CHAR Buf[BUFSIZE];
if(argc != 3)
{
GetMountPoint();
Usage(argv[0]);
return (-1);
}
bFlag = GetVolumeNameForVolumeMountPoint(
argv[2], //输入挂载点或目录
Buf, //输出卷名
BUFSIZE
);
if( bFlag != TRUE )
{
printf("Retrieeving volume name for %s failed.\n",argv[2]);
return (-2);
}
printf("Volume name of %s is %s\n",argv[2],Buf);
bFlag = SetVolumeMountPoint(
argv[1], //挂载点
Buf //需要挂载的卷
);
if(!bFlag)
{
printf("Attempt to mount %s at %s failed.error code is\n",
argv[2], argv[1], GetLastError());
}
return (bFlag);
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课