#include <include\SetupApi.au3>
Const $IOCTL_LOCK_VOLUME = 0x90018
Const $IOCTL_UNLOCK_VOLUME = 0x9001C
Const $tagDRIVE_PER_SECTOR_DATA = "ubyte SectorData[512]"
Local $hDrive, $iAccessMask, $pBuffer, $pSerial
Local $tBuffer, $tSerial, $sDrive = "C:", $bNewSerial = 0x11111111
$iAccessMask = bitOR($GENERIC_READ, $GENERIC_WRITE)
$pBuffer = _CM_Heap_Alloc(512)
Switch DriveGetFileSystem($sDrive)
Case "NTFS"
$pSerial = $pBuffer + 72
Case "FAT32"
$pSerial = $pBuffer + 67
Case Else
_CM_Heap_Free($pBuffer)
Msgbox(48, "Error!", $sDrive & " not support.")
exit
EndSwitch
$hDrive = _CM_Create_File("\\." & $sDrive, $iAccessMask, 3, 0, 3, 0)
_CM_Device_IO_Control($hDrive, $IOCTL_LOCK_VOLUME, 0, 0, 0, 0)
_CM_Read_Device($hDrive, $pBuffer, 512)
_CM_Device_IO_Control($hDrive, $IOCTL_UNLOCK_VOLUME, 0, 0, 0, 0)
_CM_Close_Handle($hDrive)
$tBuffer = DllStructCreate($tagDRIVE_PER_SECTOR_DATA, $pBuffer)
$tSerial = DllStructCreate("hWnd Serial", $pSerial)
DllStructSetData($tSerial, "Serial", $bNewSerial)
$hDrive = _CM_Create_File("\\." & $sDrive, $iAccessMask, 3, 0, 3, 0)
_CM_Device_IO_Control($hDrive, $IOCTL_LOCK_VOLUME, 0, 0, 0, 0)
_CM_Write_Device($hDrive, $pBuffer, 512)
_CM_Device_IO_Control($hDrive, $IOCTL_UNLOCK_VOLUME, 0, 0, 0, 0)
_CM_Close_Handle($hDrive)
Msgbox(64, "", "Done!")
If (DriveGetFileSystem($sDrive) = "NTFS") Then
Msgbox(48, "", "A system reboot is required to update the serial number.")
EndIf
Opt("TrayIconHide", 1)
ProcessClose(@AutoItPid)