C++Builder使用ADSI创建web站点 (转)

C++Builder使用ADSI创建web站点 (转)

C++Builder使用ADSI创建web站点

以下是我学习MSDN中的文章。总结出适合在C++Builder下创建WebServer的例子:
其中使用ADSI的一些接口,注意要将Activeds.Lib添加入工程,
还要包含以下几个头文件
比较简单,希望能抛砖引玉。

file://---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit1.h"

file://---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include "iads.h"
#include "adssts.h"
#include "Adshlp.h"


TForm1 *Form1;

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}


file://几个参数:ip:IP地址字符串,domain:域名(www.youname.com),DiskPath:虚拟目录路径(C:wwwroot)
BOOL CreateWebServer(String ip,String domain,String DiskPath)
{
IADSCOntainer *pCont=NULL;
IADs* pAds=NULL;
IADs* pVrAds=NULL;
IADsServiceOperations *pSrvOp;
IDispatch *pDisp = NULL;
IDispatch *pVrDisp = NULL;
AnsiString WNumer=IntToStr(random(1000)); //取一个随机数建立站点
String newBindings=ip+":80:"+domain;
 
/* 获得WebServer */
if(ADsGetobject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&pCont)==S_OK)
{  //创建站点
  if(pCont->Create(L"IIsWebServer",(wchar_t*)WideString(WNumer),&pDisp)==S_OK)
  {
  pDisp->QueryInterface(IID_IADs, (void**)&pAds);
  pDisp->QueryInterface(IID_IADsServiceOperations, (void**)&pSrvOp);
  pAds->Put(L"ServerSize",Variant(int(1)));
  pAds->Put(L"ServerComment",Variant(String("xiwei")));//服务器注释,没太多用处,xiwei我的名字
  pAds->Put(L"ServerBindings",Variant(String(newBindings)));
  pAds->SetInfo();

  file://创建主目录
 
  pCont->GetObject(L"IIsWebServer",(wchar_t*)WideString(WNumer),&pDisp);//得到刚才创建地网站
  if(pDisp->QueryInterface(IID_IADsContainer,(void**)&pCont)==S_OK)
  {
  if(pCont->Create(L"IIsWebVirtualDir",L"Root",&pVrDisp)==S_OK)
  {
  pVrDisp->QueryInterface(IID_IADs, (void**)&pVrAds);
  pVrAds->Put(L"AccessRead",Variant(BOOL("True")));
  pVrAds->Put(L"AccessWrite",Variant(BOOL("True")));
  pVrAds->Put(L"AccessScript",Variant(BOOL("True")));
  pVrAds->Put(L"EnableDirBrowsing",Variant(BOOL("True")));
  pVrAds->Put(L"Path",Variant(String(DiskPath)));
  pVrAds->Put(L"AppRoot",Variant(String(DiskPath)));
  pVrAds->SetInfo();
  pVrAds->Release();
  pAds->Release();
  pCont->Release();
  }
  file://启动新建的WebServer
  pSrvOp->Start();
  pSrvOp->Release();
  }
  }
}

}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
CreateWebServer(Edit1->Text,"www.cccaaa.com",Edit2->Text);
}
file://---------------------------------------------------------------------------

以上内容缺少错误处理,诸如ip地址已被占用等,我认为技术这东西用不着保守,没等生利息呢,已经贬值了。
哈哈,希望各位给以指正!我的EMail.NET" rel="nofollow">proton@yeah.net


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10797429/viewspace-101625/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10797429/viewspace-101625/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值