_WriteDriveState() permet de masquer/verrouiller des lecteurs de disque.
_ReadDriveState() permet de savoir quel sont les lecteurs masqués/verrouillés.
Voir les exemples inclus (entre les balises #CS et #CE) ainsi que la description des fonctions.
Vous devrez re-démarrer l'ordinateur pour prendre en compte la stratégie appliquée.
► Afficher le texteTroisième version
► Afficher le texte
Code : Tout sélectionner
Opt("MustDeclareVars", 1)
Global $sKey = '\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer'
Global $DriveLetter[27] = ['ZYXWVUTSRQPONMLKJIHGFEDCBA', 'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', _
'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A']
Global $DriveVar[27] = [67108863, 33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, _
65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
#CS Exemples :
#include<array.au3>
_WriteDriveState('E', 1) ; Hide drive E: for current user key.
_WriteDriveState('F', 1, 1) ; Hide and Lock drive F: for current user key.
_WriteDriveState('A', 0, 1, 'HKLM') ; Lock drive A: for all users key.
Local $x = _ReadDriveState()
MsgBox(64, 'Drives Info', _
"Hidden drive(s) on HKCU = " & $x[0] & @CRLF & _
"Hidden drive(s) on HKLM = " & $x[1] & @CRLF & _
"Locked drive(s) on HKCU = " & $x[2] & @CRLF & _
"Locked drive(s) on HKLM = " & $x[3] & @TAB & @TAB)
;_ArrayDisplay($x,"Drives State")
MsgBox(0, 'Change State', 'Now we change the state of all drives on all keys') ; Message information.
_WriteDriveState('') ; UnHide and UnLock all drives for HKCU and HKLM keys (reset)
Local $x = _ReadDriveState()
MsgBox(64, 'Drives Info', _
"Hidden drive(s) on HKCU = " & $x[0] & @CRLF & _
"Hidden drive(s) on HKLM = " & $x[1] & @CRLF & _
"Locked drive(s) on HKCU = " & $x[2] & @CRLF & _
"Locked drive(s) on HKLM = " & $x[3] & @TAB & @TAB)
#CE
; #FUNCTION# ===========================================================================================
; Name: _WriteDriveState
; Description: Change the state of drives for local user or all users
; Syntax: _WriteDriveState($sDriveLetter, [$iHide = True, [$iLock = False, [$sHKey = 'HKCU']]])
; Parameter(s): $sDriveLetter = The drive(s) letter to hide (can be some letters or value 'all')
; iHide = optional: True: hide drive(s) (default)
; False: unhide drive(s)
; iLock = optional: True: Lock drive(s)
; False: Unlock drive(s) (default)
; $sHKey = optional: HKCU: For the current user (default)
; HKLM: For all users
; Requirement(s):
; Return Value(s): on success: 1
; on error: 0 - Extended is set to : 1 error writing NoDrives Key
; 2 error writing NoViewOnDrive Key
; 3 error writing NoDrives and NoViewOnDrive Key
; 4 argument error
; 5 Error on reset all drives on all key
; Author(s): Tlem
; ====================================================================================================
Func _WriteDriveState($sDriveLetter = '', $iHide = True, $iLock = False, $sHKey = 'HKCU')
If Not (StringIsAlpha($sDriveLetter) Or $sDriveLetter = '') Then Return SetError(1, 4, 0)
If Not ($iHide = True or $iHide = False) Then Return SetError(1, 4, 0)
If Not ($iLock = True or $iLock = False) Then Return SetError(1, 4, 0)
If Not ($sHKey = 'HKCU' Or $sHKey = 'HKLM') Then Return SetError(1, 4, 0)
Local $iDataHide, $iDataLock, $sVar, $aDrives, $i, $ii
Local $Ret = 1, $iRetHide = 0, $iRetLock = 0, $iError = 0, $iExt = 0
If $sDriveLetter = '' Then
$iRetHide = RegWrite('HKCU' & $sKey, 'NoDrives', 'REG_DWORD', 0)
If @error Then
$iError = @error
$Ret = 0
EndIf
$iRetLock = RegWrite('HKCU' & $sKey, 'NoViewOnDrive', 'REG_DWORD', 0)
If @error Then
$iError = BitOR($iError, @error)
$Ret = BitAnd($Ret, 0)
EndIf
$iRetHide = RegWrite('HKLM' & $sKey, 'NoDrives', 'REG_DWORD', 0)
If @error Then
$iError = BitOR($iError, @error)
$Ret = BitAnd($Ret, 0)
EndIf
$iRetLock = RegWrite('HKLM' & $sKey, 'NoViewOnDrive', 'REG_DWORD', 0)
If @error Then
$iError = BitOR($iError, @error)
$Ret = BitAnd($Ret, 0)
EndIf
If $iError Then $iExt = 5
Return SetError($iError, $iExt, $Ret)
EndIf
$sHKey = $sHKey & $sKey
$aDrives = StringSplit(StringUpper($sDriveLetter), '')
$iDataHide = RegRead($sHKey, 'NoDrives')
If $iDataHide = '' Then $iDataHide = 0
$iDataLock = RegRead($sHKey, 'NoViewOnDrive')
If $iDataLock = '' Then $iDataLock = 0
If StringInStr($sDriveLetter, 'all') Then
$sVar = $DriveVar[0]
Else
For $i = 1 To $aDrives[0]
For $ii = 0 To 26
If $aDrives[$i] = $DriveLetter[$ii] Then $sVar += $DriveVar[$ii]
Next
Next
EndIf
If $iHide = True Then
$iRetHide = RegWrite($sHKey, 'NoDrives', 'REG_DWORD', BitOR($iDataHide, $sVar))
Else
$iRetHide = RegWrite($sHKey, 'NoDrives', 'REG_DWORD', BitAND($iDataHide, BitNOT($sVar)))
EndIf
If @error Then
$iError = @error
$iExt = 1
EndIf
If $iLock = True Then
$iRetLock = RegWrite($sHKey, 'NoViewOnDrive', 'REG_DWORD', BitOR($iDataLock, $sVar))
Else
$iRetLock = RegWrite($sHKey, 'NoViewOnDrive', 'REG_DWORD', BitAND($iDataLock, BitNOT($sVar)))
EndIf
If @error Then
$iError = BitOR($iError, @error)
$iExt = Bitor($iExt, 2)
EndIf
Return SetError($iError, $iExt, BitAnd($iRetHide, $iRetLock))
EndFunc ;==>_WriteDriveState
; #FUNCTION# ===========================================================================================
; Name: _ReadDriveState
; Description: Read the state of drive(s) on explorer for local user or all users
; Syntax: _ReadDriveState()
; Parameter(s):
; Requirement(s):
; Return Value(s): on success: an array of strings with drive(s) letter(s):
; - $array[0]: Hidden drive(s) for HKCU
; - $array[1]: Hidden drive(s) for HKLM
; - $array[2]: Locked drive(s) for HKCU
; - $array[3]: Locked drive(s) for HKLM
; on error: 0 (argument error)
; Author(s): Tlem
; ====================================================================================================
Func _ReadDriveState()
Local $aDriveList[4] = ['HKCU' & $sKey & ';' & 'NoDrives', 'HKLM' & $sKey & ';' & 'NoDrives', _
'HKCU' & $sKey & ';' & 'NoViewOnDrive', 'HKLM' & $sKey & ';' & 'NoViewOnDrive']
Local $sDriveList, $sReverse, $iData, $iCount, $Search, $i, $j
For $i = 0 To 3
$sDriveList = ''
$Search = StringSplit($aDriveList[$i], ';')
$iData = RegRead($Search[1], $Search[2])
If Not (@error Or $iData = 0) Then
For $j = 0 To 26
If $iData >= $DriveVar[$j] Then
$iData = $iData - $DriveVar[$j]
$sDriveList &= $DriveLetter[$j]
EndIf
Next
If StringLen($sDriveList) >= 1 Then
$sReverse = ''
For $iCount = 1 To StringLen($sDriveList)
$sReverse = StringMid($sDriveList, $iCount, 1) & $sReverse
Next
$aDriveList[$i] = $sReverse
EndIf
Else
$aDriveList[$i] = ''
EndIf
Next
Return $aDriveList
EndFunc ;==>_ReadDriveState
► Afficher le texteDeuxième version
Code : Tout sélectionner
Opt("MustDeclareVars", 1)
Global $sKey = '\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer'
Global $DriveLetter[27] = ['ZYXWVUTSRQPONMLKJIHGFEDCBA', 'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', _
'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A']
Global $DriveVar[27] = [67108863, 33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, _
65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
#CS Exemples :
_WriteDriveState('F', 'hide') ; Hide drive F: for current user key.
_WriteDriveState('F', 'lock') ; Lock drive F: for current user key.
MsgBox(0, 'Drive(s) State', "Hidden drive(s): " & _ReadDriveState('hide') & @CRLF & _
"Locked drive(s): " & _ReadDriveState('lock')) ; Show the drive state.
MsgBox(0, 'Change State', 'Now we change the state') ; Message information.
_WriteDriveState('F', 'unhide') ; UnHide drive F: for current user key.
_WriteDriveState('F', 'unlock') ; Unlock drive F: for current user key.
MsgBox(0, 'Drive(s) State', "Hidden drive(s): " & _ReadDriveState('hide') & @CRLF & _
"Locked drive(s): " & _ReadDriveState('lock')) ; Show the drive state.
_WriteDriveState('EF', 'hide', 'HKLM') ; Hide drive E and F for all users key.
_WriteDriveState('all', 'unhide', 'HKLM') ; Unhide all drives for all users key.
#CE
; #FUNCTION# ===========================================================================================
; Name: _WriteDriveState
; Description: Change the state of drives for local user or all users
; Syntax: _WriteDriveState($sDriveLetter, $fState = 'hide', $sHKey = 'HKCU')
; Parameter(s): $sDriveLetter = The drive(s) letter to hide (can be some letters or value 'all')
; $fState = optional: hide: hide drive(s) (default)
; unhide: unhide drive(s)
; lock: Lock drive(s)
; unLock: Unlock drive(s)
; $sHKey = optional: HKCU: For the current user (default)
; HKLM: For the all users
; Requirement(s):
; Return Value(s): on success: 1
; on error: 0 (Extended is set to 0 on writing reg key error or 1 on argument error)
; Author(s): Tlem
; ====================================================================================================
Func _WriteDriveState($sDriveLetter, $fState = 'hide', $sHKey = 'HKCU')
If Not StringIsAlpha($sDriveLetter) Then Return SetError(1, 1, 0)
If Not ($sHKey = 'HKCU' Or $sHKey = 'HKLM') Then Return SetError(1, 1, 0)
If Not ($fState = 'hide' Or $fState = 'unhide' Or $fState = 'lock' Or $fState = 'unlock') Then Return SetError(1, 1, 0)
Local $sStateKey, $iData, $sVar, $iRet, $aDrives, $i, $ii
If $fState = 'hide' Or $fState = 'unhide' Then
$sStateKey = 'NoDrives'
Else
$sStateKey = 'NoViewOnDrive'
EndIf
$sHKey = $sHKey & $sKey
$aDrives = StringSplit(StringUpper($sDriveLetter), '')
$iData = RegRead($sHKey, $sStateKey)
If $iData = '' Then $iData = 0
If StringInStr($sDriveLetter, 'all') Then
$sVar = $DriveVar[0]
Else
For $i = 1 To $aDrives[0]
For $ii = 0 To 26
If $aDrives[$i] = $DriveLetter[$ii] Then $sVar += $DriveVar[$ii]
Next
Next
EndIf
If ($fState = 'hide' Or $fState = 'lock') Then
$iData = BitOR($iData, $sVar)
Else
$iData = BitAND($iData, BitNOT($sVar))
EndIf
$iRet = RegWrite($sHKey, $sStateKey, 'REG_DWORD', $iData)
Return SetError(@error, 0, $iRet)
EndFunc ;==>_WriteDriveState
; #FUNCTION# ===========================================================================================
; Name: _ReadDriveState
; Description: Read the state of drive on explorer for local user or all users
; Syntax: _ReadDriveState([$sHKey = 'HKCU', [$sStateKey = 'NoDrives']])
; Parameter(s): $sHKey = optional: HKCU: For the current user (default)
; HKLM: For the all users
; $sStateKey = optional: 1 : For the hidden drive(s) (default)
; 2 : For the locked drive(s)
; Requirement(s):
; Return Value(s): on success: a string with drive(s) letter(s)
; on error: 0 (argument error)
; Author(s): Tlem
; ====================================================================================================
Func _ReadDriveState($sStateKey = 'hide', $sHKey = 'HKCU')
Local $sDriveList, $sReverse, $iData, $iCount, $i
If $sHKey <> 'HKCU' And $sHKey <> 'HKLM' Then Return SetError(1, 1, 0)
If $sStateKey = 'hide' Then
$sStateKey = 'NoDrives'
ElseIf $sStateKey = 'lock' Then
$sStateKey = 'NoViewOnDrive'
Else
Return SetError(1, 1, 0)
EndIf
$iData = RegRead($sHKey & $sKey, $sStateKey)
If (@error Or $iData = 0) Then Return SetError(0, 0, '')
For $i = 0 To 26
If $iData >= $DriveVar[$i] Then
$iData = $iData - $DriveVar[$i]
$sDriveList &= $DriveLetter[$i]
EndIf
Next
If StringLen($sDriveList) >= 1 Then
For $iCount = 1 To StringLen($sDriveList)
$sReverse = StringMid($sDriveList, $iCount, 1) & $sReverse
Next
$sDriveList = $sReverse
EndIf
Return $sDriveList
EndFunc ;==>_ReadDriveState
► Afficher le textePremière version
_WriteHiddenDrive() permet de masquer des lecteurs de disque.
_ReadHiddenDrive() permet de savoir quel sont les lecteurs masqués.
_ReadHiddenDrive() permet de savoir quel sont les lecteurs masqués.
Code : Tout sélectionner
opt("MustDeclareVars", 1)
Global $sKey = '\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer', $sVal = 'NoDrives'
Global $DriveLetter[27] = ['ZYXWVUTSRQPONMLKJIHGFEDCBA', 'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', _
'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A']
Global $DriveVar[27] = [67108863, 33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, _
65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
#CS
; Start examples :
_WriteHiddenDrive('F') ; Hide drive F: for current user.
MsgBox(0, 'Hidden Drive(s)', _ReadHiddenDrive()) ; Show the Hidden drive.
_WriteHiddenDrive('F', 0) ; UnHide drive F:
MsgBox(0, 'Hidden Drive(s)', _ReadHiddenDrive()) ; Show the Hidden drive.
_WriteHiddenDrive('EF', 1, 'HKLM') ; Hide drive E: and F: for all users.
MsgBox(0, 'Hidden Drive(s)', _ReadHiddenDrive()) ; Show the Hidden drives.
_WriteHiddenDrive('EF', 0, 'HKLM') ; UnHide drive E: and F:
MsgBox(0, 'Hidden Drive(s)', _ReadHiddenDrive()) ; Show the Hidden drives.
; End examples :
#CE
; #FUNCTION# ===========================================================================================
; Name: _WriteHiddenDrive
; Description: Hide drive on explorer for local user or all users
; Syntax: _WriteHiddenDrive($sDriveLetter, [$fHide = True, [$sHKey = 'HKCU']])
; Parameter(s): $sDriveLetter = The drive(s) letter to hide (can be some letters or all letters)
; $fHide = optional: True to hide drive(s) (default)
; False to unhide drive(s)
; $sHKey = optional: HKCU: For the current user (default)
; HKLM: For the all users
; Requirement(s):
; Return Value(s): on success: 1
; on error: 0 (Extended is set to 0 on writing reg key error or 1 on argument error)
; Author(s): Tlem
; ====================================================================================================
Func _WriteHiddenDrive($sDriveLetter, $fHide = True, $sHKey = 'HKCU')
If Not StringIsAlpha($sDriveLetter) Then Return SetError(1, 1, 0)
If $sHKey <> 'HKCU' And $sHKey <> 'HKLM' Then Return SetError(1, 1, 0)
If $fHide <> True And $fHide <> False Then Return SetError(1, 1, 0)
Local $iData, $sVar, $iRet, $aDrives, $i, $ii
$aDrives = StringSplit(StringUpper($sDriveLetter), '')
$iData = RegRead($sKey, $sVal)
If $iData = '' Then $iData = 0
For $i = 1 To $aDrives[0]
For $ii = 0 To 26
If $aDrives[$i] = $DriveLetter[$ii] Then $sVar += $DriveVar[$ii]
Next
Next
If $fHide Then
$iData = BitOR($iData, $sVar)
Else
$iData = BitAND($iData, BitNOT($sVar))
EndIf
$iRet = RegWrite($sHKey & $sKey, $sVal, 'REG_DWORD', $iData)
Return SetError(@error, 0, $iRet)
EndFunc ;==>_WriteHiddenDrive
; #FUNCTION# ===========================================================================================
; Name: _ReadHiddenDrive
; Description: Read the Hidden drive on explorer for local user or all users
; Syntax: _ReadHiddenDrive([$sHKey = 'HKCU']])
; Parameter(s): $sHKey = optional: HKCU: For the current user (default)
; HKLM: For the all users
; Requirement(s):
; Return Value(s): on success: a string with drive(s) letter(s)
; on error: 0 (Extended is set to 0 on reading reg key error or 1 on argument error)
; Author(s): Tlem
; ====================================================================================================
Func _ReadHiddenDrive($sHKey = 'HKCU')
Local $sDriveList, $sReverse, $iData, $iCount, $i
If $sHKey <> 'HKCU' And $sHKey <> 'HKLM' Then Return SetError(1, 1, 0)
$iData = RegRead($sHKey & $sKey, $sVal)
If @error Then Return SetError(1, 0, 0)
If $iData = 0 Then Return SetError(0, 0, '')
For $i = 0 To 26
If $iData >= $DriveVar[$i] Then
$iData = $iData - $DriveVar[$i]
$sDriveList &= $DriveLetter[$i]
EndIf
Next
If StringLen($sDriveList) >= 1 Then
For $iCount = 1 To StringLen($sDriveList)
$sReverse = StringMid($sDriveList, $iCount, 1) & $sReverse
Next
$sDriveList = $sReverse
EndIf
Return $sDriveList
EndFunc ;==>_ReadHiddenDrive