Add/Remove ISAPI Filter to/from IIS Automatically Using Delphi

封昊天
2023-12-01

  function SplitString(Source,ch: String): TStringList;
  var
    temp:String;
    i:Integer;
  begin
    Result:=TStringList.Create;
    if Source = '' then Exit;
    temp := Source;
    i := pos(ch,Source);
    while i <> 0 do
    begin
       Result.add(copy(temp,0,i-1));
       Delete(temp,1,i);
       i := pos(ch,temp);
    end;
    Result.add(temp);
  end;
 
  //Add a global IIS isapi filter
  function AddISAPIFilterToIISGlobal(FilterName, FilterPath, FilterDesc:String):Boolean;
  var
    IISNameSpace,
    IISWebService,
    IISFilters,
    IISFilter:Variant;
  begin
    Result := True;
    try
      try
        IISNameSpace := CreateOleObject('IISNamespace');
        IISWebService := IISNameSpace.GetObject('IIsWebService',   'localhost/w3svc');
        IISFilters := IISWebService.GetObject('IISFilters',   'Filters');
        IISFilters.FilterLoadOrder := IISFilters.FilterLoadOrder + ',' + FilterName;
        IISFilters.SetInfo;

        IISFilter := IISFilters.Create('IIsFilter',FilterName);
        IISFilter.FilterPath := FilterPath;
        IISFilter.FilterDescription := FilterDesc;
        IISFilter.SetInfo;
      except
        Result := False;
      end;
    finally
      IISFilter := Unassigned;
      IISFilters := Unassigned;
      IISWebService := Unassigned;
      IISNameSpace := Unassigned;
    end;
  end;

  //Add a isapi IIS filter to default web site
  function AddISAPIFilterToIISDefaultSite(FilterName, FilterPath, FilterDesc:String):Boolean;
  var
    IISNameSpace,
    IISWebService,
    IISWebServer,
    IISFilters,
    IISFilter:Variant;
  begin
    Result := True;
    try
      try
        IISNameSpace := CreateOleObject('IISNamespace');
        IISWebService := IISNameSpace.GetObject('IISWebService',   'localhost/w3svc');
        IISWebServer := IISWebService.GetObject('IISWebServer',   '1');
        IISFilters := IISWebServer.GetObject('IISFilters',   'Filters');
        IISFilters.FilterLoadOrder := IISFilters.FilterLoadOrder + ',' + FilterName;
        IISFilters.SetInfo;

        IISFilter := IISFilters.Create('IISFilter',FilterName);
        IISFilter.FilterPath := FilterPath;
        IISFilter.FilterDescription := FilterDesc;
        IISFilter.SetInfo;
      except
        Result := False;
      end;
    finally
      IISFilter := Unassigned;
      IISFilters := Unassigned;
      IISWebService := Unassigned;
      IISNameSpace := Unassigned;
    end;
  end;

  function RemoveISAPIFilterFromIISGlobal(FilterName:String):Boolean;
  var
    IISNameSpace,
    IISWebService,
    IISFilters,
    LoadOrder:Variant;
    List:TStringList;
    I:Integer;
  begin
    Result := True;
    List := nil;
    LoadOrder := '';
    try
      try
        IISNameSpace := CreateOleObject('IISNamespace');
        IISWebService := IISNameSpace.GetObject('IIsWebService',   'localhost/w3svc');
        IISFilters := IISWebService.GetObject('IISFilters',   'Filters');
        List := SplitString(IISFilters.FilterLoadOrder, ',');
        for I := 0 to List.Count - 1 do
        begin
          if FilterName <> List[I] then
          begin
            if I <> List.Count - 1 then
              LoadOrder := LoadOrder + List[I] + ','
            else
              LoadOrder := LoadOrder + List[I];
          end;
        end;
        if IISFilters.FilterLoadOrder <> LoadOrder then
        begin
          IISFilters.FilterLoadOrder := LoadOrder;
          IISFilters.SetInfo;
        end;

        IISFilters.Delete('IISFilters', FilterName);
      except
        Result := False;
      end;
    finally
      LoadOrder := Unassigned;
      IISFilters := Unassigned;
      IISWebService := Unassigned;
      IISNameSpace := Unassigned;
      if List <> nil then
        List.Free;
    end;
  end;

  function RemoveISAPIFilterFromIISDefaultSite(FilterName:String):Boolean;
  var
    IISNameSpace,
    IISWebService,
    IISWebServer,
    IISFilters,
    LoadOrder:Variant;
    List:TStringList;
    I:Integer;
  begin
    Result := True;
    List := nil;
    LoadOrder := '';
    try
      try
        IISNameSpace := CreateOleObject('IISNamespace');
        IISWebService := IISNameSpace.GetObject('IIsWebService',   'localhost/w3svc');
        IISWebServer := IISWebService.GetObject('IISWebServer',   '1');
        IISFilters := IISWebServer.GetObject('IISFilters',   'Filters');
        List := SplitString(IISFilters.FilterLoadOrder, ',');
        for I := 0 to List.Count - 1 do
        begin
          if FilterName <> List[I] then
          begin
            if I <> List.Count - 1 then
              LoadOrder := LoadOrder + List[I] + ','
            else
              LoadOrder := LoadOrder + List[I];
          end;
        end;
        if IISFilters.FilterLoadOrder <> LoadOrder then
        begin
          IISFilters.FilterLoadOrder := LoadOrder;
          IISFilters.SetInfo;
        end;

        IISFilters.Delete('IISFilters', FilterName);
      except
        Result := False;
      end;
    finally
      LoadOrder := Unassigned;
      IISFilters := Unassigned;
      IISWebServer := Unassigned;
      IISWebService := Unassigned;
      IISNameSpace := Unassigned;
      if List <> nil then
        List.Free;
    end;
  end;
 

 类似资料: