使用FSCTL

编程入门 行业动态 更新时间:2024-10-26 18:25:42
使用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 faile with error 1:Incorrect function”。

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); }

你们有什么想知道出了什么问题吗?

I'm having trouble locking my C drive so I can extract some file information later.

#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"); }

The Error exit below returns "GetProcessID faile with error 1: Incorrect function."

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); }

Do you guys have any idea what's going on wrong?

最满意答案

FSCTL_LOCK_VOLUME的MSDN文档说明了这一点

如果未使用,则锁定卷

C:驱动器几乎总是在使用中。 它没有被使用的唯一时间是你从不同的媒体启动。

The MSDN docs for FSCTL_LOCK_VOLUME say right off the bat that

Locks a volume if it is not in use

The C: drive is almost always in use. The only time it's not in use is if you've booted from different media.

更多推荐

本文发布于:2023-08-04 15:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1418923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:FSCTL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!