以下是我學(xué)習(xí)MSDN中的文章。總結(jié)出適合在C++Builder下創(chuàng)建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://幾個參數(shù):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)); //取一個隨機數(shù)建立站點 String newBindings=ip+":80:"+domain; /* 獲得WebServer */ if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&pCont)==S_OK) { //創(chuàng)建站點 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")));//服務(wù)器注釋,沒太多用處,xiwei我的名字 pAds->Put(L"ServerBindings",Variant(String(newBindings))); pAds->SetInfo();
file://創(chuàng)建主目錄 pCont->GetObject(L"IIsWebServer",(wchar_t*)WideString(WNumer),&pDisp);//得到剛才創(chuàng)建地網(wǎng)站 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://---------------------------------------------------------------------------
以上內(nèi)容缺少錯誤處理,諸如ip地址已被占用等,我認(rèn)為技術(shù)這東西用不著保守,沒等生利息呢,已經(jīng)貶值了。 哈哈,希望各位給以指正!我的Email:proton@yeah.net
|