//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;
}
Top
2 楼masterz(www.fruitfruit.com)回复于 2003-02-24 16:16:53 得分 0 在Visual C++中使用ADSI,需要#include activeds.h,连接activeds.lib和adsiid.lib库文件,并要使用Unicode字符串。
这个例子是使用VC在默认Web站点建立的虚拟目录。
*/
///
// CreateVirtualDirection参数说明
// lpszVirtualDirName需要建立的虚拟目录的目录名字
// lpszDiskPath 需要建立虚拟目录的本地磁盘目录
//
BOOL CreateVirtualDirection(LPCTSTR lpszVirtualDirName,LPCTSTR lpszDiskPath)
{
IADsContainer* iContainer;
IADs* iAds;
if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&iContainer)==S_OK)
{
//等到默认站点
iContainer->GetObject(_bstr_t("IIsWebServer"), _bstr_t("1"),(IDispatch**)&iAds);
if(iAds->QueryInterface(IID_IADsContainer,(void**)&iContainer)==S_OK)
{
//得到默认站点的根目录
iContainer->GetObject(_bstr_t("IIsWebVirtualDir"),_bstr_t("Root"),(IDispatch**)&iAds);
//获得访问虚拟目录
if(iAds->QueryInterface(IID_IADsContainer,(void**)&iContainer)==S_OK)
{
//先删除了虚拟目录
iContainer->Delete(_bstr_t("IIsWebVirtualDir"), _bstr_t(lpszVirtualDirName));
//建立虚拟目录
if(iContainer->Create(_bstr_t("IIsWebVirtualDir"), _bstr_t(lpszVirtualDirName),(IDispatch**)&iAds)==S_OK)
{
//设置虚拟目录的属性
iAds->Put(_bstr_t("AccessRead"),_variant_t("True"));//注意跟VB中的设置属性比较
iAds->Put(_bstr_t("AccessWrite"),_variant_t("True"));
iAds->Put(_bstr_t("Path"),_variant_t(lpszDiskPath));
iAds->SetInfo();
iAds->Release();
iAds->Release();
iContainer->Release();
iContainer->Release();
return TRUE;
}
else
{
iAds->Release();
iAds->Release();
iContainer->Release();
iContainer->Release();
return FALSE;
}
}
else
{
iAds->Release();
iContainer->Release();
}
}
else
{
iAds->Release();
}
iContainer->Release();
}
return FALSE;
}
//使用VC需要初始化COM环境,别忘了APP类的InitInstance中AfxOleInit()函数的调用哦。