Code: Select all
MsgBox % GetScriptDiskSerialNumber()
GetScriptDiskSerialNumber()
{
static OPEN_EXISTING := 3
static METHOD_BUFFERED := 0
static FILE_ANY_ACCESS := 0
static FILE_DEVICE_MASS_STORAGE := 0x0000002d
static IOCTL_STORAGE_BASE := FILE_DEVICE_MASS_STORAGE
static IOCTL_STORAGE_GET_DEVICE_NUMBER := CTL_CODE(IOCTL_STORAGE_BASE, 0x0420, METHOD_BUFFERED, FILE_ANY_ACCESS)
static SCRIPT_DIR := "\\.\" SubStr(A_ScriptDir, 1, 2)
hFile := DllCall("CreateFile", "Str", SCRIPT_DIR, "UInt", 0, "UInt", 0, "Ptr", 0, "UInt", OPEN_EXISTING, "UInt", 0, "Ptr", 0, "Ptr")
if (hFile = -1)
{
DllCall("CloseHandle", "Ptr", hFile)
return False
}
BufSize := VarSetCapacity(Buf, 12)
STORAGE_DEVICE_NUMBER := DllCall("DeviceIoControl", "Ptr", hFile, "UInt", IOCTL_STORAGE_GET_DEVICE_NUMBER, "Ptr", 0, "UInt", 0, "Ptr", &Buf, "UInt", BufSize, "UInt*", SizeOut, "Ptr", 0)
if (STORAGE_DEVICE_NUMBER = 0)
{
DllCall("CloseHandle", "Ptr", hFile)
return False
}
DeviceNumber := NumGet(Buf, 4, "Uint")
DllCall("CloseHandle", "Ptr", hFile)
for objItem in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_DiskDrive")
{
if (objItem.Index = DeviceNumber)
SerialNumber := objItem.SerialNumber
}
return SerialNumber
}
CTL_CODE(DeviceType, Function, Method, Access)
{
return ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
}