应用层的控制程序的功能:创建,打开,关闭函数的主要操作:
(1)MiniDiskCreateMount
检测指定盘符是否存在
若不存在,创建盘符并关联盘符和设备
打开虚拟磁盘设备,发送自定义IOCTL_MINI_DISK_CREATE_FILE控制码
int MiniDiskCreateMount(char DeviceNumber,POPEN_FILE_INFORMATION pOpen,char DriverTarget)
{
……
//尝试打开指定卷
hDevice = CreateFile(
VolumeName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
NULL);
……
//创建虚拟磁盘设备,盘符和设备捆绑
if(!DefineDosDevice(DDD_RAW_TARGET_PATH,&VolumeName[4],DeviceName))
{
……
}
//打开虚拟磁盘设备
hDevice = CreateFile(
VolumeName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
NULL);
if(hDevice == INVALID_HANDLE_VALUE)
{
……
}
//发送IOCTL_MINI_DISK_CREATE_FILE控制码
if(!DeviceIoControl(
hDevice,
IOCTL_MINI_DISK_CREATE_FILE,
pOpen,
sizeof(OPEN_FILE_INFORMATION)+pOpen->FileNameLength - 1,
TempBuf,
sizeof(TempBuf)/sizeof(char),
&BytesReturned,
NULL))
{
……
}
……
}
int MiniDiskOpenMount(char DeviceNumber,POPEN_FILE_INFORMATION pOpen,char DriverTarget)
{
……
//尝试打开指定卷
hDevice = CreateFile(
VolumeName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
NULL);
//当前卷已经存在,需要更改盘符
if(hDevice != INVALID_HANDLE_VALUE)
{
……
}
……
//创建虚拟磁盘设备,盘符和设备捆绑
if(!DefineDosDevice(DDD_RAW_TARGET_PATH,&VolumeName[4],DeviceName))
{
……
}
//打开虚拟磁盘设备
hDevice = CreateFile(
VolumeName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
NULL);
if(hDevice == INVALID_HANDLE_VALUE)
{
……
}
//发送IOCTL_MINI_DISK_OPEN_FILE控制码
if(!DeviceIoControl(
hDevice,
IOCTL_MINI_DISK_OPEN_FILE,
pOpen,
sizeof(OPEN_FILE_INFORMATION)+pOpen->FileNameLength - 1,
TempBuf,
sizeof(TempBuf)/sizeof(char),
&BytesReturned,
NULL))
{
……
}
……
}
int MiniDiskUnMount(char DriverTarget)
{
……
//检测设备是否存在
hDevice = CreateFile(
VolumeName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
NULL);
if(hDevice == INVALID_HANDLE_VALUE)
{
……
}
//发送控制码FSCTL_LOCK_VOLUME
/*if(!DeviceIoControl(hDevice,FSCTL_LOCK_VOLUME,NULL,0,NULL,0,&BytesReturned,NULL))
{
……
}*/
//发送控制码IOCTL_MINI_DISK_CLOSE_FILE
if(!DeviceIoControl(hDevice,IOCTL_MINI_DISK_CLOSE_FILE,NULL,0,Buf,sizeof(Buf)/sizeof(char),&BytesReturned,NULL))
{
……
}
//发送控制码FSCTL_DISMOUNT_VOLUME
if(!DeviceIoControl(hDevice,FSCTL_DISMOUNT_VOLUME,NULL,0,NULL,0,&BytesReturned,NULL))
{
……
}
//发送控制码FSCTL_UNLOCK_VOLUME
/*if(!DeviceIoControl(hDevice,FSCTL_UNLOCK_VOLUME,NULL,0,NULL,0,&BytesReturned,NULL))
{
……
}*/
//删除盘符
if(!DefineDosDevice(DDD_REMOVE_DEFINITION,&VolumeName[4],NULL))
{
……
}
……
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)