#Region Header
#Include-once
;#Include <StructureConstants.au3>
#Include <WinAPI.au3>
#RequireAdmin
#EndRegion Header
#Region Global Variables and Constants
; ===============================================================================================================================
; _WinAPI_GetDiskSerialNumber()
; ===============================================================================================================================
Const $IOCTL_STORAGE_BASE = 0x2D
Const $IOCTL_STORAGE_QUERY_PROPERTY = __MakeCtrlCode($IOCTL_STORAGE_BASE, 0x500)
Const $DFP_RECEIVE_DRIVE_DATA = 0x7C088
; ===============================================================================================================================
; *Structure constants
; ===============================================================================================================================
Global Const $tagBITMAP = 'long bmType;long bmWidth;long bmHeight;long bmWidthBytes;ushort bmPlanes;ushort bmBitsPixel;ptr bmBits'
;lobal Const $tagBITMAPINFOHEADER = 'dword biSize;long biWidth;long biHeight;ushort biPlanes;ushort biBitCount;dword biCompression;dword biSizeImage;long biXPelsPerMeter;long biYPelsPerMeter;dword biClrUsed;dword biClrImportant'
;Global Const $tagBITMAPINFO = $tagBITMAPINFOHEADER & ';dword bmiColors[1]'
Global Const $tagDIBSECTION = $tagBITMAP & ';' & $tagBITMAPINFOHEADER & ';dword dsBitfields[3];ptr dshSection;dword dsOffset'
Global Const $tagDISK_GEOMETRY = 'int64 Cylinders;dword MediaType;dword TracksPerCylinder;dword SectorsPerTrack;dword BytesPerSector'
Global Const $tagDISK_GEOMETRY_EX = $tagDISK_GEOMETRY & ';int64 DiskSize;dword Data'
Global Const $tagEXTLOGPEN = 'dword PenStyle;dword Width;uint BrushStyle;dword Color;ulong_ptr Hatch;dword NumEntries' ; & ';dword StyleEntry[n]'
Global Const $tagLOGBRUSH = 'uint Style;dword Color;ulong_ptr Hatch'
Global Const $tagLOGPEN = 'uint Style;dword Width;dword Color'
Global Const $tagLUID = 'dword LowPart;long HighPart'
Global Const $tagNOTIFYICONDATA = 'dword Size;hwnd hWnd;uint ID;uint Flags;uint CallbackMessage;ptr hIcon;wchar Tip[128];dword State;dword StateMask;wchar Info[256];uint Version;wchar InfoTitle[64];dword InfoFlags'
Global Const $tagNOTIFYICONDATA_XP = $tagNOTIFYICONDATA & ';uint GUID'
Global Const $tagNOTIFYICONDATA_VISTA = $tagNOTIFYICONDATA_XP & ';ptr hBalloonIcon'
Global Const $tagSHFILEINFO = 'ptr hIcon;int iIcon;dword Attributes;wchar DisplayName[260];wchar TypeName[80]'
Global Const $tagOSVERSIONINFO = 'dword OSVersionInfoSize;dword MajorVersion;dword MinorVersion;dword BuildNumber;dword PlatformId;wchar CSDVersion[128]'
Global Const $tagOSVERSIONINFOEX = $tagOSVERSIONINFO & ';short ServicePackMajor;short ServicePackMinor;short SuiteMask;byte ProductType;byte Reserved'
Global Const $tagPAINTSTRUCT = 'hwnd hDC;int fErase;dword rPaint[4];int fRestore;int fIncUpdate;byte rgbReserved[32]'
Global Const $tagPROCESS_MEMORY_COUNTERS = 'dword Size;dword PageFaultCount;long PeakWorkingSetSize;long WorkingSetSize;long QuotaPeakPagedPoolUsage;long QuotaPagedPoolUsage;long QuotaPeakNonPagedPoolUsage;long QuotaNonPagedPoolUsage;long PagefileUsage;long PeakPagefileUsage'
Global Const $tagSTORAGE_DEVICE_NUMBER = 'dword DeviceType;ulong DeviceNumber;ulong PartitionNumber'
;lobal Const $tagTEXTMETRIC = 'long tmHeight;long tmAscent;long tmDescent;long tmInternalLeading;long tmExternalLeading;long tmAveCharWidth;long tmMaxCharWidth;long tmWeight;long tmOverhang;long tmDigitizedAspectX;long tmDigitizedAspectY;wchar tmFirstChar;wchar tmLastChar;wchar tmDefaultChar;wchar tmBreakChar;byte tmItalic;byte tmUnderlined;byte tmStruckOut;byte tmPitchAndFamily;byte tmCharSet'
Global Const $tagWINDOWINFO = 'dword Size;dword rWindow[4];dword rClient[4];dword Style;dword ExStyle;dword WindowStatus;uint cxWindowBorders;uint cyWindowBorders;ushort atomWindowType;ushort CreatorVersion'
Global Const $tagDRIVERSTATUS = "byte DriverError;byte IDEStatus;byte Reserved1[2];dword Reserved2[2]"
Global Const $tagIDEREGS = "byte Features;byte SectorCount;byte SectorNumber;byte CylLow;byte CylHigh;byte DriveHead;byte Command;byte Reserved"
Global Const $tagSTORAGEPROPERTYQUERY = "int PropertyId;int QueryType;byte AdditionalParams"
Global Const $tagSTORAGEDEVICEDESCRIPTOR = "ulong Version;ulong Size;byte DeviceType;byte DeviceTypeModifier;byte RemovableMedia;byte CommandQueueing;ulong VendorIdOffset;ulong ProductIdOffset;ulong ProductRevisionOffset;ulong SerialNumberOffset;int BusType;ulong RawPropertiesLength;byte RawDeviceProperties"
Global Const $tagSENDCMDOUTPARAMS = "dword Size;" & $tagDRIVERSTATUS
Global Const $tagSENDCMDINPARAMS = "dword Size;" & $tagIDEREGS & ";byte DriveNumber;byte Reserved1[3];dword Reserved2[4];byte Buffer"
#EndRegion Global Variables and Constants
#Region Local Variables and Constants
Global $__Data, $__RGB = True
#EndRegion Local Variables and Constants
#Region Public Functions
; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_GetDiskSerialNumber
; Description....: Retrieves information about the amount of space that is available on a disk volume.
; Syntax.........: _WinAPI_GetDiskFreeSpaceEx ( $sDrive )
; Parameters.....: $sDrive - The drive to retrieve information, in the format D:, E:, etc.
; Return values..: Success - The array containing the following parameters:
; [0] - The total number of available free bytes on a disk. If per-user quotas are being used, this value
; may be less than the total number of free bytes on a disk.
; [1] - The total number of available bytes on a disk. If per-user quotas are being used, this value
; may be less than the total number of bytes on a disk.
; [2] - The total number of free bytes on a disk.
; Failure - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ GetDiskFreeSpaceEx
; Example........: Yes
; ===============================================================================================================================
Func _WinAPI_GetDiskSerialNumber($sPhysDisk)
$hDisk = _WinAPI_CreateFile("\\." & $sPhysDisk, 2, 6, 6, 1)
If $hDisk = -1 Then
Exit(ConsoleWrite("_WinAPI_CreateFile fails, error code: " & _WinAPI_GetLastError() & @CRLF))
EndIf
$tSCIP = DllStructCreate($tagSENDCMDINPARAMS)
$tSCOP = DllStructCreate($tagSENDCMDOUTPARAMS & ";char Buffer[1024]")
$tQuery = DllStructCreate($tagSTORAGEPROPERTYQUERY)
$tDescr = DllStructCreate($tagSTORAGEDEVICEDESCRIPTOR)
DllStructSetData($tQuery, "PropertyId", 0)
DllStructSetData($tQuery, "QueryType", 0)
$iResult = __DeviceIoControl($hDisk, $IOCTL_STORAGE_QUERY_PROPERTY, _
$tQuery, DllStructGetSize($tQuery), _
$tDescr, DllStructGetSize($tDescr))
If $iResult = 0 Then
Exit(ConsoleWrite("_DeviceIoControl fails, error code: " & @error & @CRLF))
EndIf
If DllStructGetData($tDescr, "BusType") = 2 Then
DllStructSetData($tSCIP, "Command", 0xA1)
Else
DllStructSetData($tSCIP, "Command", 0xEC)
EndIf
$iResult = __DeviceIoControl($hDisk, $DFP_RECEIVE_DRIVE_DATA, _
$tSCIP, DllStructGetSize($tSCIP), _
$tSCOP, DllStructGetSize($tSCOP))
If $iResult = 0 Then
Exit(ConsoleWrite("_DeviceIoControl fails, error code: " & @error & @CRLF))
EndIf
Dim $sSerialNum
For $i = 21 to 40 Step 2
$sSerialNum &= DllStructGetData($tSCOP, "Buffer", $i + 1)
$sSerialNum &= DllStructGetData($tSCOP, "Buffer", $i)
Next
$sSerialNum = StringReplace($sSerialNum, " ", "")
$tSCIP = 0
$tSCOP = 0
$tQuery = 0
$tDescr = 0
_WinAPI_CloseHandle($hDisk)
Return $sSerialNum
EndFunc
#EndRegion Public Functions
#Region Internal Functions
Func __MakeCtrlCode($iDevType, $iFunction, $iMethod = 0, $iAccess = 0)
Return bitOR(bitShift($iDevType,-16),bitShift($iAccess, -14),bitShift($iFunction, -2), $iMethod)
EndFunc ;==>_MakeCtrlCode
Func __DeviceIoControl($hDevice, $iCtrlCode, $pIn, $iIn, $pOut, $iOut, $pOverlapped = 0)
Local $iResult
If IsDllStruct($pIn) Then $pIn = DllStructGetPtr($pIn)
If IsDllStruct($pOut) Then $pOut = DllStructGetPtr($pOut)
If IsDllStruct($pOverlapped) Then $pOverlapped = DllStructGetPtr($pOverLapped)
$iResult = DllCall("Kernel32.dll", "int", "DeviceIoControl", "ptr", $hDevice, _
"dword", $iCtrlCode, "ptr", $pIn, "dword", $iIn, _
"ptr", $pOut, "dword", $iOut, "int*", 0, "ptr", $pOverlapped)
Return SetError(_WinAPI_GetLastError(), $iResult[7], $iResult[0])
EndFunc ;==>_DeviceIoControl
#EndRegion Internal Functions
;程序调用方法
;MsgBox(0,"",_Winapi_GetDiskSerialNumber("D:"))