Direct write to HardDisk, WriteFile returns 5 access denied error

Hi All,

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.

UINT ReadWriteDrive::WipeDrive(DriveWipeParams *targetDriveParams, WhatToWrite wtw, LPBYTE dataToWrite)
{
       

   //right now we will assume whole drive wipe; don't look at other parameters
    
    DriveInfo *driveInfo = targetDriveParams->driveInfo;

    QWORD startSector = 0;
    QWORD endSector = driveInfo->TotalSectors;
   

    //right now start point = start of drive, end point = end of drive
    QWORD startPos = 0;
    QWORD endPos = driveInfo->Size;
    
    HANDLE hDevice;

    //reset the reference position for the thread's portion of the progress bar
    myCurrentStepPos = 0;

    //DWORD result;
    DWORD bytesPerSector = driveInfo->BytesPerSector;
    

    CString targetDrive;// = _T("\\\\.");
    targetDrive.Append(driveInfo->DeviceID);
    
   *_tprintf(_T("Opening drive %s\n"),targetDrive);

    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;

                       NULL);            // do not copy file attributes

    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;//for now will always be beginning of drive
    QWORD intermediateEndPos = endPos - (endPos % BLOCK_SIZE);

    
    //should send update for every 16MB

    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)

        FillRandBuff(diskBuffer);
        break;
    
    case(WhatToWrite::Zeros):
        for(int i = 0; i < BLOCK_SIZE; i++)
            diskBuffer[i] = 0;
        break;


    case(WhatToWrite::Ones):
        for(int i = 0; i < BLOCK_SIZE; i++)
            diskBuffer[i] = 1;
       
        break;

    default:
        //_tprintf(_T("Invalid write selection.\n"));
            CloseHandle(hDevice);
            //CloseHandle(hConsole);
            threadExitCode = 2;
            errorString = _T("Invalid write selection.");
            errorCodeDetail = GetLastError();
            delete [] diskBuffer;
            return 2;
    }

       //fix for percent done overwhelming the log
    double lastProgressEntry = -100;
     for(; currentPos < intermediateEndPos;)         
    {
        updateInterval++;       
           
        int rcode = WriteFile(hDevice, diskBuffer, BLOCK_SIZE, &bytesProcessed, NULL);

        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):
        for(int i = 0; i < BLOCK_SIZE; i++)
            diskBuffer[i] = 0;

Then the createFile() also successful. But WriteFile() returns error code 5. Please help me to solve this issue.

Started by Rani Manickam at April 11, 2017 - 1:41 AM