先引入类型库(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.
主窗里写
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;
function ADsGetObject(const PathName: WideString; const GUID:TGUID; out I: IUnknown): HRESULT; stdcall;
var
IISConfigFrm: TIISConfigFrm;
implementation
{$R *.dfm}
function ADsGetObject;external 'ActiveDS.dll' name 'ADsGetObject';
procedure TIISConfigFrm.dlbIISChange(Sender: TObject);
begin
edtPath.Text:=dlbIIS.Directory;
end;
procedure TIISConfigFrm.bbtOKClick(Sender: TObject);
var
I: IADsContainer;
ADs: IADs;
begin
if Length(Trim(edtAlias.Text))=0 then begin
Application.MessageBox('別名不可以為空!','警告');
Exit;
end;
if Length(Trim(edtPath.Text))=0 then begin
Application.MessageBox('請選定虛擬目錄位置!','警告');
Exit;
end;
if ADsGetObject('IIS://localhost', IID_IADsContainer, IUnknown(I)) = S_Ok then begin //IIS已經安裝
if ADsGetObject('IIS://localhost/w3svc', IID_IADsContainer, IUnknown(I)) = S_Ok then begin //Web伺服器存在
ADs := IADs(I.GetObject('IIsWebServer', '1')); //取得服務
if ADs.QueryInterface(IID_IADsContainer, I) = S_OK then begin //服務支持
ADs := IADs(I.GetObject('IIsWebVirtualDir', 'Root')); //在Web伺服器的Root下建立虛擬目錄
if ADs.QueryInterface(IID_IADsContainer, I) = S_OK then begin //服務支持
try
ADs := IADs(I.Create('IIsWebVirtualDir', edtAlias.Text)); //建立虛擬目錄,別名為edtAlias.Text
except
Application.MessageBox('這個別名已經存在,請選擇另外的別名!','警告');
Exit;
end; //try except
ADs.Put('AccessRead', cbRead.Checked); //設定各參數
ADs.Put('AccessWrite', cbWrite.Checked);
ADs.put('AccessScript',cbScript.Checked);
ADs.Put('AccessExecute',cbExecute.Checked);
ADs.put('EnableDirBrowsing',cbBrowse.Checked);
ADs.Put('Path', edtPath.text);
ADs.Put('DefaultDoc','Default.asp, Default.html, Default.htm, ndex.asp, Index.html, Index.htm, Home.asp, Home.Html, Home.htm');
ADs.Put('EnableDefaultDoc',True);//允許打開默認文件
ADs.SetInfo; //保存參數
Application.MessageBox('您的設定已經保存。','恭喜');
end;
end;
end;
end else
Application.MessageBox('您的電腦上沒有安裝IIS或者您無權訪問IIS。','警告');
end;
procedure TIISConfigFrm.FormCreate(Sender: TObject);
begin
edtPath.Text:=dlbIIS.Directory;
end;