以下代码能够卸除D盘的卷,D盘没有读写操作后先将其锁定,然后再将卷卸下,之后打开我的电脑后不能再访问D盘(但是我的电脑中依然能看见“驱动器D”)。但是我想进一步删除盘符D(即在我的电脑中不能再看见D盘),而且能再将D盘装配上去,且里面的数据不能丢失,如何实现呢? #include <iostream.h>
#include <windows.h>
#include <WinIoCtl.h>
void main()
{
HANDLE hDevice;//handle to the drive to be examined
BOOL lockResult;//lock results flag
BOOL dismountResult;//dismount results flag
BOOL unlockResult;//unlock results flag
DWORD cbReturned;//discard results
DWORD dwError;
cout<<"Dismount drive D, Y or N?";
char chDismount;
cin>>chDismount;
if(chDismount == 'Y' || chDismount == 'y')
{
//打开卷
hDevice = CreateFile("\\.\D:",//drive to open
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,//share mode
NULL, //default security attributes
OPEN_EXISTING,//disposition
0, //file attributes.
NULL); // don't copy any file's attributes
if(hDevice == INVALID_HANDLE_VALUE)//can't open the drive
{
dwError = GetLastError();
cout<<"Fail to create file!"<<endl;
return;
}
//当驱动器D无读写时,即可锁定卷
lockResult = DeviceIoControl(
hDevice,
FSCTL_LOCK_VOLUME,
NULL,
0,
NULL,
0,
&cbReturned,
NULL);
if(!lockResult)
cout<<"Fail to lock volume!"<<endl;
else
{
//锁定成功即可将卷卸除
dismountResult = DeviceIoControl(
hDevice,
FSCTL_DISMOUNT_VOLUME,
NULL,
0,
NULL,
0,
&cbReturned,
NULL);
if(!dismountResult)
cout<<"Fail to dismount volume!"<<endl;
else
cout<<"Dismount successful!"<<endl;
}
}
}
#include <windows.h>
#include <WinIoCtl.h>
void main()
{
HANDLE hDevice;//handle to the drive to be examined
BOOL lockResult;//lock results flag
BOOL dismountResult;//dismount results flag
BOOL unlockResult;//unlock results flag
DWORD cbReturned;//discard results
DWORD dwError;
cout<<"Dismount drive D, Y or N?";
char chDismount;
cin>>chDismount;
if(chDismount == 'Y' || chDismount == 'y')
{
//打开卷
hDevice = CreateFile("\\.\D:",//drive to open
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,//share mode
NULL, //default security attributes
OPEN_EXISTING,//disposition
0, //file attributes.
NULL); // don't copy any file's attributes
if(hDevice == INVALID_HANDLE_VALUE)//can't open the drive
{
dwError = GetLastError();
cout<<"Fail to create file!"<<endl;
return;
}
//当驱动器D无读写时,即可锁定卷
lockResult = DeviceIoControl(
hDevice,
FSCTL_LOCK_VOLUME,
NULL,
0,
NULL,
0,
&cbReturned,
NULL);
if(!lockResult)
cout<<"Fail to lock volume!"<<endl;
else
{
//锁定成功即可将卷卸除
dismountResult = DeviceIoControl(
hDevice,
FSCTL_DISMOUNT_VOLUME,
NULL,
0,
NULL,
0,
&cbReturned,
NULL);
if(!dismountResult)
cout<<"Fail to dismount volume!"<<endl;
else
cout<<"Dismount successful!"<<endl;
}
}
}
解决方案 »
- 今天运气不好, 散分
- 300元,很简单的一个NS模拟
- 怎么才能知道点击了CListCtrl控件的哪一行哪一列?
- 严正抗议CSDN,不加可用分,我都不好意思提问了。
- 一个简单的问题,请大家来看看,谢谢了
- 如何实现视图滚动!!!(去掉了 Document/View architecture support)
- 在MFC工程中调整了对话框的大小,编译运行,弹出的对话框没有改变
- 关于menu上获得消息的方法.
- WinMain的第一个参数hInstance是哪来的?49分白送!!
- 大佬们,mfc怎么调用qt对话框的dl?求助
- _RecordsetPtr的值如何在DBGrid中显示?(新手求教)
- 找: 根据硬盘ID和程序使用时间期限来生成序列码的算法,生成的序列码要求最好是可见字符.
1. Call CreateFile with GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, and OPEN_EXISTING. The lpFileName parameter should be \\.\X: (where X is the real drive letter). All other parameters can be zero.
2. Lock the volume by issuing the FSCTL_LOCK_VOLUME IOCTL via DeviceIoControl. If any other application or the system is using the volume, this IOCTL fails. Once this function returns successfully, the application is guaranteed that the volume is not used by anything else in the system.
3. Dismount the volume by issuing the FSCTL_DISMOUNT_VOLUME IOCTL. This causes the file system to remove all knowledge of the volume and to discard any internal information that it keeps regarding the volume.
4. Make sure the media can be removed by issuing the IOCTL_STORAGE_MEDIA_REMOVAL IOCTL. Set the PreventMediaRemoval member of the PREVENT_MEDIA_REMOVAL structure to FALSE before calling this IOCTL. This stops the device from preventing the removal of the media.
5. Eject the media with the IOCTL_STORAGE_EJECT_MEDIA IOCTL. If the device doesn't allow automatic ejection, then IOCTL_STORAGE_EJECT_MEDIA can be skipped and the user can be instructed to remove the media.
6. Close the volume handle obtained in the first step or issue the FSCTL_UNLOCK_VOLUME IOCTL. This allows the drive to be used by other processes.
但是下面程序做到第五步,DeviceIOControl的值为false,不知道哪错了
#include <iostream.h>
#include <windows.h>
#include <WinIoCtl.h>void main()
{
HANDLE hDevice;//handle to the drive to be examined
BOOL lockResult;//lock results flag
BOOL dismountResult;//dismount results flag
BOOL unlockResult;//unlock results flag
BOOL removalResult;
BOOL ejectResult;
DWORD cbReturned;//discard results
DWORD dwError;
cout<<"Dismount drive D, Y or N?";
char chDismount;
cin>>chDismount;
if(chDismount == 'Y' || chDismount == 'y')
{ //Open the volume
hDevice = CreateFile("\\\\.\\D:",//drive to open
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,//share mode
NULL, //default security attributes
OPEN_EXISTING,//disposition
0, //file attributes.
NULL); // don't copy any file's attributes if(hDevice == INVALID_HANDLE_VALUE)//can't open the drive
{
dwError = GetLastError();
cout<<"Fail to create file!"<<endl;
return;
} //Lock the volume
lockResult = DeviceIoControl(
hDevice,
FSCTL_LOCK_VOLUME,
NULL,
0,
NULL,
0,
&cbReturned,
NULL); if(!lockResult)
{
cout<<"Fail to lock volume!"<<endl;
exit(0);
}
else
{
//Dismount the volume
dismountResult = DeviceIoControl(
hDevice,
FSCTL_DISMOUNT_VOLUME,
NULL,
0,
NULL,
0,
&cbReturned,
NULL); if(!dismountResult)
{
cout<<"Fail to dismount volume!"<<endl;
exit(0);
}
else
{
cout<<"Dismount successful!"<<endl; //Make sure the media can be removed
PREVENT_MEDIA_REMOVAL PMRBuffer;
PMRBuffer.PreventMediaRemoval = true;
removalResult = DeviceIoControl(
hDevice,
IOCTL_STORAGE_MEDIA_REMOVAL,
&PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL),
NULL,
0,
&cbReturned,
NULL); if(!removalResult)
{
cout<<"Fail to remove drive D!"<<endl;
exit(0);
}
else
{
cout<<"Remove successful!"<<endl;
//Eject device 这里出错了!
ejectResult = DeviceIoControl( hDevice,
IOCTL_STORAGE_EJECT_MEDIA,
NULL, 0,
NULL, 0,
&cbReturned,
NULL); if(!ejectResult)
{
cout<<"Fail to eject drive D!"<<endl;
}
else
{
cout<<"eject successful!"<<endl;
}
}
}
}
}
}