标题:关于windows下对磁盘DeviceIoControl操作时,碰到GetLastError 5的问题
作者:lwei24
日期:2022-07-12 17:20
内容:
如题,在Qt中调用DeviceIoControl,控制码为FSCTL_LOCK_VOLUME,结果总是GetLastError 5拒绝访问,这个为啥呢?为啥用MFC就可以,用Qt就不能调用呢?具体代码如下:
char diskPath;
sprintf(diskPath,"\\\\.\\PhysicalDrive%d",Disk);
HANDLE hDevice = CreateFileA(
diskPath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, //default security attributes
OPEN_EXISTING,// disposition
0,// file attributes
NULL
);
DWORD ReturnedByteCount;
BOOL hDevice = DeviceIoControl(
hDisk,
FSCTL_LOCK_VOLUME,
NULL,
0,
NULL,
0,
&ReturnedByteCount,
NULL
&nb ..
#1 [feng851 07-12 17:52]
是否需要提升权限,具体百度
#2 回 feng851 的帖子 [lwei24 07-12 17:54]
feng851:是否需要提升权限,具体百度 (2022-07-12 17:52)
这个,已经以管理员权限运行了,还需要再提升权限吗?
#3 [feng851 07-12 17:57]
〖5〗-拒绝访问。
不是你运行时管理员权限,不是那个意思。是有个windows api函数,需要提前执行它以提升权限。
#4 [feng851 07-12 17:59]
#define UNICODE 1
#define _UNICODE 1
/* The code of interest is in the subroutine GetDriveGeometry. The
code in main shows how to interpret the results of the call. */
#include
#include
#include
#define wszDrive L"\\\\.\\PhysicalDrive0"
BOOL GetDriveGeometry(LPWSTR wszPath, DISK_GEOMETRY *pdg)
{
HANDLE hDevice = INVALID_HANDLE_VALUE;// handle to the drive to be examined
BOOL bResult = FALSE; // results flag
DWORD junk = 0; // discard results
hDevice = CreateFileW(wszPath,// drive to open
0,// no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING,// disposi ..
#5 回 feng851 的帖子 [lwei24 07-12 18:17]
feng851:#define UNICODE 1
#define _UNICODE 1
/* The code of interest is in the subroutine GetDriveGeometry. The
code in main shows how to interpret the results of the call. */
....... (2022-07-12 17:59)
这个是有的sprintf(diskPath,"\\\\.\\PhysicalDrive%d",Disk);,就是在Qt下,不知道怎么好像不太适应,即获取到\\.\PhysicalDrive%d这个路径,后面FSCTL_LOCK_VOLUME的时候总会GetLastError 5错误。