我建立新的FTP站点的流程是这样的:
1 找到FTP服务
2 建立一个新的站点
3 配置这个站点的注释,绑定两个属性
4 创建根目录“ROOT”
5 配置根目录的path,读写权限属性
(然后稍后设置这个站点的读写权限都成功了)
所有的执行都执行成功,但是在IIS的服务管理器里面就是显示出不来,用程序连这个ftp也连不上,而手工建立的FTP就很正常
:(郁闷
3 个解决方案
#1
这是我的代码:
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"//
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"//
#2
这里有一个建立web站点的例子,或许对你有点参考作用
http://www.fruitfruit.com/vc/adsi.cpp
/////////////////////////////Create web site
//VC6.0+SP5, SDK200210, windows 2000 server
#include <windows.h>
#include <Iads.h>
#include <comdef.h>
#include <tchar.h>
#include <stdio.h>
#include <Adshlp.h>
#pragma comment(lib,"ActiveDS")
#pragma comment(lib,"adsiid")
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR DiskPath);
void main()
{
CoInitialize(NULL);
if(TRUE==CreateWebServer(_T("192.168.0.224:80"),_T("www.masterz.com"),_T("d:\\tmp")))
printf("create site ok\n");
else
printf("create site failed\n");
CoUninitialize();
}
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR pathname)
{
if(bindaddress==NULL||NULL==domain||NULL==pathname)
return FALSE;
IADsContainer *pCont=NULL;
IADs* pAds=NULL;
IADs* pVrAds=NULL;
IADsServiceOperations *pSrvOp=NULL;
IDispatch *pDisp = NULL;
IDispatch *pVrDisp = NULL;
_bstr_t WNumer="123";
_bstr_t newBindings=_bstr_t(bindaddress)+":"+domain;
HRESULT hr;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebServer",WNumer,&pDisp)==S_OK)
{
hr=pDisp->QueryInterface(IID_IADs, (void**)&pAds);
hr=pDisp->QueryInterface(IID_IADsServiceOperations, (void**)&pSrvOp);
pAds->Put(L"ServerSize",_variant_t(long(1)));
pAds->Put(L"ServerComment",_variant_t(_bstr_t("masterz")));
pAds->Put(L"ServerBindings",_variant_t(newBindings));
pAds->SetInfo();
hr=pCont->GetObject(L"IIsWebServer",(WNumer),&pDisp);
if(pDisp->QueryInterface(IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebVirtualDir",L"Root",&pVrDisp)==S_OK)
{
hr=pVrDisp->QueryInterface(IID_IADs, (void**)&pVrAds);
pVrAds->Put(L"AccessRead",_variant_t(true));
pVrAds->Put(L"AccessWrite",_variant_t(true));
pVrAds->Put(L"AccessScript",_variant_t(true));
pVrAds->Put(L"EnableDirBrowsing",_variant_t(true));
pVrAds->Put(L"Path",_variant_t(pathname));
pVrAds->Put(L"AppRoot",_variant_t(pathname));
pVrAds->SetInfo();
pVrAds->Release();
pAds->Release();
pCont->Release();
}
hr=pSrvOp->Start();
hr=pSrvOp->Release();
}
}
}
return true;
}
http://www.fruitfruit.com/vc/adsi.cpp
/////////////////////////////Create web site
//VC6.0+SP5, SDK200210, windows 2000 server
#include <windows.h>
#include <Iads.h>
#include <comdef.h>
#include <tchar.h>
#include <stdio.h>
#include <Adshlp.h>
#pragma comment(lib,"ActiveDS")
#pragma comment(lib,"adsiid")
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR DiskPath);
void main()
{
CoInitialize(NULL);
if(TRUE==CreateWebServer(_T("192.168.0.224:80"),_T("www.masterz.com"),_T("d:\\tmp")))
printf("create site ok\n");
else
printf("create site failed\n");
CoUninitialize();
}
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR pathname)
{
if(bindaddress==NULL||NULL==domain||NULL==pathname)
return FALSE;
IADsContainer *pCont=NULL;
IADs* pAds=NULL;
IADs* pVrAds=NULL;
IADsServiceOperations *pSrvOp=NULL;
IDispatch *pDisp = NULL;
IDispatch *pVrDisp = NULL;
_bstr_t WNumer="123";
_bstr_t newBindings=_bstr_t(bindaddress)+":"+domain;
HRESULT hr;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebServer",WNumer,&pDisp)==S_OK)
{
hr=pDisp->QueryInterface(IID_IADs, (void**)&pAds);
hr=pDisp->QueryInterface(IID_IADsServiceOperations, (void**)&pSrvOp);
pAds->Put(L"ServerSize",_variant_t(long(1)));
pAds->Put(L"ServerComment",_variant_t(_bstr_t("masterz")));
pAds->Put(L"ServerBindings",_variant_t(newBindings));
pAds->SetInfo();
hr=pCont->GetObject(L"IIsWebServer",(WNumer),&pDisp);
if(pDisp->QueryInterface(IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebVirtualDir",L"Root",&pVrDisp)==S_OK)
{
hr=pVrDisp->QueryInterface(IID_IADs, (void**)&pVrAds);
pVrAds->Put(L"AccessRead",_variant_t(true));
pVrAds->Put(L"AccessWrite",_variant_t(true));
pVrAds->Put(L"AccessScript",_variant_t(true));
pVrAds->Put(L"EnableDirBrowsing",_variant_t(true));
pVrAds->Put(L"Path",_variant_t(pathname));
pVrAds->Put(L"AppRoot",_variant_t(pathname));
pVrAds->SetInfo();
pVrAds->Release();
pAds->Release();
pCont->Release();
}
hr=pSrvOp->Start();
hr=pSrvOp->Release();
}
}
}
return true;
}
#3
。。。搞了半天原来是站点名字只能是数字的字符串,adsi文档好像没这个说明吧,呵呵
#1
这是我的代码:
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"//
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"//
#2
这里有一个建立web站点的例子,或许对你有点参考作用
http://www.fruitfruit.com/vc/adsi.cpp
/////////////////////////////Create web site
//VC6.0+SP5, SDK200210, windows 2000 server
#include <windows.h>
#include <Iads.h>
#include <comdef.h>
#include <tchar.h>
#include <stdio.h>
#include <Adshlp.h>
#pragma comment(lib,"ActiveDS")
#pragma comment(lib,"adsiid")
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR DiskPath);
void main()
{
CoInitialize(NULL);
if(TRUE==CreateWebServer(_T("192.168.0.224:80"),_T("www.masterz.com"),_T("d:\\tmp")))
printf("create site ok\n");
else
printf("create site failed\n");
CoUninitialize();
}
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR pathname)
{
if(bindaddress==NULL||NULL==domain||NULL==pathname)
return FALSE;
IADsContainer *pCont=NULL;
IADs* pAds=NULL;
IADs* pVrAds=NULL;
IADsServiceOperations *pSrvOp=NULL;
IDispatch *pDisp = NULL;
IDispatch *pVrDisp = NULL;
_bstr_t WNumer="123";
_bstr_t newBindings=_bstr_t(bindaddress)+":"+domain;
HRESULT hr;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebServer",WNumer,&pDisp)==S_OK)
{
hr=pDisp->QueryInterface(IID_IADs, (void**)&pAds);
hr=pDisp->QueryInterface(IID_IADsServiceOperations, (void**)&pSrvOp);
pAds->Put(L"ServerSize",_variant_t(long(1)));
pAds->Put(L"ServerComment",_variant_t(_bstr_t("masterz")));
pAds->Put(L"ServerBindings",_variant_t(newBindings));
pAds->SetInfo();
hr=pCont->GetObject(L"IIsWebServer",(WNumer),&pDisp);
if(pDisp->QueryInterface(IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebVirtualDir",L"Root",&pVrDisp)==S_OK)
{
hr=pVrDisp->QueryInterface(IID_IADs, (void**)&pVrAds);
pVrAds->Put(L"AccessRead",_variant_t(true));
pVrAds->Put(L"AccessWrite",_variant_t(true));
pVrAds->Put(L"AccessScript",_variant_t(true));
pVrAds->Put(L"EnableDirBrowsing",_variant_t(true));
pVrAds->Put(L"Path",_variant_t(pathname));
pVrAds->Put(L"AppRoot",_variant_t(pathname));
pVrAds->SetInfo();
pVrAds->Release();
pAds->Release();
pCont->Release();
}
hr=pSrvOp->Start();
hr=pSrvOp->Release();
}
}
}
return true;
}
http://www.fruitfruit.com/vc/adsi.cpp
/////////////////////////////Create web site
//VC6.0+SP5, SDK200210, windows 2000 server
#include <windows.h>
#include <Iads.h>
#include <comdef.h>
#include <tchar.h>
#include <stdio.h>
#include <Adshlp.h>
#pragma comment(lib,"ActiveDS")
#pragma comment(lib,"adsiid")
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR DiskPath);
void main()
{
CoInitialize(NULL);
if(TRUE==CreateWebServer(_T("192.168.0.224:80"),_T("www.masterz.com"),_T("d:\\tmp")))
printf("create site ok\n");
else
printf("create site failed\n");
CoUninitialize();
}
BOOL CreateWebServer(LPCTSTR bindaddress,LPCTSTR domain,LPCTSTR pathname)
{
if(bindaddress==NULL||NULL==domain||NULL==pathname)
return FALSE;
IADsContainer *pCont=NULL;
IADs* pAds=NULL;
IADs* pVrAds=NULL;
IADsServiceOperations *pSrvOp=NULL;
IDispatch *pDisp = NULL;
IDispatch *pVrDisp = NULL;
_bstr_t WNumer="123";
_bstr_t newBindings=_bstr_t(bindaddress)+":"+domain;
HRESULT hr;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebServer",WNumer,&pDisp)==S_OK)
{
hr=pDisp->QueryInterface(IID_IADs, (void**)&pAds);
hr=pDisp->QueryInterface(IID_IADsServiceOperations, (void**)&pSrvOp);
pAds->Put(L"ServerSize",_variant_t(long(1)));
pAds->Put(L"ServerComment",_variant_t(_bstr_t("masterz")));
pAds->Put(L"ServerBindings",_variant_t(newBindings));
pAds->SetInfo();
hr=pCont->GetObject(L"IIsWebServer",(WNumer),&pDisp);
if(pDisp->QueryInterface(IID_IADsContainer,(void**)&pCont)==S_OK)
{
if(pCont->Create(L"IIsWebVirtualDir",L"Root",&pVrDisp)==S_OK)
{
hr=pVrDisp->QueryInterface(IID_IADs, (void**)&pVrAds);
pVrAds->Put(L"AccessRead",_variant_t(true));
pVrAds->Put(L"AccessWrite",_variant_t(true));
pVrAds->Put(L"AccessScript",_variant_t(true));
pVrAds->Put(L"EnableDirBrowsing",_variant_t(true));
pVrAds->Put(L"Path",_variant_t(pathname));
pVrAds->Put(L"AppRoot",_variant_t(pathname));
pVrAds->SetInfo();
pVrAds->Release();
pAds->Release();
pCont->Release();
}
hr=pSrvOp->Start();
hr=pSrvOp->Release();
}
}
}
return true;
}
#3
。。。搞了半天原来是站点名字只能是数字的字符串,adsi文档好像没这个说明吧,呵呵