可否编程实现在IIS建立WEB虚拟目录和FTP虚拟目录?

能否提供例程?多谢了!
---------------------------------------------------------------

IIS中如何利用ADSI编程实现添加虚拟目录

IIS中如何编程实现添加虚拟目录
大富翁上aiminggo总结的.我实在没法添加什么东东(因为太完美了),
最后我找了asp的实现,来了个狗尾续貂.哈

ADSI 即 active directory service interface ,
微软的东东, 用来控制NT, IIS, EXCHANGER SERVER.

首先确认,API是绝对解决不了这些问题的,必须使用COM,使用ADSI接口。
在WinNT\System32下的adsiis.tlb是MS封装的ADSI公开接口,非常的全,也非常之庞大。你可
以使用它,也可以去这儿下载一个公开源码的ADSI COM控件。但是,它是C写的。
ftp://ftp.15seconds.com/990107.zip
打开990107.zip,用regsvr32.exe myadsi.dll注册这个控件。然后,你就可以开始用Delphi
干活儿了。

一、生成接口文件
------------------------------------------------------------------------
由于myADSI.dll不是OCX/EXE方式的ActiveX服务,所以,必须手工生成TLB接口文件。
运行\Delphi\BIN目录的TLIBImp.exe文件。如下:
tlibimp -L+ MyADSI.dll
// L+参数是生成能够在Delphi的IDE环境中使用的可视组件。可选。
// 如果你使用adsiis.tlb,也需要用tlibimp来生成接口文件。
这个控件的编写者有病,会将COM控件命名为Contorl,生成的Delphi类名叫TControl,与
Delphi自己的一个控件会冲突,所以你需要打开生成的myADSILib_TLB.pas文件,将所有的
TControl替换成TIISControl。就成了。——你也可以不替换,但出了问题可被怪我。

二、安装组件
------------------------------------------------------------------------
安装myADSILib_TLB.pas到组件板,与普通操作无二。不讲了。

三、编程
------------------------------------------------------------------------
太简单了。 ^-^。下面假设控件名:IISConfig
var selectDir : integer; //示例中用来控制创建的虚拟目录类型。
procedure TMainForm.Button1Click(Sender: TObject);
const //Permissions Const, From MSDN.
IISReadAccess = 1;
IISWriteAccess = 2;
IISExecuteAccess = 4; //(including ScriptAccess)
IISScriptAccess = 512;
var
VDirName : string;
begin
VDirName := Edit1.Text;
if (VDirName='') or (VDirName[1]='/') then
begin
showMessage('虚拟目录不能为空, 且第一个字符不能为''/''.'#$0D'请重新填写.');
exit;
end;

IISConfig.Site := 1; //如果IIS中有多个Web Site,这里可选。
IISConfig.Connect;
try
if BOOL(IISConfig.ExistsVDir(VDirName))
then showMessage('对不起, 该虚拟目录已经存在.'#$0D'不能创建虚拟目录.')
else
case selectDir of
1 : //普通目录
begin
IISConfig.Permissions := IISReadAccess;
if not BOOL(IISConfig.CreateVDir(WideString(PBF.Folder), WideString(VDirName))) then
showMessage('对不起, 未知情况导致虚拟目录不能成功创建.');
end;
2 : //脚本目录
begin
IISConfig.Permissions := IISExecuteAccess;
if not BOOL(IISConfig.CreateVDir(WideString(PBF.Folder), WideString(VDirName))) then
showMessage('对不起, 未知情况导致虚拟目录不能成功创建.');
end;
end;
finally
IISConfig.Disconnect;
end;
end;

就这样啦。不难的。
TIISControl主要有三个功能:CreateVDir(), ExistsVDir(), DeleteVDir()。
OnStartPage()和OnEndPage()两个功能我也没有太搞明白,好象是设置ASP的起始和结束页的。
Permissions设置的全部定义是:
{ //Define In MSDN
MD_ACCESS_READ 0x00000001 Allow read access.
MD_ACCESS_WRITE 0x00000002 Allow write access.
MD_ACCESS_EXECUTE 0x00000004 Allow file execution (includes script permission).
MD_ACCESS_SOURCE 0x00000010 Allow source access.
MD_ACCESS_SCRIPT 0x00000200 Allow script execution.
MD_ACCESS_NO_REMOTE_WRITE 0x00000400 Local write access only.
MD_ACCESS_NO_REMOTE_READ 0x00001000 Local read access only.
MD_ACCESS_NO_REMOTE_EXECUTE 0x00002000 Local execution only.
MD_ACCESS_NO_REMOTE_SCRIPT 0x00004000 Local host access only. }
但注意MyASDI中的Permissions是smallInt类型的。小有区别啦。 ^-^

四、其它
------------------------------------------------------------------------
如果你要发布软件的话,当然不能要用户自已去运行regsvr32.exe来注册MyADSI.dll了。
如果你不是使用专门的安装工具来做这件事的话,你可以用一段小程序来完成这件事。
type
TRegisterMode = (regRegister, regUnregister);
function OLERegisterDLLFile (strFileName : STRING; mode : TRegisterMode) : BOOLEAN;
type
TOleRegister = function : HResult;
var
hLib : THandle;
fnAdr: TFarProc;
begin
Result := FALSE;
hLib := LoadLibrary(PCHAR(strFileName));
if (hLib > 0) then
begin
try
if (mode = regRegister) then
fnAdr := GetProcAddress(hLib, pchar('DllRegisterServer'))
else
fnAdr := GetProcAddress(hLib, pchar('DllUnregisterServer'));
if (fnAdr < > nil) then
Result := (TOleRegister(fnAdr) > = 0);
finally
FreeLibrary(hLib);
end;
end;
end; { RegisterDLLFile }

OLERegisterDLLFile()函数可以加到TForm.onCreate和TForm.onClose事件中。即可以完成
自动注册和卸载。
好了。用Delphi简单吧?^-^

五、关于ASP
------------------------------------------------------------------------
需要的话,去查MSDN,关键字:“Virtual directories, creating”。也可以去MS的MSDN网
站,查“Create a Virtual Directory Automatically with ADSI”,就成了。

不过只使用于NT4.0(安装ADSI)和Win2k的机器:(以下是续的狗尾...)
< %
'创建物理目录
Set FS = Server.CreateObject( "Scripting.FileSystemObject" )
FS.CreateFolder "c:\testdir"

'创建虚拟目录
myServer = Request.ServerVariables( "SERVER_NAME" )
ADSIPath = "IIS://" & myServer & "/W3SVC/1/ROOT"
Response.Write "ADSIPath = " & ADSIPath
Set defaultSite = getObject( ADSIPath )
Set vDir = defaultSite.Create( "IISWebVirtualDir", "StellcomScripts" )
vDir.Path = "c:\testdir"

'设置虚拟目录属性
vDir.AccessRead = TRUE
vDir.AccessScript = TRUE
vDir.DefaultDoc = "index.asp"
vDir.SetInfo
Response.Write "Virtual Directory Created!"
%>

Published At
Categories with Web编程
Tagged with
comments powered by Disqus