Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results


More Info on Driver Writing and Debugging

The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.

Check out The OSR Learning Library at:

Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

ATA Pass through read using DeviceIoControl returns error : error_invalid_function

kernelboikernelboi Member Posts: 21

I'm trying to use ATA pass through to read the disk sectors, but DeviceIoControl is returns 0, and the GetLastError is 1 which is error_invalid_function, what am i doing wrong here? i just want to read the first sector of disk. tried the command 0x20 instead of 0x24 too and still no luck

This is my code :

    BOOL status = FALSE;
    DWORD dataSize = sizeof(ATA_PASS_THROUGH_EX) + 512;
    BYTE Buffer[sizeof(ATA_PASS_THROUGH_EX) + 512];
    DWORD bytescopied = 0;

    HANDLE hDevice = CreateFileA("\\\\.\\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE,

    if (hDevice == NULL) return 0;

    pATAData = (ATA_PASS_THROUGH_EX*)Buffer;

    ZeroMemory(pATAData, dataSize); 

    pATAData->Length = sizeof(ATA_PASS_THROUGH_EX);
    pATAData->DataBufferOffset = sizeof(ATA_PASS_THROUGH_EX);
    pATAData->DataTransferLength = 512;
    pATAData->TimeOutValue = 2;

    pATAData->CurrentTaskFile[0] = 0x00;
    pATAData->CurrentTaskFile[1] = 0x01;
    pATAData->CurrentTaskFile[2] = 0x01;
    pATAData->CurrentTaskFile[3] = 0x00;
    pATAData->CurrentTaskFile[4] = 0x00;
    pATAData->CurrentTaskFile[5] = 0x40;
    pATAData->CurrentTaskFile[6] = 0x24;
    pATAData->CurrentTaskFile[7] = 0x00;

    pATAData->PreviousTaskFile[0] = 0x00;
    pATAData->PreviousTaskFile[1] = 0x00;
    pATAData->PreviousTaskFile[2] = 0x00;
    pATAData->PreviousTaskFile[3] = 0x00;
    pATAData->PreviousTaskFile[4] = 0x00;
    pATAData->PreviousTaskFile[5] = 0x04;
    pATAData->PreviousTaskFile[6] = 0x00;
    pATAData->PreviousTaskFile[7] = 0x00;

    status = DeviceIoControl(hDevice, IOCTL_ATA_PASS_THROUGH, pATAData, dataSize, Buffer, dataSize, &bytescopied, NULL);


  • kernelboikernelboi Member Posts: 21
    edited July 2020

    Sorry guys there is a mistake in the code, in my actual code the if (hDevice == NULL) is changed to if (hDevice == INVALID_HANDLE_VALUE) but i forgot to edit it.

    and this change doesn't effect the result, still the same GetLastError message.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Developing Minifilters 24 May 2021 Live, Online
Writing WDF Drivers 14 June 2021 Live, Online
Internals & Software Drivers 27 September 2021 Live, Online
Kernel Debugging TBD 2021 Live, Online