BOOL CIISFTP::CreateFTPSite(const char* sAddress,unsigned int iPort,const char* sRootFolder,tagFTPSiteInfo* pOut)
/*pOut是输出参数*/
{
try
{
if(!sAddress || iPort <=0 || !sRootFolder || iPort> 65535)
return FALSE;
if(strlen(sRootFolder) <=0)
{return FALSE;}
if(pOut)
{
strcpy(pOut-> ADsPath, " ");
strcpy(pOut-> IPAddress, " ");
pOut-> Port=0;
}
IADsContainer* iContainer=NULL;//FTP服务
IADsContainer* iContainer_ROOT=NULL;//FTP站点虚拟目录
IADs* iAds=NULL;//FTP站点
IADs* iAds_ROOT=NULL;//FTP根目录
_variant_t vPro;
/* 获得FTPService */
_bstr_t sClassName= "IIS:// ";
sClassName+=sAddress;
sClassName+= "/ ";
sClassName+=ADSIIS_FTPSERVICE_CLASS;
if(ADsGetObject(sClassName,IID_IADsContainer,(void**)&iContainer)!=S_OK)
{
return FALSE;
}
//创建站点对象
_bstr_t sAlias= " ";
char sPort[64];
sprintf(sPort, "FTPServer_%d ",iPort);
sAlias+=sPort;
iContainer-> Delete(ADSIIS_PATHNAME_FTPSERVER, sAlias);//先删除旧得站点
iContainer-> Create(ADSIIS_PATHNAME_FTPSERVER, sAlias,(IDispatch**)&iAds);
iContainer-> Release();
iContainer=NULL;
if(!iAds)
{
return FALSE;
}
//配置站点属性
vPro= "new FTP site,hah! ";
iAds-> Put(L "ServerComment ",vPro);
char sBinding[128];
sprintf(sBinding, "%s:%d: ",sAddress,iPort);
/*SAFEARRAY* psa=NULL;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements =(unsigned long)sBinding;
psa=SafeArrayCreate(VT_VARIANT,1,rgsabound);
if(psa==NULL)
{
iAds-> Release();
return FALSE;
}*/
vPro=sBinding;
if(iAds-> Put(L "ServerBindings ",vPro)!=S_OK)
{
iAds-> Release();
return FALSE;
}
if(iAds-> SetInfo()!=S_OK)
{
iAds-> Release();
return FALSE;
}
//配置根目录
iAds-> QueryInterface(IID_IADsContainer,(void**)&iContainer_ROOT);
if(!iContainer_ROOT)
{
iAds-> Release();
return FALSE;
}
iContainer_ROOT-> Create(L "IIsFtpVirtualDir ",L "ROOT ",(IDispatch**)&iAds_ROOT);
iContainer_ROOT-> Release();
if(!iAds_ROOT)
{
iAds-> Release();
return FALSE;
}
vPro=sRootFolder;
if(iAds_ROOT-> Put(L "Path ",vPro)!=S_OK)
{
iAds-> Release();
iAds_ROOT-> Release();
return FALSE;
}
vPro=true;
if(iAds_ROOT-> Put(L "AccessRead ",vPro)!=S_OK)
{
iAds-> Release();
iAds_ROOT-> Release();
return FALSE;
}
vPro=(bool)false;
if(iAds_ROOT-> Put(L "AccessWrite ",vPro)!=S_OK)
{
iAds-> Release();
iAds_ROOT-> Release();
return FALSE;
}
iAds_ROOT-> SetInfo();
iAds_ROOT-> Release();
//填写输出参数
if(pOut)
{
strcpy(pOut-> IPAddress,sAddress);
pOut-> Port=iPort;
BSTR sPath;
iAds-> get_ADsPath(&sPath);
try
{
char* sBuf=_com_util::ConvertBSTRToString(sPath);
if(sBuf)
{
strcpy(pOut-> ADsPath,sBuf);
delete sBuf;
}
}
catch(_com_error e1)
{ }
}
iAds-> Release();
return TRUE;
}
catch (...)
{
return FALSE;
}
}
其中的常量:
#define ADSIIS_WEBSERVICE_LOCAL L "IIS://localhost/w3svc "
#define ADSIIS_FTPSERVICE_LOCAL L "IIS://localhost/MSFTPSVC "//
#define ADSIIS_WEBSERVICE_CLASS L "w3svc "
#define ADSIIS_FTPSERVICE_CLASS L "MSFTPSVC "
#define ADSIIS_PATHNAME_WEBSERVER L "IIsWebServer "
#define ADSIIS_PATHNAME_FTPSERVER L "IIsFtpServer "//