I am making a C++ Console Application to get Disk’s ATA Information but I am getting Error Code 1 in DeviceIoControl.
ConsoleApp.cpp
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
#include <winioctl.h>
#pragma pack(push, 1)
typedef struct _ATA_PASS_THROUGH_EX {
USHORT Length;
USHORT AtaFlags;
UCHAR PathId;
UCHAR TargetId;
UCHAR Lun;
UCHAR ReservedAsUchar;
ULONG DataTransferLength;
ULONG TimeOutValue;
ULONG ReservedAsUlong;
ULONG_PTR DataBufferOffset;
UCHAR PreviousTaskFile[8];
UCHAR CurrentTaskFile[8];
} ATA_PASS_THROUGH_EX, * PATA_PASS_THROUGH_EX;
typedef struct _IDENTIFY_DEVICE_DATA {
USHORT GeneralConfiguration;
USHORT NumberOfCylinders;
USHORT Reserved1;
USHORT NumberOfHeads;
USHORT UnformattedBytesPerTrack;
USHORT UnformattedBytesPerSector;
USHORT SectorsPerTrack;
USHORT VendorUnique1[3];
USHORT SerialNumber[10];
USHORT BufferType;
USHORT BufferSize;
USHORT ECCSize;
USHORT FirmwareRevision[4];
USHORT ModelNumber[20];
USHORT MaximumSectorsPerInterrupt;
USHORT Reserved2;
USHORT Capabilities[2];
USHORT Reserved3;
USHORT PIODataTransferCycleTimingMode;
USHORT MinimumPIODataTransferCycleTimingMode;
USHORT DMADataTransferCycleTimingMode;
USHORT MinimumDMAMultiwordDataTransferCycleTime;
USHORT RecommendedDMAMultiwordDataTransferCycleTime;
USHORT MinimumPIOTransferCycleTimeWithoutFlowControl;
USHORT MinimumPIOTransferCycleTimeWithFlowControl;
USHORT Reserved4[6];
USHORT MajorRevision;
USHORT MinorRevision;
USHORT CommandSetsSupported[3];
USHORT CommandSetsEnabled[3];
USHORT UltraDMAModes;
USHORT Reserved5[167];
USHORT Checksum;
} IDENTIFY_DEVICE_DATA, * PIDENTIFY_DEVICE_DATA;
#pragma pack(pop)
#define IOCTL_ATA_PASS_THROUGH CTL_CODE(FILE_SHARE_READ | FILE_SHARE_WRITE, 0x040b, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define IDENTIFY_DEVICE 0xEC
#define ATA_FLAGS_DATA_IN 0x01
void PrintATAInformation() {
HANDLE hDevice = CreateFile(L"\\.\PhysicalDrive0",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
std::cerr << "Error opening device: " << GetLastError() << std::endl;
return;
}
ATA_PASS_THROUGH_EX apt = { 0 };
apt.Length = sizeof(ATA_PASS_THROUGH_EX);
apt.AtaFlags = ATA_FLAGS_DATA_IN;
apt.DataTransferLength = sizeof(IDENTIFY_DEVICE_DATA);
apt.TimeOutValue = 10;
apt.DataBufferOffset = sizeof(ATA_PASS_THROUGH_EX);
apt.CurrentTaskFile[6] = IDENTIFY_DEVICE;
BYTE buffer[sizeof(ATA_PASS_THROUGH_EX) + sizeof(IDENTIFY_DEVICE_DATA)] = { 0 };
memcpy(buffer, &apt, sizeof(ATA_PASS_THROUGH_EX));
DWORD bytesReturned = 0;
BOOL result = DeviceIoControl(hDevice,
IOCTL_ATA_PASS_THROUGH,
buffer,
sizeof(buffer),
buffer,
sizeof(buffer),
&bytesReturned,
NULL);
if (result == FALSE) {
std::cerr << "Error in DeviceIoControl: " << GetLastError() << std::endl;
CloseHandle(hDevice);
return;
}
UCHAR* data = buffer + sizeof(ATA_PASS_THROUGH_EX);
PIDENTIFY_DEVICE_DATA identifyDevice = reinterpret_cast<PIDENTIFY_DEVICE_DATA>(data);
std::string modelNumber(reinterpret_cast<char*>(identifyDevice->ModelNumber), sizeof(identifyDevice->ModelNumber));
modelNumber.erase(std::remove(modelNumber.begin(), modelNumber.end(), ' '), modelNumber.end());
std::cout << "Model Number: " << modelNumber << std::endl;
std::cout << "Number of Cylinders: " << identifyDevice->NumberOfCylinders << std::endl;
std::cout << "Number of Heads: " << identifyDevice->NumberOfHeads << std::endl;
std::cout << "Sectors per Track: " << identifyDevice->SectorsPerTrack << std::endl;
std::cout << "ATA Revision: " << identifyDevice->MajorRevision << "." << identifyDevice->MinorRevision << std::endl;
CloseHandle(hDevice);
}
int main() {
PrintATAInformation();
return 0;
}
The error is Error in DeviceIoControl: 1, I also tried getting Information using IOCTL_ATA_PASSTHROUGH_DIRECT but there I got Error in DeviceIoControl: 87.
Vendor Information DELL(tm)
Hard Disk Cylinders 1938021
Hard Disk Heads 16
Hard Disk Sectors 63
ATA Revision ATA8-ACS version 4
Transport Version SATA Rev 3.0
Total Sectors 244190646
Bytes Per Sector 4096 [Advanced Format]
Buffer Size 23652 KB
Multiple Sectors 16
Error Correction Bytes 56
Unformatted Capacity 953870 MB
Maximum PIO Mode 4
Maximum Multiword DMA Mode 2
Highest Possible Transfer Rate S-ATA Gen3 Signaling Speed (6 Gps)
Negotiated Transfer Rate S-ATA Gen3 Signaling Speed (6 Gps)
Minimum multiword DMA Transfer Time 120 ns
Recommended Multiword DMA Transfer Time 120 ns
Minimum PIO Transfer Time Without IORDY 120 ns
Minimum PIO Transfer Time With IORDY 120 ns
ATA Control Byte Valid
ATA Checksum Value Valid
Does anyone know by which IOCTL I can get these data or the cause of error in my code?
karen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.