问题描述
大家好,
我们有一个工具可以擦除硬盘上的数据。但是我得到WriteFile()的访问被拒绝错误。这意味着GetLastError()重新编译错误代码5.我在这里处理代码。< o:p>< / o:p>
we are having a tool which wipes the data from the hard disk. But I get access denied error for WriteFile(). It means the GetLastError() reutrns error code 5. I am coping the code here.<o:p></o:p>
UINT ReadWriteDrive :: WipeDrive(DriveWipeParams * targetDriveParams,WhatToWrite wtw,LPBYTE dataToWrite)
{
&NBSP; &NBSP;
UINT ReadWriteDrive::WipeDrive(DriveWipeParams *targetDriveParams, WhatToWrite wtw, LPBYTE dataToWrite)
{
//现在我们将假设整个驱动器擦除;不要查看其他参数
&NBSP; DriveInfo * driveInfo = targetDriveParams-> driveInfo;<○:p为H.< / O:p为H.
//right now we will assume whole drive wipe; don't look at other parameters
DriveInfo *driveInfo = targetDriveParams->driveInfo;<o:p></o:p>
&NBSP;&NBSP; QWORD startSector = 0;
QWORD endSector = driveInfo-> TotalSectors;
QWORD startSector = 0;
QWORD endSector = driveInfo->TotalSectors;
//现在开始点=开始开车,结束点=开车结束
QWORD startPos = 0;
QWORD endPos = driveInfo->尺寸;
HANDLE hDevice;
//right now start point = start of drive, end point = end of drive
QWORD startPos = 0;
QWORD endPos = driveInfo->Size;
HANDLE hDevice;
//重置进度条线程部分的参考位置
myCurrentStepPos = 0;
//reset the reference position for the thread's portion of the progress bar
myCurrentStepPos = 0;
// DWORD结果;
DWORD bytesPerSector = driveInfo-> BytesPerSector;
//DWORD result;
DWORD bytesPerSector = driveInfo->BytesPerSector;
CString targetDrive; // = _T(" \\\\。");
targetDrive.Append(driveInfo-> DeviceID);
* _tprintf(_T("打开驱动器%s \ n"),targetDrive);
CString targetDrive;// = _T("\\\\.");
targetDrive.Append(driveInfo->DeviceID);
*_tprintf(_T("Opening drive %s\n"),targetDrive);
hDevice = CreateFile(< b&b GENERIC_READ | GENERIC_WRITE, //读取和写入权限 &bbsp; FILE_SHARE_READ | //共享模式
FILE_SHARE_WRITE,
NULL, &NBSP;&NBSP;&NBSP; //默认安全属性
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; OPEN_EXISTING, //处置
FILE_FLAG_N O_BUFFERING,//文件属性;
hDevice = CreateFile(targetDrive,
GENERIC_READ | GENERIC_WRITE, //read and write access
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
FILE_FLAG_NO_BUFFERING,// file attributes;
&NBSP; &NBSP; &NBSP; &NBSP;&NBSP; &NBSP; NULL); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //不要复制文件属性
NULL); // do not copy file attributes
if(hDevice == INVALID_HANDLE_VALUE)
{
&NBSP;&NBSP;&NBSP; _tprintf(_T(" [%s]错误:无法打开drive.\ n"),driveInfo-> DeviceID);
&NBSP;&NBSP;&NBSP; fWriteToLog(_T(" [%s]错误:无法打开drive.\ n"),TRUE,driveInfo-> DeviceID);
&NBSP;&NBSP;&NBSP; errorString = _T("错误:无法打开驱动器。"); $
&NBSP;&NBSP;&NBSP; threadExitCode = 1;
&NBSP;&NBSP;&NBSP; errorCodeDetail = GetLastError();
&NBSP;&NBSP;&NBSP; // CloseHandle(hConsole);
&NBSP;&NBSP;&NBSP; 返回1;
}
if (hDevice == INVALID_HANDLE_VALUE)
{
_tprintf(_T("[%s] Error: unable to open drive.\n"), driveInfo->DeviceID);
fWriteToLog(_T("[%s] Error: unable to open drive.\n"), TRUE, driveInfo->DeviceID);
errorString = _T("Error: unable to open drive.");
threadExitCode = 1;
errorCodeDetail = GetLastError();
//CloseHandle(hConsole);
return 1;
}
// 4194304 = 4MB
DWORD bytesProcessed = 0;
QWORD currentPos = startPos; //现在将永远是驱动器的开头
QWORD intermediateEndPos = endPos - (endPos%BLOCK_SIZE);
//4194304 = 4MB
DWORD bytesProcessed = 0;
QWORD currentPos = startPos;//for now will always be beginning of drive
QWORD intermediateEndPos = endPos - (endPos % BLOCK_SIZE);
//应该每16MB发送一次更新
//should send update for every 16MB
int updateInterval = 0;
LPBYTE diskBuffer = new BYTE [BLOCK_SIZE];
//零,一,随机写作代码
//使用
设置内存块与我们想要写入驱动器的内容 //将来可能需要与其他驱动器共享这一点。
//如果内存成为问题
开关(wtw)
{
case(WhatToWrite :: Random):
&NBSP;&NBSP;&NBSP; // for(int i = 0; i< BLOCK_SIZE; i ++)
&NBSP;&NBSP;&NBSP; // diskBuffer [i] = 0;
&NBSP;&NBSP;&NBSP; // FillRandBuff(LPBYTE lfprandbuf)< o:p>< / o:p>
int updateInterval = 0;
LPBYTE diskBuffer = new BYTE[BLOCK_SIZE];
//code for zero, one, random writing
//set the memory block with what we want to write the drive with
//this may need to be shared with other drives in the future
//if memory becomes an issue
switch(wtw)
{
case(WhatToWrite::Random):
//for(int i = 0; i < BLOCK_SIZE; i++)
//diskBuffer[i] = 0;
//FillRandBuff(LPBYTE lfprandbuf)<o:p></o:p>
&NBSP;&NBSP;&NBSP; FillRandBuff(diskBuffer);
&NBSP; &NBSP; break;
case(WhatToWrite :: Zeros):
&NBSP;&NBSP;&NBSP; for(int i = 0; i< BLOCK_SIZE; i ++)
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; diskBuffer [i] = 0;
&NBSP; &NBSP; break;
FillRandBuff(diskBuffer);
break;
case(WhatToWrite::Zeros):
for(int i = 0; i < BLOCK_SIZE; i++)
diskBuffer[i] = 0;
break;
case(WhatToWrite :: Ones):
&NBSP;&NBSP;&NBSP; for(int i = 0; i< BLOCK_SIZE; i ++)
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; diskBuffer [i] = 1;
&NBSP; &NBSP;
&NBSP;&NBSP;&NBSP; break;
case(WhatToWrite::Ones):
for(int i = 0; i < BLOCK_SIZE; i++)
diskBuffer[i] = 1;
break;
默认:
&NBSP;&NBSP;&NBSP; // _ tprintf(_T("无效的写入选择。\ n"));
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; CloseHandle(hDevice);
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; // CloseHandle(hConsole);
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; threadExitCode = 2;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; errorString = _T("无效的写入选择。");
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; errorCodeDetail = GetLastError();
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; delete [] diskBuffer;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; 返回2;
}
default:
//_tprintf(_T("Invalid write selection.\n"));
CloseHandle(hDevice);
//CloseHandle(hConsole);
threadExitCode = 2;
errorString = _T("Invalid write selection.");
errorCodeDetail = GetLastError();
delete [] diskBuffer;
return 2;
}
&NBSP;&NBSP; //修复已完成的百分比压倒日志
double lastProgressEntry = -100;
for(; currentPos< intermediateEndPos;) &NBSP;&NBSP;&NBSP;
{
&NBSP;&NBSP;&NBSP; &NBSP; updateInterval ++;&NBSP; &NBSP; &NBSP;
&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;
&NBSP;&NBSP;&NBSP; int rcode = WriteFile(hDevice,diskBuffer,BLOCK_SIZE,& bytesProcessed,NULL);
//fix for percent done overwhelming the log
double lastProgressEntry = -100;
for(; currentPos < intermediateEndPos;)
{
updateInterval++;
int rcode = WriteFile(hDevice, diskBuffer, BLOCK_SIZE, &bytesProcessed, NULL);
&NBSP; &NBSP;&NBSP;&NBSP; if(rcode == 0)
&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; threadExitCode = 1;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; errorString = _T("无法写入驱动器。"); $
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; errorCodeDetail = GetLastError();
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; fWriteToLog(_T("错误代码:%d \ n")),TRUE,errorCodeDetail);
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; _tprintf(_T("无法写入drive.\ n"));
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; fWriteToLog(_T(" [%s]无法写入drive.\ n"),TRUE,driveInfo-> DeviceID);
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;&NBSP; // CloseHandle(hConsole);
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; CloseHandle(hDevice);
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; delete [] diskBuffer;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; 返回1;
&NBSP;&NBSP;&NBSP; }
if (rcode == 0)
{
threadExitCode = 1;
errorString = _T("Failed to write to drive.");
errorCodeDetail = GetLastError();
fWriteToLog(_T("Error Code: %d\n"), TRUE, errorCodeDetail);
_tprintf(_T("Failed to write to drive.\n"));
fWriteToLog(_T("[%s] Failed to write to drive.\n"),TRUE,driveInfo->DeviceID);
//CloseHandle(hConsole);
CloseHandle(hDevice);
delete [] diskBuffer;
return 1;
}
在切换案例中,它执行以下代码
in switch case statment it executes the below code
case(WhatToWrite :: Zeros):
&NBSP;&NBSP;&NBSP; for(int i = 0; i< BLOCK_SIZE; i ++)
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; diskBuffer [i] = 0;
case(WhatToWrite::Zeros):
for(int i = 0; i < BLOCK_SIZE; i++)
diskBuffer[i] = 0;
然后createFile()也成功了。但WriteFile()返回错误代码5.请帮我解决这个问题。
Then the createFile() also successful. But WriteFile() returns error code 5. Please help me to solve this issue.
Rani manickam
Rani manickam
推荐答案
我不确定您的位置追踪是硬盘驱动器将在位置0(并且如果它是交易安全的情况下可能位置为1)具有主引导记录,您可能没有写入权限,并且可能是您的
错误的来源。 我建议搜索一些其他有用的示例代码网站,因为消隐磁盘的内容不是一个新概念,可能会有一些有用的帖子。 例如,我发现这是在计算器上
功能。可以帮助你。
I am not sure about your position tracking as the hard drive will have a Master Boot Record at position 0 (and potentially position 1 if it is transaction safe) that you probably don't have permission to write and potentially that is the source of your error. I recommend searching some of the other helpful sample code sites as blanking the contents of a disk is not a new concept there are likely to be some helpful posts there. For example I found this one on StackOverflowhttp://stackoverflow.com/questions/13390843/how-to-erase-a-hard-disk-drive that might help you.
此致
IoTGirl
这篇关于直接写入HardDisk,WriteFile返回5访问被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!