Get Disk Volume Serial Number: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Added a macro for easier usage)
(Fixed example usage)
 
(One intermediate revision by one other user not shown)
Line 18: Line 18:
To then convert this serial number into hexadecimal:
To then convert this serial number into hexadecimal:
<highlight-nsis>
<highlight-nsis>
IntFmt $0 %08X $0
IntFmt $0 "%08X" $0
</highlight-nsis>
</highlight-nsis>


Line 36: Line 36:
!macro GetDiskVolumeSerialNumberHex _SERIAL _DRIVE
!macro GetDiskVolumeSerialNumberHex _SERIAL _DRIVE
   ${GetDiskVolumeSerialNumber} ${_SERIAL} ${_DRIVE}
   ${GetDiskVolumeSerialNumber} ${_SERIAL} ${_DRIVE}
   IntFmt ${_SERIAL} %08X ${_SERIAL}
   IntFmt ${_SERIAL} "%08X" ${_SERIAL}
!macroend
!macroend
!define GetDiskVolumeSerialNumberHex "!insertmacro GetDiskVolumeSerialNumberHex"
!define GetDiskVolumeSerialNumberHex "!insertmacro GetDiskVolumeSerialNumberHex"
Line 44: Line 44:
<highlight-nsis>
<highlight-nsis>
ReadEnvStr $0 SystemDrive
ReadEnvStr $0 SystemDrive
StrCpy $0 "$0\"
; Find the serial number
; Find the serial number
${GetDiskVolumeSerialNumber} $1 $0\
${GetDiskVolumeSerialNumber} $1 $0
; Or find it as hex
; Or find it as hex
${GetDiskVolumeSerialNumberHex} $1 $0\
${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