从注册表读取服务状态

1/1/2009来源:注册表人气:6727

function ReadFromRegistry(ServiceName: string; KeyName: string; IsInt: Integer = 0): string;
var { 读取注册表中的服务信息 }
reg: TRegistry;
begin
reg := TRegistry.Create;
try
    reg.RootKey := HKEY_LOCAL_MACHINE;
    reg.OpenKey('SYSTEM\CurrentControlSet\Services\' + ServiceName, False);
    if IsInt <> 0 then
    begin
      case reg.ReadInteger(KeyName) of
        2: Result := '自动';
        3: Result := '手动';
        4: Result := '已禁用';
      end;
    end
    else
      Result := reg.ReadString(KeyName);
finally
    reg.CloseKey;
    reg.Free;
end;
end;

PRocedure WriteIntegerToRegistry(ServiceName: string; KeyName: string; KeyValue: Integer);
var { 写入一个服务整型值到注册表 }
reg: TRegistry;
begin
reg := TRegistry.Create;
try
    reg.RootKey := HKEY_LOCAL_MACHINE;
    reg.OpenKey('SYSTEM\CurrentControlSet\Services\' + ServiceName, False);
    reg.WriteInteger(KeyName, KeyValue);
finally
    reg.CloseKey;
    reg.Free;
end;
end;