How do I read registry key values using WMI?
Use the StdRegProv class, located in root\default (for Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0) and root\cimv2 namespace for newers versions of Windows . You cannot get any instances of this class because the System Registry Provider is a method and event provider only. However, you can get registry data through methods such as EnumKey or EnumValue. The Win32_Registry, located in root\cimv2 namespace, gets data about the registry as a whole, such as how large it is.
var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet : OLEVariant; FInParams : OLEVariant; FOutParams : OLEVariant; begin FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); //http://msdn.microsoft.com/en-us/library/aa393664%28v=vs.85%29.aspx //StdRegProv is preinstalled in the WMI namespaces root\default and root\cimv2. //Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: StdRegProv is available only in root\default namespace. FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\default', '', ''); //For Windows Vista or Windows 7 you must use the root\CIMV2 namespace //FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); FWbemObjectSet:= FWMIService.Get('StdRegProv'); FInParams := FWbemObjectSet.Methods_.Item('GetDWORDValue').InParameters.SpawnInstance_(); FInParams.hDefKey:=HKEY_LOCAL_MACHINE; FInParams.sSubKeyName:='SOFTWARE\Microsoft\Windows\CurrentVersion\BITS'; FInParams.sValueName:='LogFileMinMemory'; FOutParams := FWMIService.ExecMethod('StdRegProv', 'GetDWORDValue', FInParams); Writeln(Format('sValue %d',[Integer(FOutParams.uValue)])); Writeln(Format('ReturnValue %d',[Integer(FOutParams.ReturnValue)])); end;
How do I create a new registry key?
Use the StdRegProv class, located in root\default namespace, and the CreateKey method.
var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet : OLEVariant; FInParams : OLEVariant; FOutParams : OLEVariant; begin FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); //http://msdn.microsoft.com/en-us/library/aa393664%28v=vs.85%29.aspx //StdRegProv is preinstalled in the WMI namespaces root\default and root\cimv2. //Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: StdRegProv is available only in root\default namespace. FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\default', '', ''); //For Windows Vista or Windows 7 you must use the root\CIMV2 namespace //FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); FWbemObjectSet:= FWMIService.Get('StdRegProv'); FInParams := FWbemObjectSet.Methods_.Item('CreateKey').InParameters.SpawnInstance_(); FInParams.hDefKey:=HKEY_LOCAL_MACHINE; FInParams.sSubKeyName:='SOFTWARE\NewKey'; FOutParams := FWMIService.ExecMethod('StdRegProv', 'CreateKey', FInParams); Writeln(Format('ReturnValue %d',[Integer(FOutParams.ReturnValue)])); end;
How do I create a new registry value under a key?
Use the StdRegProv class, located in the root\default namespace, and the CreateKey method. Then use one of the Set methods, depending on what registry datatype the value is, such as the SetDWORDValue. The Set methods create a value if it does not already exist. For more information, see Mapping a Registry Data Type to a WMI Data Type.
var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet : OLEVariant; FInParams : OLEVariant; FOutParams : OLEVariant; begin FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); //http://msdn.microsoft.com/en-us/library/aa393664%28v=vs.85%29.aspx //StdRegProv is preinstalled in the WMI namespaces root\default and root\cimv2. //Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: StdRegProv is available only in root\default namespace. FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\default', '', ''); //For Windows Vista or Windows 7 you must use the root\CIMV2 namespace //FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); FWbemObjectSet:= FWMIService.Get('StdRegProv'); FInParams := FWbemObjectSet.Methods_.Item('SetExpandedStringValue').InParameters.SpawnInstance_(); FInParams.hDefKey:=HKEY_LOCAL_MACHINE; FInParams.sSubKeyName:='SOFTWARE\NewKey'; FInParams.sValueName :='Example_Expanded_String_Value'; FInParams.sValue :='%PATHEXT%'; FOutParams := FWMIService.ExecMethod('StdRegProv', 'SetExpandedStringValue', FInParams); Writeln(Format('ReturnValue %d',[Integer(FOutParams.ReturnValue)])); end;
How do I check security on a specific registry key?
Use the StdRegProv class, located in root\default namespace and the CheckAccess method. You can only check the access rights for the current user that is running the script or application. You cannot check the access rights for another specified user.
var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet : OLEVariant; FInParams : OLEVariant; FOutParams : OLEVariant; begin FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); //http://msdn.microsoft.com/en-us/library/aa393664%28v=vs.85%29.aspx //StdRegProv is preinstalled in the WMI namespaces root\default and root\cimv2. //Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: StdRegProv is available only in root\default namespace. FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\default', '', ''); //For Windows Vista or Windows 7 you must use the root\CIMV2 namespace //FWMIService := FSWbemLocator.ConnectServer(Server, 'root\CIMV2', User, Pass); FWbemObjectSet:= FWMIService.Get('StdRegProv'); FInParams := FWbemObjectSet.Methods_.Item('CheckAccess').InParameters.SpawnInstance_(); FInParams.hDefKey:=HKEY_LOCAL_MACHINE; FInParams.sSubKeyName:='SYSTEM\CurrentControlSet'; FInParams.uRequired:=KEY_QUERY_VALUE; FOutParams := FWMIService.ExecMethod('StdRegProv', 'CheckAccess', FInParams); Writeln(Format('Granted %s',[booltoStr(Boolean(FOutParams.bGranted),True)])); Writeln(Format('ReturnValue %d',[Integer(FOutParams.ReturnValue)])); end;
How do I read and write binary registry values?
Use the StdRegProv class, located in root\default namespace and the GetBinaryValue and SetBinaryValue methods. Registry values that appear in the regedt32 utility as a series of byte hexadecimal values are in the REG_BINARY data format. For more information, see Mapping a Registry Data Type to a WMI Data Type. The following code example creates a new key with a binary value. The binary value is supplied in the iValues byte array specified in Hex.
var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet : OLEVariant; FInParams : OLEVariant; FOutParams : OLEVariant; i : Integer; function ArrayToVarArray(Arr : Array Of Word):OleVariant; var i : integer; begin Result :=VarArrayCreate([0, High(Arr)], varVariant); for i:=Low(Arr) to High(Arr) do Result[i]:=Arr[i]; end; begin FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); //http://msdn.microsoft.com/en-us/library/aa393664%28v=vs.85%29.aspx //StdRegProv is preinstalled in the WMI namespaces root\default and root\cimv2. //Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: StdRegProv is available only in root\default namespace. FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\default', '', ''); //For Windows Vista or Windows 7 you must use the root\CIMV2 namespace //FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); FWbemObjectSet:= FWMIService.Get('StdRegProv'); //Write the binary value FInParams := FWbemObjectSet.Methods_.Item('SetBinaryValue').InParameters.SpawnInstance_(); FInParams.hDefKey:=HKEY_LOCAL_MACHINE; FInParams.sSubKeyName:='SOFTWARE\NewKey'; FInParams.sValueName :='Example Binary Value'; FInParams.uValue :=ArrayToVarArray([1,2,3,4,5,6,7,8]); FOutParams := FWMIService.ExecMethod('StdRegProv', 'SetBinaryValue', FInParams); Writeln(Format('ReturnValue %d',[Integer(FOutParams.ReturnValue)])); //Read the binary value FInParams := FWbemObjectSet.Methods_.Item('GetBinaryValue').InParameters.SpawnInstance_(); FInParams.hDefKey:=HKEY_LOCAL_MACHINE; FInParams.sSubKeyName:='SOFTWARE\NewKey'; FInParams.sValueName :='Example Binary Value'; FOutParams := FWMIService.ExecMethod('StdRegProv', 'GetBinaryValue', FInParams); for i := VarArrayLowBound(FOutParams.uValue, 1) to VarArrayHighBound(FOutParams.uValue, 1) do Write(Format('%d,',[Integer(FOutParams.uValue[i])])); Writeln; Writeln(Format('ReturnValue %d',[Integer(FOutParams.ReturnValue)])); end;
How do I read and write registry values that contain multiple strings?
Use the StdRegProv class, located in root\default namespace and the GetMultiStringValue and SetMultiStringValue methods. Registry keys that appear in the regedt32 utility as a series of strings separated by spaces are in the REG_MULTI_SZ data format. For more information, see Mapping a Registry Data Type to a WMI Data Type. The following code example creates a new key and a new multistring value.
var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet : OLEVariant; FInParams : OLEVariant; FOutParams : OLEVariant; i : Integer; function ArrayToVarArray(Arr : Array Of String):OleVariant; var i : integer; begin Result :=VarArrayCreate([0, High(Arr)], varVariant); for i:=Low(Arr) to High(Arr) do Result[i]:=Arr[i]; end; begin FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); //http://msdn.microsoft.com/en-us/library/aa393664%28v=vs.85%29.aspx //StdRegProv is preinstalled in the WMI namespaces root\default and root\cimv2. //Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: StdRegProv is available only in root\default namespace. FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\default', '', ''); //For Windows Vista or Windows 7 you must use the root\CIMV2 namespace //FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); FWbemObjectSet:= FWMIService.Get('StdRegProv'); //Write the Multi String Value FInParams := FWbemObjectSet.Methods_.Item('SetMultiStringValue').InParameters.SpawnInstance_(); FInParams.hDefKey:=HKEY_LOCAL_MACHINE; FInParams.sSubKeyName:='SOFTWARE\NewKey'; FInParams.sValueName :='Example Multistring Value'; FInParams.sValue :=ArrayToVarArray(['String 1','String 2','String 3','String 4','String 5','String 6','String 7','String 8']); FOutParams := FWMIService.ExecMethod('StdRegProv', 'SetMultiStringValue', FInParams); Writeln(Format('ReturnValue %d',[Integer(FOutParams.ReturnValue)])); //Read the Multi String Value FInParams := FWbemObjectSet.Methods_.Item('GetMultiStringValue').InParameters.SpawnInstance_(); FInParams.hDefKey:=HKEY_LOCAL_MACHINE; FInParams.sSubKeyName:='SOFTWARE\NewKey'; FInParams.sValueName :='Example Multistring Value'; FOutParams := FWMIService.ExecMethod('StdRegProv', 'GetMultiStringValue', FInParams); for i := VarArrayLowBound(FOutParams.sValue, 1) to VarArrayHighBound(FOutParams.sValue, 1) do Writeln(Format('%s',[String(FOutParams.sValue[i])])); Writeln; Writeln(Format('ReturnValue %d',[Integer(FOutParams.ReturnValue)])); end;
This article is based in the MSDN Entry WMI Tasks: Registry