Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys I am making tool for getting SMART data from HDD which is connected to RAID contoller but i am getting "Incorrect function" error while i am trying to send Identify device command via DeviceIoConrol()....Below is my code sample...plz help me to get rid of that error...
C++
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <winioctl.h>
#include <ntddscsi.h>

ATA_PASS_THROUGH_EX ATAData;
ATA_OUTPUT_BUFFER ATAOut;typedef struct _ATA_OUTPUT_BUFFER
{
	ATA_PASS_THROUGH_EX pSCOP;
	BYTE output_buff[512];
}ATA_OUTPUT_BUFFER,*PATA_OUTPUT_BUFFER;


int main()
{
	HANDLE				hSMARTIOCTL;
	int			        i,status,dataSize,flag=0;
	DWORD				cbBytesReturned=0;
	BYTE				bIDCmd;
	BYTE				bDfpDriveMap = 0;
	

	dataSize=512;

	for(i=1;i<2;i++)
	{
            if ((hSMARTIOCTL = OpenSMART(i)) !=     INVALID_HANDLE_VALUE)		// Try to get a handle to SMART IOCTL, report failure and exit if can't.
              {				                  
                memset(ATAOut.output_buff, 0, sizeof(512));
				//Allocate memory for   
                ATA_PASS_THROUGH_EX and clear the contents 
		 if ( DoIDENTIFY(hSMARTIOCTL, &ATAData,&ATAOut,bIDCmd, i, &cbBytesReturned))
		{
			DisplayIdInfo((PIDSECTOR) ATAOut.output_buff,bIDCmd,i);
		}
	         else
	        {
	       	        printf("Identify Command Error %d\n",GetLastError());
		}
             }
             }
}

HANDLE OpenSMART(int n)
{
	HANDLE	hSMARTIOCTL = 0;
	char szT1[256]={0};
	printf("***----------------------------------------***\n");
	printf(" <<            Device Discover             >>\n");
	printf("***----------------------------------------***\n\n");

		 wsprintf(szT1,"\\\\.\\PHYSICALDRIVE%d",n);
	
         if ((hSMARTIOCTL = CreateFile(szT1,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL)) == INVALID_HANDLE_VALUE)
		printf("Unable to open physical drive, error code: 0x%lX\n", GetLastError());
   	else
		printf("Physical drive %d opened successfully\n\n",n);

	return hSMARTIOCTL;
}

BOOL DoIDENTIFY(HANDLE hSMARTIOCTL, PATA_PASS_THROUGH_EX pSCIP,PATA_OUTPUT_BUFFER pSCOP, BYTE bIDCmd, BYTE bDriveNum, PDWORD lpcbBytesReturned)
{
	int val;
	// Set up data structures for IDENTIFY command.
	pSCIP->AtaFlags = ATA_FLAGS_DATA_IN;
	pSCIP->Length= sizeof(ATAData);
	pSCIP->DataTransferLength= IDENTIFY_BUFFER_SIZE;
	pSCIP->TimeOutValue= 10;
	pSCIP->DataBufferOffset= offsetof(ATA_OUTPUT_BUFFER,output_buff); 
	pSCIP->CurrentTaskFile[0]=0;
	pSCIP->CurrentTaskFile[1]=1;
	pSCIP->CurrentTaskFile[2]=1;
	pSCIP->CurrentTaskFile[3]=0;
	pSCIP->CurrentTaskFile[4]=0;
	pSCIP->CurrentTaskFile[5]=0xA0 | ((bDriveNum & 1) << 4); 	// Compute the drive number.
	pSCIP->CurrentTaskFile[6]=0XEC;
	pSCIP->CurrentTaskFile[7] = 0;    // The command can either be IDE identify or ATAPI identify.
	pSCIP->TargetId=bDriveNum;
	
    val=DeviceIoControl(hSMARTIOCTL, IOCTL_ATA_PASS_THROUGH,
            pSCIP,sizeof(ATAOut),pSCOP, sizeof(ATAOut), 
            lpcbBytesReturned, NULL);
	  return val;
}
Posted
Comments
KarstenK 9-Jul-14 6:52am    
check whether the drive firmware supports this feature.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900