Get Disk Volume Serial Number: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(To view Disc Serial Number in hexsadecimal format)
(Fixed example usage)
 
(5 intermediate revisions by 3 users not shown)
Line 7: Line 7:
; $0 - Drive to check returns the Disc Serial Number
; $0 - Drive to check returns the Disc Serial Number
Function GetDiskVolumeSerialNumber
Function GetDiskVolumeSerialNumber
   !define GetVolumeInformation "Kernel32::GetVolumeInformation(t,*t,i,*i,*i,*i,t,i) i"
   !define GetVolumeInformation "Kernel32::GetVolumeInformation(t,t,i,*i,*i,*i,t,i) i"
   System::Call '${GetVolumeInformation}("$0",,${NSIS_MAX_STRLEN},.r0,,,,${NSIS_MAX_STRLEN})'
   System::Call '${GetVolumeInformation}("$0",,${NSIS_MAX_STRLEN},.r0,,,,${NSIS_MAX_STRLEN})'
FunctionEnd  ;GetDiskVolumeSerialNumber
FunctionEnd  ;GetDiskVolumeSerialNumber
Line 16: Line 16:
http://forums.winamp.com/showthread.php?postid=1320397#post1320397
http://forums.winamp.com/showthread.php?postid=1320397#post1320397


To then convert this serial number into hexadecimal:
<highlight-nsis>
IntFmt $0 "%08X" $0
</highlight-nsis>
== Macro ==
This macro was created from the code above by [[User:ChrisMorgan|Chris Morgan]].
<highlight-nsis>
!define _GetVolumeInformationSysCall "Kernel32::GetVolumeInformation(t,t,i,*i,*i,*i,t,i) i"
!macro GetDiskVolumeSerialNumber _SERIAL _DRIVE
  Push $0
  System::Call '${_GetVolumeInformationSysCall}("${_DRIVE}",,${NSIS_MAX_STRLEN},.r0,,,,${NSIS_MAX_STRLEN})'
  Exch $0
  Pop ${_SERIAL}
!macroend
!define GetDiskVolumeSerialNumber "!insertmacro GetDiskVolumeSerialNumber"


!macro GetDiskVolumeSerialNumberHex _SERIAL _DRIVE
  ${GetDiskVolumeSerialNumber} ${_SERIAL} ${_DRIVE}
  IntFmt ${_SERIAL} "%08X" ${_SERIAL}
!macroend
!define GetDiskVolumeSerialNumberHex "!insertmacro GetDiskVolumeSerialNumberHex"
</highlight-nsis>
Example usage, to find the serial number of the system drive:
<highlight-nsis>
<highlight-nsis>
; To view Disc Serial Number in hexsadecimal format
ReadEnvStr $0 SystemDrive
IntFmt $0 "%08X" $0
StrCpy $0 "$0\"
; Find the serial number
${GetDiskVolumeSerialNumber} $1 $0
; Or find it as hex
${GetDiskVolumeSerialNumberHex} $1 $0
</highlight-nsis>
</highlight-nsis>
[[Category:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Latest revision as of 20:25, 17 August 2012

Author: b_avery@yahoo.com (talk, contrib)


Description

Do you need to find out the Disc Serial Number, well here you go, $0 - Drive to check returns the Disc Serial Number

; $0 - Drive to check returns the Disc Serial Number
Function GetDiskVolumeSerialNumber
  !define GetVolumeInformation "Kernel32::GetVolumeInformation(t,t,i,*i,*i,*i,t,i) i"
  System::Call '${GetVolumeInformation}("$0",,${NSIS_MAX_STRLEN},.r0,,,,${NSIS_MAX_STRLEN})'
FunctionEnd  ;GetDiskVolumeSerialNumber

This has been created as a result of:

http://forums.winamp.com/showthread.php?postid=1320397#post1320397

To then convert this serial number into hexadecimal:

IntFmt $0 "%08X" $0


Macro

This macro was created from the code above by Chris Morgan.

!define _GetVolumeInformationSysCall "Kernel32::GetVolumeInformation(t,t,i,*i,*i,*i,t,i) i"
!macro GetDiskVolumeSerialNumber _SERIAL _DRIVE
  Push $0
  System::Call '${_GetVolumeInformationSysCall}("${_DRIVE}",,${NSIS_MAX_STRLEN},.r0,,,,${NSIS_MAX_STRLEN})'
  Exch $0
  Pop ${_SERIAL}
!macroend
!define GetDiskVolumeSerialNumber "!insertmacro GetDiskVolumeSerialNumber"
 
!macro GetDiskVolumeSerialNumberHex _SERIAL _DRIVE
  ${GetDiskVolumeSerialNumber} ${_SERIAL} ${_DRIVE}
  IntFmt ${_SERIAL} "%08X" ${_SERIAL}
!macroend
!define GetDiskVolumeSerialNumberHex "!insertmacro GetDiskVolumeSerialNumberHex"

Example usage, to find the serial number of the system drive:

ReadEnvStr $0 SystemDrive
StrCpy $0 "$0\"
; Find the serial number
${GetDiskVolumeSerialNumber} $1 $0
; Or find it as hex
${GetDiskVolumeSerialNumberHex} $1 $0