<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; VBScript: удаление заданного раздела реестра вместе с подразделами]]></title>
	<link rel="self" href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=3905&amp;type=atom" />
	<updated>2009-11-21T16:13:22Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.script-coding.com/viewtopic.php?id=3905</id>
		<entry>
			<title type="html"><![CDATA[VBScript: удаление заданного раздела реестра вместе с подразделами]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=30928#p30928" />
			<content type="html"><![CDATA[<p>Используется WMI провайдер «StdRegProv» и рекурсивный перебор вложенных подразделов с последующим их удалением. Результат выполнения функции «DeleteRegistryKey()» определяется группой констант «con_*» («0» — успешное удаление).<br /></p><div class="codebox"><pre><code>&#039; Объявляется глобально (рекомендуется), либо на уровне RecursiveRegKeyDelete
Set objReg = GetObject(&quot;winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv&quot;)

&#039; Пример вызова
Result = DeleteRegistryKey(&quot;HKLM\SOFTWARE\SOMETHING&quot;)

WScript.Echo &quot;Result:&quot;, Result

Set objReg = Nothing
&#039;---------------------------------------------------------
Function DeleteRegistryKey(parKey)
    &#039;On Error Resume Next
    
    Const HKEY_CLASSES_ROOT   = &amp;H80000000
    Const HKEY_CURRENT_USER   = &amp;H80000001
    Const HKEY_LOCAL_MACHINE  = &amp;H80000002
    Const HKEY_CURRENT_CONFIG = &amp;H80000005
    
    Select Case Left(parKey,4)
        Case &quot;HKLM&quot;: lngRoot = HKEY_LOCAL_MACHINE
        Case &quot;HKCU&quot;: lngRoot = HKEY_CURRENT_USER
        Case &quot;HKCR&quot;: lngRoot = HKEY_CLASSES_ROOT
        Case &quot;HKCC&quot;: lngRoot = HKEY_CURRENT_CONFIG
    End Select 
    
    intResult = RecursiveRegKeyDelete(lngRoot, Mid(parKey, 6))
    
    DeleteRegistryKey = intResult
End Function

&#039;-------------------------------------------------    

Function RecursiveRegKeyDelete(parRoot, parKey)
    &#039;On Error Resume Next
    
    Const con_READ_CONTROL           = 131072
    Const con_KeyDeletedSuccessfully = 0
    Const con_KeyDoesNotExist        = 1
    Const con_AccessDenied           = 2 &#039; либо нет доступа, либо не удалось удалить вложенные ключи
    Const con_SomeOtherError         = 3
    
    
    If objReg.CheckAccess(parRoot,parKey,con_READ_CONTROL) = 0 Then
        If objReg.EnumKey(parRoot, parKey, arrSubKeys) = 0 Then
            If Not IsNull(arrSubKeys) Then
                For i = 0 To UBound(arrSubKeys)
                    RecursiveRegKeyDelete parRoot, parKey &amp; &quot;\&quot; &amp; arrSubKeys(i)
                Next
            End If
        End If
        
        intResult = objReg.DeleteKey(parRoot, parKey)
        
        If intResult = 0 Then 
            RecursiveRegKeyDelete = con_KeyDeletedSuccessfully
        ElseIf intResult = 5 Then
            RecursiveRegKeyDelete = con_AccessDenied
        Else
            RecursiveRegKeyDelete = Con_SomeOtherError
        End If
    Else
        RecursiveRegKeyDelete = con_KeyDoesNotExist
    End If
End Function
&#039;-------------------------------------------------</code></pre></div><p>Автор идеи — <strong>dsb</strong>.</p>]]></content>
			<author>
				<name><![CDATA[alexii]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=1844</uri>
			</author>
			<updated>2009-11-21T16:13:22Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=30928#p30928</id>
		</entry>
</feed>
