GetDrives: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
Line 35: Line 35:
                 ;  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"

Revision as of 10:29, 12 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.4
____________________________________________________________________________
 
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 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

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