代码在这里
HANDLE hDrive = CreateFile(_T("\\\\.\\D:"), GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,///*FILE_FLAG_WRITE_THROUGH |*/FILE_FLAG_NO_BUFFERING,
NULL);
assert(hDrive != INVALID_HANDLE_VALUE);
我还尝试了CreateFile(_T("\\\.\D:",...)
和OPEN_EXISTING
,但也返回INVALID_HANDLE_VALUE
。
我致电GetLastError()
,错误代码为5,ERROR_ACCESS_DENIED
。
答案 0 :(得分:2)
我有一个可行的代码
HANDLE hDrive = CreateFile(_T("\\\\.\\PhysicalDrive0"), GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY,///*FILE_FLAG_WRITE_THROUGH |*/FILE_FLAG_NO_BUFFERING,
NULL);
和
HANDLE hDrive = CreateFile(_T("\\\\.\\D:"), GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY,///*FILE_FLAG_WRITE_THROUGH |*/FILE_FLAG_NO_BUFFERING,
NULL);
DWORD dwError = GetLastError();
assert(hDrive != INVALID_HANDLE_VALUE);
cin.get();
需要以管理员身份运行