先引入類型庫(Project|Import Type Library)adsiis.dll、IISext.dll和activeds.tlb新建一個單元,聲明。
unit ActiveDs;
interface
function ADsGetObject(const PathName: WideString; const GUID:
TGUID; out I: IUnknown): HRESULT; stdcall;
implementation
function ADsGetObject; external 'activeds.dll' name 'ADsGetObject';
end.
方法一(參照C++)
var
I: IADsContainer;
ADs: IADs;
begin
if ADsGetObject('IIS://localhost/w3sVC', IID_IADsContainer, IUnknown(I)) = S_Ok then
begin
ADs := IADs(I.GetObject('IISWebServer', '1'));
ShowMessage(ADs.ADsPath);
if ADs.QueryInterface(IID_IADsContainer, I) = S_OK then
begin
ADs := IADs(I.GetObject('IISWebVirtualDir', 'Root'));
ShowMessage(ADs.ADsPath);
if ADs.QueryInterface(IID_IADsContainer, I) = S_OK then
begin
ADs := IADs(I.Create('IISWebVirtualDir', 'DelphiTest'));
ADs.Put('AccessRead', 'True');
ADs.Put('Path', 'c:Temp');
ADs.SetInfo;
end;
end;
end;
end;
方法二(使用接口)
procedure TForm3.BitBtn4Click(Sender: TObject);
var
Disp: IDispatch;
begin
Disp := IISNamespace1.GetObject('IISWebService', 'localhost/w3svc');
Disp := (Disp as IADsContainer).GetObject('IISWebServer', '1');
Disp := (Disp as IADsContainer).GetObject('IISWebVirtualDir', 'Root');
Disp := (Disp as IADsContainer).Create('IISWebVirtualDir', 'DelphiADSITest');
(Disp as IADs).Put('AccessRead', 'True');
(Disp as IADs).Put('Path', 'c:ADSITest');
(Disp as IADs).SetInfo;
end;
方法三(使用Variant,就是類似VB和ASP的方法)
procedure TForm2.BitBtn1Click(Sender: TObject);
var
WebSite, WebServer, WebRoot, VDir: Variant;
begin
WebSite := CreateOleObject('IISNamespace');
WebSite := WebSite.GetObject('IISWebService', 'localhost/w3svc');
WebServer := WebSite.GetObject('IISWebServer', '1');
WebRoot := WebServer.GetObject('IISWebVirtualDir', 'Root');
VDir := WebRoot.Create('IISWebVirtualDir', 'VariantTest');
VDir.AccessRead := True;
VDir.Path := 'C:Test';
VDir.SetInfo;
end;