#pragma BEGINDUMP
#include <hbapi.h>
#include <Windows.h>
HB_FUNC( TEST )
{
char szVolumeLetter[32];
char szPhysicalDrive[64];
HANDLE hPhysicalDriveIOCTL = CreateFile(L"\\\\.\\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if(hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
{
DWORD dwBytesReturned = 0;
// Get Physical Drive Information
VOLUME_DISK_EXTENTS vdExtents;
GETVERSIONINPARAMS gvopVersionParams;
ZeroMemory(&vdExtents, sizeof(vdExtents));
if(!DeviceIoControl(hPhysicalDriveIOCTL, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0,
&vdExtents, sizeof(vdExtents), &dwBytesReturned, NULL)){
MessageBox( 0 , "Error volumes that span multiple disks are not supported!", "sorry", 0 );
CloseHandle(hPhysicalDriveIOCTL);
return 1;
}
// Get SMART version information
ZeroMemory(&gvopVersionParams, sizeof(gvopVersionParams));
if(!DeviceIoControl(hPhysicalDriveIOCTL, SMART_GET_VERSION, NULL, 0,
&gvopVersionParams , sizeof(gvopVersionParams), &dwBytesReturned, NULL)){
MessageBox( 0, "Error cannot get SMART version information from device!", "warning", 0 );
CloseHandle(hPhysicalDriveIOCTL);
return 1;
}
if(gvopVersionParams.bIDEDeviceMap > 0)
{
// Setup SMART request
SENDCMDINPARAMS InParams = {
IDENTIFY_BUFFER_SIZE, { 0, 1, 1, 0, 0, ((vdExtents.Extents[0].DiskNumber & 1) ? 0xB0 : 0xA0),
((gvopVersionParams.bIDEDeviceMap >> vdExtents.Extents[0].DiskNumber & 0x10) ? ATAPI_ID_CMD : ID_CMD) },
(BYTE)vdExtents.Extents [0].DiskNumber
};
DWORD dwBufSize = sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE;
SENDCMDOUTPARAMS pbtIDOutCmd; // = (PSENDCMDOUTPARAMS) new BYTE[dwBufSize];
ZeroMemory(&pbtIDOutCmd, dwBufSize);
// Get SMART information
if(DeviceIoControl (hPhysicalDriveIOCTL, SMART_RCV_DRIVE_DATA, &InParams, sizeof(SENDCMDINPARAMS),
&pbtIDOutCmd, dwBufSize, &dwBytesReturned, NULL))
{
// Little Endian To Big Endian
USHORT *pIDSector = (USHORT*)pbtIDOutCmd.bBuffer;
int nShort;
char lpszSerialNumber1[ 21 ];
LPSTR lpszSerialNumber2;
for( nShort = 10; nShort < 21; nShort++)
pIDSector [nShort] = (((pIDSector[nShort] & 0x00FF) << 8) + ((pIDSector[nShort] & 0xFF00) >> 8));
// Get Drive Serial Number
ZeroMemory(lpszSerialNumber1, 21);
RtlCopyMemory(lpszSerialNumber1, &pIDSector[10], 20);
// Remove those horrible spaces caused because of endianess
// and print out the serial
lpszSerialNumber2 = lpszSerialNumber1;
while(*lpszSerialNumber2 == ' ') lpszSerialNumber2++;
MessageBox( 0, "Physical Serial:", lpszSerialNumber2, 0);
// delete lpszSerialNumber1;
}
// delete pbtIDOutCmd;
}else{
printf ("Error !\n");
CloseHandle(hPhysicalDriveIOCTL);
return 1;
}
CloseHandle (hPhysicalDriveIOCTL);
}
}
#pragma ENDDUMP