Поделиться через


DeleteValue method of the StdRegProv class

The DeleteValue method deletes a named value in the specified subkey.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.

Syntax

uint32 DeleteValue(
  [in] uint32 hDefKey = HKEY_LOCAL_MACHINE,
  [in] string sSubKeyName,
  [in] string sValueName
);

Parameters

hDefKey [in]

A registry tree, also known as a hive, that contains the sSubKeyName path. The default value is HKEY_LOCAL_MACHINE.

The following trees are defined in WinReg.h.

HKEY_CLASSES_ROOT (2147483648)

HKEY_CURRENT_USER (2147483649)

HKEY_LOCAL_MACHINE (2147483650)

HKEY_USERS (2147483651)

HKEY_CURRENT_CONFIG (2147483653)

sSubKeyName [in]

A key that contains the named value to be deleted.

sValueName [in]

The named value to be deleted from the subkey. Specify an empty string to delete the default named value. The default named value is not deleted. The value is set to the following: value not set.

Return value

In C++, the method returns a uint32 value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error code that is defined in WinError.h. In C++, use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error. You can also look up return values under the WMI Error Constants.

In scripting or Visual Basic, the method returns an integer value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error code that you can look up in WbemErrorEnum.

Examples

The following VBScript code example shows how to use the DeleteValue method. For purposes of the example, a new key and value are created then the value is deleted.

Const HKEY_CURRENT_USER As Long = &H80000001
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
                     strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\MyKey\MySubKey"
' Create new key and value
Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, strKeyPath)

If (Return = 0) And (Err.Number = 0) Then    
    Wscript.Echo "HKEY_LOCAL_MACHINE\Software\MyKey\MySubKey created"
Else
    Wscript.Echo "CreateKey failed. Error = " & Err.Number
End If
' Create new value
strValueName = "Example DWORD Value"
objReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,250
If Err = 0 Then
   objReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue
   WScript.Echo "HKEY_LOCAL_MACHINE\SOFTWARE\NewKey\Example DWORD " _
                & "Value contains " & dwValue
Else 
   WScript.Echo "Error in creating key and DWORD value = " & Err.Number
' Delete new value
Return = objReg.DeleteValue_
    (HKEY_LOCAL_MACHINE,strKeyPath,strValueName)

If (Return = 0) And (Err.Number = 0) Then
    WScript.Echo strValueName & "Registry value HKEY_LOCAL_MACHINE," _
                 & strKeyPath & "," & strValueName & "," & dwValue & " deleted"
Else
     WScript.Echo "Registry value not deleted" & VBNewLine _
                  & "Error = " & Err.Number
End If

Requirements

Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Namespace
Root\default
MOF
RegEvent.mof
DLL
Stdprov.dll

See also

StdRegProv

Modifying the System Registry

WMI Tasks: Registry