//获取WebServer
CComPtr<IADsContainer> cpWebServer;
HRESULT hr = ADsGetObject(L"IIS://localhost/w3svc",
IID_IADsContainer, (void**)&cpWebServer);
if (FAILED(hr))
{
return FALSE;
}
//获取网站
CComPtr<IEnumVARIANT> cpEnum;
hr = ADsBuildEnumerator(cpWebServer,&cpEnum);
if (FAILED(hr))
{
return FALSE;
}
CComVariant cvTemp;
CComPtr<IADs> cpSite;
CComBSTR cbClass;
ULONG ulCount = 0;
while (SUCCEEDED(ADsEnumerateNext(cpEnum, 1, &cvTemp, &ulCount))
&& cvTemp.vt != VT_EMPTY
&& cvTemp.vt != VT_NULL)
{
hr = V_DISPATCH(&cvTemp)->QueryInterface(&cpSite);
if (FAILED(hr))
{
continue;
}
cpSite->get_Class(&cbClass);
if (cbClass == _T("IIsWebServer"))
{
// 取站点主目录
CComPtr<IADsContainer> cpContWeb;
cpSite->QueryInterface(&cpContWeb);
CComPtr<IADs> cpWebRoot;
cpContWeb->GetObject(CComBSTR("IIsWebVirtualDir"),
CComBSTR("Root"), (IDispatch**)&cpWebRoot);
CComVariant pPath;
cpWebRoot->Get(_T("Path"),&pPath);
//获取一级的虚拟目录
CComPtr<IADsContainer> cpContWebVirtualDir;
cpWebRoot->QueryInterface(IID_IADsContainer,(void**)&cpContWebVirtualDir);
IEnumVARIANT *pDirEnum = NULL;
HRESULT hrDir = ADsBuildEnumerator(cpContWebVirtualDir, &pDirEnum);
if(SUCCEEDED(hrDir))
{
VARIANT varDir;
ULONG ulDirFetched = 0L;
// Get the next contained object.
while(S_OK == (hrDir = ADsEnumerateNext(pDirEnum, 1, &varDir,
&ulDirFetched)) && (ulDirFetched > 0))
{
IADs *pADsDir;
hrDir = V_DISPATCH(&varDir)->QueryInterface(IID_IADs, (void**)&pADsDir);
if(SUCCEEDED(hrDir))
{
CComBSTR sbstrClass;
CComBSTR sbstrName;
CComVariant pPath;
pADsDir->get_Class(&sbstrClass);
if(sbstrClass == _T("IIsWebVirtualDir"))
{
pADsDir->get_Name(&sbstrName);
CComVariant pVirtualDirPath;
pADsDir->Get(_T("Path"),&pVirtualDirPath);
}
}
}
ADsFreeEnumerator(pDirEnum);
}
}
cvTemp.Clear();
cpSite = NULL;
}
ADsFreeEnumerator(cpEnum.Detach());