使用FSCTL_LOCK_VOLUME锁定驱动器
Locking a drive with FSCTL_LOCK_VOLUME
我有麻烦锁定我的C驱动器,所以我可以提取一些文件信息以后。
#define wszDrive L"\\.\PhysicalDrive0"
HANDLE targetVol = INVALID_HANDLE_VALUE;
DWORD stats;
targetVol = CreateFile(wszDrive,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
/*FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS*/0,
NULL);
if (targetVol == INVALID_HANDLE_VALUE) // cannot open the drive
{
cout << "error in ioControl with volume handler" << endl;
system("pause");
}
if (DeviceIoControl(targetVol,
FSCTL_LOCK_VOLUME,
NULL, 0, NULL, 0,
&stats,
NULL) ==0)
{
cout << "Error with targetVol DeviceIo" << endl;
ErrorExit(TEXT("GetProcessId"));
system("pause");
}
下面的错误退出返回"GetProcessID失败,错误1:不正确的函数"。
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
你们知道发生了什么事吗?
FSCTL_LOCK_VOLUME的MSDN文档马上说
锁定未使用的卷
C:驱动器几乎总是在使用。
相关文章:
- 如何找到锁定Linux futex的C++行
- G锁定铸造到基础上会释放模拟行为
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- 如何检查线程是否锁定
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 我应该在锁定TBitmap画布后解锁它吗
- 在QTreeView中仅显示共享驱动器和文件夹
- C++ 11 中的锁定是否保证访问数据的新鲜度?
- 在两个线程上读/写 64 位,无互斥/锁定/原子
- 如何在实时应用程序中锁定线程
- 在 lambda 中锁定 std::shared_ptr 的复制操作
- 使用简单两相锁定的并发程序
- 锁定来自其他线程的类成员
- 他们如何将红外锁定像素转换为镜头前方 1m 的正常平面上的位置
- 同一互斥锁顺序上的锁定和解锁是否一致?
- 共享内存的升压容器是否实现锁定?
- QT MYSQL 驱动器构建,未定义对"mysql_get_client_version@0"的引用
- 当只有一个线程主要使用该对象而其他线程很少使用它时,如何最小化该对象的互斥锁锁定?
- 如何在 x86 处理器上实现"锁定添加"
- 使用FSCTL_LOCK_VOLUME锁定驱动器