#include <WinAPI.au3>
$sID = _DriveATA_GetSerial()
If @error Then ConsoleWrite("ERR: "& @error &":"& @extended &@CRLF)
ConsoleWrite($sID &@CRLF)
Func _DriveATA_GetSerial($iDrvNum = 0)
Local $tVP = DllStructCreate("byte bVersion;byte bRevision;byte bReserved;"& _
"byte bIDEDeviceMap;ulong fCapabilities;ulong dwReserved[4]")
Local $pVP = DllStructGetPtr($tVP), $zVP = DllStructGetSize($tVP)
Local $tSCIP = DllStructCreate("ulong cBufferSize;byte irDriveRegs[8];byte bDriveNumber;"& _
"byte bReserved[3];ulong dwReserved[4];byte bBuffer[1]")
Local $pSCIP = DllStructGetPtr($tSCIP), $zSCIP = DllStructGetSize($tSCIP)
Local $tSCOP = DllStructCreate("ulong cBufferSize;byte[4];ulong[2];byte bBuffer[1];byte[512]")
Local $pSCOP = DllStructGetPtr($tSCOP), $zSCOP = DllStructGetSize($tSCOP)
Local $tDR = DllStructCreate("byte bFeaturesReg;byte bSectorCountReg;byte bSectorNumberReg;"& _
"byte bCylLowReg;byte bCylHighReg;byte bDriveHeadReg;byte bCommandReg;byte bReserved", _
DllStructGetPtr($tSCIP, "irDriveRegs") )
Local $tBytes = DllStructCreate("dword"), $pBytes = DllStructGetPtr($tBytes)
Local $aRet, $bIDCnd = 0xEC, $tSerial, $sSerial
$iDrvNum = Abs(Int($iDrvNum))
Local $hDisk = _WinAPI_CreateFile("\\.\PHYSICALDRIVE"& $iDrvNum, 2, 6, 6)
If $hDisk=0 Then Return SetError(1, 0, "")
$aRet = DllCall("kernel32.dll", "int", "DeviceIoControl", _
"hwnd", $hDisk, "dword", 0x00074080, "ptr", 0, "dword", 0, _
"ptr", $pVP, "dword", $zVP, "ptr", $pBytes, "ptr", 0)
If $aRet[0]=0 Then Return SetError(2, _WinAPI_GetLastError(), _WinAPI_CloseHandle($hDisk))
If DllStructGetData($tVP, "bIDEDeviceMap")<=0 Then Return SetError(3,0,_WinAPI_CloseHandle($hDisk))
If BitAND(BitShift(DllStructGetData($tVP, "bIDEDeviceMap"),$iDrvNum),0x10) Then _
Return SetError(4,0,_WinAPI_CloseHandle($hDisk))
DllStructSetData($tDR, "bSectorCountReg", 1)
DllStructSetData($tDR, "bSectorNumberReg", 1)
DllStructSetData($tDR, "bDriveHeadReg", BitOR(BitShift(BitAND($iDrvNum,1),-4),0xA0) )
DllStructSetData($tDR, "bCommandReg", 0xEC)
DllStructSetData($tSCIP, "bDriveNumber", $iDrvNum)
DllStructSetData($tSCIP, "cBufferSize", 512)
$aRet = DllCall("kernel32.dll", "int", "DeviceIoControl", _
"hwnd", $hDisk, "dword", 0x0007c088, "ptr", $pSCIP, "dword", $zSCIP-1, _
"ptr", $pSCOP, "dword", $zSCOP-1, "ptr", $pBytes, "ptr", 0)
If $aRet[0]=0 Then Return SetError(5, _WinAPI_GetLastError(), _WinAPI_CloseHandle($hDisk))
_WinAPI_CloseHandle($hDisk)
$tSerial = DllStructCreate("char[30]", DllStructGetPtr($tSCOP, "bBuffer")+20)
$sSerial = StringRegExpReplace(DllStructGetData($tSerial,1), "(.)(.)", "\2\1")
Return $sSerial
EndFunc