以下是我学习MSDN中的文章。总结出适合在C++Builder下创建WebServer的例子:
其中使用ADSI的一些接口,注意要将Activeds.Lib添加入工程,
还要包含以下几个头文件。
比较简单,希望能抛砖引玉。
file://---------------------------------------------------------------------------
#include <vcl.h>
#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:proton@yeah.net
……