GetDrives: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
Line 104: Line 104:
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 122: Line 122:
Push $4
Push $4
Push $5
Push $5
Push $6
Push $8
Push $8
Push $9
Push $9
Line 127: Line 128:
System::Alloc 1024
System::Alloc 1024
Pop $2
Pop $2
System::Call 'kernel32::GetLogicalDriveStringsA(i,i) i(1024, r2)'


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


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 '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 '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 '*$3(&t1024 .r9)'


Push $0
Push $0
Line 194: Line 196:
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 207: Line 211:


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


Line 216: Line 220:
Pop $9
Pop $9
Pop $8
Pop $8
Pop $6
Pop $5
Pop $5
Pop $4
Pop $4
Line 222: Line 227:
Pop $1
Pop $1
Pop $0
Pop $0
FunctionEnd</highlight-nsis>
FunctionEnd
</highlight-nsis>


[[Category:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Revision as of 22:48, 4 July 2005

Author: Instructor (talk, contrib)


Links

Latest version of headers "nsh.zip"
http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost

If a function is used without header, put the function code in your script before calling it.

The Function Description

____________________________________________________________________________
 
                            GetDrives v1.3
____________________________________________________________________________
 
Thanks deguix (Based on his idea of Function "DetectDrives")
 
 
Find all available drives in the system.
 
 
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 then 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

The 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 1024
	Pop $2
	System::Call '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 'kernel32::lstrlenA(t) i(i r3) .r4'
	StrCmp $4$0 '0ALL' enumex
	StrCmp $4 0 typeset
	System::Call '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 '*$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