GetDrives: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{|align=right
{{PageAuthor|Instructor}}
|<small>Author: [[{{ns:2}}:Instructor|Instructor]] ([[{{ns:3}}:Instructor|talk]], [[{{ns:-1}}:Contributions/Instructor|contrib]])</small>
|}
<br style="clear:both;">
== Links ==
; Latest version of headers "nsh.zip":
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost


If function used without header then put function in script before call it
{{User:Instructor/Headers/Template}}


== The Function ==
== Function Description ==
<highlight-nsis>/*
 
<highlight-nsis>
____________________________________________________________________________
____________________________________________________________________________


                             GetDrives v1.3
                             GetDrives v1.5
____________________________________________________________________________
____________________________________________________________________________


Line 21: Line 16:
Find all available drives in the system.
Find all available drives in the system.


Include these headers:
!include "FileFunc.nsh"
!insertmacro GetDrives


Syntax:
Syntax:
Line 35: Line 33:
                 ;  Find all drives by letter (default)
                 ;  Find all drives by letter (default)
                 ;
                 ;
"Function"      ; Callback function then found
"Function"      ; Callback function when found


Function "Function"
Function "Function"
Line 48: Line 46:
Push $var    ; If $var="StopGetDrives" Then exit from function
Push $var    ; If $var="StopGetDrives" Then exit from function
FunctionEnd
FunctionEnd
</highlight-nsis>






Example1:
<b>Example1:</b>
Section
<highlight-nsis>Section
${GetDrives} "FDD+CDROM" "Example1"
${GetDrives} "FDD+CDROM" "Example1"
SectionEnd
SectionEnd
Line 61: Line 60:
Push $0
Push $0
FunctionEnd
FunctionEnd
</highlight-nsis>






Example2:
<b>Example2:</b>
Section
<highlight-nsis>Section
${GetDrives} "ALL" "Example2"
${GetDrives} "ALL" "Example2"
SectionEnd
SectionEnd
Line 74: Line 74:
Push $0
Push $0
FunctionEnd
FunctionEnd
</highlight-nsis>






Example3 (Get type of drive):
<b>Example3 (Get type of drive):</b>
Section
<highlight-nsis>Section
StrCpy $R0 "D:\"      ;Drive letter
StrCpy $R0 "D:\"      ;Drive letter
StrCpy $R1 "invalid"
StrCpy $R1 "invalid"
Line 93: Line 94:


Push $0
Push $0
FunctionEnd*/
FunctionEnd
</highlight-nsis>


== Function Code ==


;---------------------------------------------------------------------------
<highlight-nsis>
Function GetDrives
Function GetDrives
!define GetDrives `!insertmacro GetDrivesCall`
!define GetDrives `!insertmacro GetDrivesCall`
 
!macro GetDrivesCall _DRV _FUNC
!macro GetDrivesCall _DRV _FUNC
Push $0
Push $0
Line 117: Line 120:
Push $4
Push $4
Push $5
Push $5
Push $6
Push $8
Push $8
Push $9
Push $9


System::Alloc 1024
System::Alloc /NOUNLOAD 1024
Pop $2
Pop $2
System::Call /NOUNLOAD 'kernel32::GetLogicalDriveStringsA(i,i) i(1024, r2)'


StrCmp $0 ALL drivestring
StrCmp $0 ALL drivestring
Line 129: Line 134:


typeset:
typeset:
StrCpy $5 -1
StrCpy $6 -1
IntOp $5 $5 + 1
IntOp $6 $6 + 1
StrCpy $8 $0 1 $5
StrCpy $8 $0 1 $6
StrCmp $8$0 '' enumex
StrCmp $8$0 '' enumex
StrCmp $8 '' +2
StrCmp $8 '' +2
StrCmp $8 '+' 0 -4
StrCmp $8 '+' 0 -4
StrCpy $8 $0 $5
StrCpy $8 $0 $6
IntOp $5 $5 + 1
IntOp $6 $6 + 1
StrCpy $0 $0 '' $5
StrCpy $0 $0 '' $6


StrCmp $8 'FDD' 0 +3
StrCmp $8 'FDD' 0 +3
StrCpy $5 2
StrCpy $6 2
goto drivestring
goto drivestring
StrCmp $8 'HDD' 0 +3
StrCmp $8 'HDD' 0 +3
StrCpy $5 3
StrCpy $6 3
goto drivestring
goto drivestring
StrCmp $8 'NET' 0 +3
StrCmp $8 'NET' 0 +3
StrCpy $5 4
StrCpy $6 4
goto drivestring
goto drivestring
StrCmp $8 'CDROM' 0 +3
StrCmp $8 'CDROM' 0 +3
StrCpy $5 5
StrCpy $6 5
goto drivestring
goto drivestring
StrCmp $8 'RAM' 0 typeset
StrCmp $8 'RAM' 0 typeset
StrCpy $5 6
StrCpy $6 6


drivestring:
drivestring:
System::Call 'kernel32::GetLogicalDriveStringsA(i,i) i(1024,r2)'
StrCpy $3 $2


enumok:
enumok:
System::Call 'kernel32::lstrlenA(t) i(i r2) .r3'
System::Call /NOUNLOAD 'kernel32::lstrlenA(t) i(i r3) .r4'
StrCmp $3$0 '0ALL' enumex
StrCmp $4$0 '0ALL' enumex
StrCmp $3 0 typeset
StrCmp $4 0 typeset
System::Call 'kernel32::GetDriveTypeA(t) i (i r2) .r4'
System::Call /NOUNLOAD 'kernel32::GetDriveTypeA(t) i(i r3) .r5'


StrCmp $0 ALL +2
StrCmp $0 ALL +2
StrCmp $4 $5 letter enumnext
StrCmp $5 $6 letter enumnext
StrCmp $4 2 0 +3
StrCmp $5 2 0 +3
StrCpy $8 FDD
StrCpy $8 FDD
goto letter
goto letter
StrCmp $4 3 0 +3
StrCmp $5 3 0 +3
StrCpy $8 HDD
StrCpy $8 HDD
goto letter
goto letter
StrCmp $4 4 0 +3
StrCmp $5 4 0 +3
StrCpy $8 NET
StrCpy $8 NET
goto letter
goto letter
StrCmp $4 5 0 +3
StrCmp $5 5 0 +3
StrCpy $8 CDROM
StrCpy $8 CDROM
goto letter
goto letter
StrCmp $4 6 0 enumex
StrCmp $5 6 0 enumex
StrCpy $8 RAM
StrCpy $8 RAM


letter:
letter:
System::Call '*$2(&t1024 .r9)'
System::Call /NOUNLOAD '*$3(&t1024 .r9)'


Push $0
Push $0
Line 189: Line 194:
Push $4
Push $4
Push $5
Push $5
Push $6
Push $8
Push $8
Call $1
Call $1
Pop $9
Pop $9
Pop $8
Pop $8
Pop $6
Pop $5
Pop $5
Pop $4
Pop $4
Line 202: Line 209:


enumnext:
enumnext:
IntOp $2 $2 + $3
IntOp $3 $3 + $4
IntOp $2 $2 + 1
IntOp $3 $3 + 1
goto enumok
goto enumok


Line 211: Line 218:
Pop $9
Pop $9
Pop $8
Pop $8
Pop $6
Pop $5
Pop $5
Pop $4
Pop $4
Line 217: Line 225:
Pop $1
Pop $1
Pop $0
Pop $0
FunctionEnd</highlight-nsis>
FunctionEnd
</highlight-nsis>
 
[[Category:Disk, Path & File Functions]]

Latest revision as of 12:22, 22 July 2007

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