GetDrives

From NSIS Wiki
Jump to navigationJump to search
Author: Instructor (talk, contrib)


Page for NSIS 2.07 and below users

You can use the latest version of headers (recommended) or the following function code (put the function code in your script before calling it)

Function Description

____________________________________________________________________________
 
                            GetDrives v1.5
____________________________________________________________________________
 
Thanks deguix (Based on his idea of Function "DetectDrives")
 
 
Find all available drives in the system.
 
Include these headers:
!include "FileFunc.nsh"
!insertmacro GetDrives
 
Syntax:
${GetDrives} "[Option]" "Function"
 
"[Option]"      ; [FDD+HDD+CDROM+NET+RAM]
                ;   FDD    Floppy Disk Drives
                ;   HDD    Hard Disk Drives 
                ;   CDROM  CD-ROM Drives
                ;   NET    Network Drives
                ;   RAM    RAM Disk Drives
                ;
                ; [ALL]
                ;   Find all drives by letter (default)
                ;
"Function"      ; Callback function when found
 
Function "Function"
	; $9    "drive letter"  (a:\ c:\ ...)
	; $8    "drive type"    (FDD HDD ...)
 
 
	; $R0-$R9  are not used (save data in them).
	; ...
 
 
	Push $var    ; If $var="StopGetDrives" Then exit from function
FunctionEnd


Example1:

Section
	${GetDrives} "FDD+CDROM" "Example1"
SectionEnd
 
Function Example1
	MessageBox MB_OK "$9  ($8 Drive)"
 
	Push $0
FunctionEnd


Example2:

Section
	${GetDrives} "ALL" "Example2"
SectionEnd
 
Function Example2
	MessageBox MB_OK "$9  ($8 Drive)"
 
	Push $0
FunctionEnd


Example3 (Get type of drive):

Section
	StrCpy $R0 "D:\"      ;Drive letter
	StrCpy $R1 "invalid"
 
	${GetDrives} "ALL" "Example3"
 
	MessageBox MB_OK "Type of drive $R0 is $R1"
SectionEnd
 
Function Example3
	StrCmp $9 $R0 0 +3
	StrCpy $R1 $8
	StrCpy $0 StopGetDrives
 
	Push $0
FunctionEnd

Function Code

Function GetDrives
	!define GetDrives `!insertmacro GetDrivesCall`
 
	!macro GetDrivesCall _DRV _FUNC
		Push $0
		Push `${_DRV}`
		GetFunctionAddress $0 `${_FUNC}`
		Push `$0`
		Call GetDrives
		Pop $0
	!macroend
 
	Exch $1
	Exch
	Exch $0
	Exch
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $8
	Push $9
 
	System::Alloc /NOUNLOAD 1024
	Pop $2
	System::Call /NOUNLOAD 'kernel32::GetLogicalDriveStringsA(i,i) i(1024, r2)'
 
	StrCmp $0 ALL drivestring
	StrCmp $0 '' 0 typeset
	StrCpy $0 ALL
	goto drivestring
 
	typeset:
	StrCpy $6 -1
	IntOp $6 $6 + 1
	StrCpy $8 $0 1 $6
	StrCmp $8$0 '' enumex
	StrCmp $8 '' +2
	StrCmp $8 '+' 0 -4
	StrCpy $8 $0 $6
	IntOp $6 $6 + 1
	StrCpy $0 $0 '' $6
 
	StrCmp $8 'FDD' 0 +3
	StrCpy $6 2
	goto drivestring
	StrCmp $8 'HDD' 0 +3
	StrCpy $6 3
	goto drivestring
	StrCmp $8 'NET' 0 +3
	StrCpy $6 4
	goto drivestring
	StrCmp $8 'CDROM' 0 +3
	StrCpy $6 5
	goto drivestring
	StrCmp $8 'RAM' 0 typeset
	StrCpy $6 6
 
	drivestring:
	StrCpy $3 $2
 
	enumok:
	System::Call /NOUNLOAD 'kernel32::lstrlenA(t) i(i r3) .r4'
	StrCmp $4$0 '0ALL' enumex
	StrCmp $4 0 typeset
	System::Call /NOUNLOAD 'kernel32::GetDriveTypeA(t) i(i r3) .r5'
 
	StrCmp $0 ALL +2
	StrCmp $5 $6 letter enumnext
	StrCmp $5 2 0 +3
	StrCpy $8 FDD
	goto letter
	StrCmp $5 3 0 +3
	StrCpy $8 HDD
	goto letter
	StrCmp $5 4 0 +3
	StrCpy $8 NET
	goto letter
	StrCmp $5 5 0 +3
	StrCpy $8 CDROM
	goto letter
	StrCmp $5 6 0 enumex
	StrCpy $8 RAM
 
	letter:
	System::Call /NOUNLOAD '*$3(&t1024 .r9)'
 
	Push $0
	Push $1
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $8
	Call $1
	Pop $9
	Pop $8
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
	StrCmp $9 'StopGetDrives' enumex
 
	enumnext:
	IntOp $3 $3 + $4
	IntOp $3 $3 + 1
	goto enumok
 
	enumex:
	System::Free $2
 
	Pop $9
	Pop $8
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
FunctionEnd