ATA Pass through read using DeviceIoControl returns error : error_invalid_function

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;
    PATA_PASS_THROUGH_EX pATAData;
    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,
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

    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->AtaFlags = ATA_FLAGS_48BIT_COMMAND | ATA_FLAGS_DRDY_REQUIRED | ATA_FLAGS_DATA_IN;

    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);

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.