当前位置:   article > 正文

C/C++编程操作IIS7_c++ iis

c++ iis

由于IIS6与IIS7以后的编程接口更改,导致IIS6的项目无法访问IIS7,研究了几天时间,终于有些眉目,现在记录如下。

首先需要说明如下问题:

1.iis7以后的版本不再支持nntp (http://forums.iis.net/t/1160175.aspx这个页面有明显说明)
2.如果要使用smtp需要系统安装iis6兼容组件
3.如果要使用iis6的接口来访问iis7的信息需要安装iis6兼容组件

网络上很多关于IIS6的编程接口应用,现只说明C/C++编程操作IIS7的方法。详细的操作方式及说明,可以查看微软的在线MSDN(msdn.microsoft.com)

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ahadmin.h>
  4. #include <tchar.h>
  5. HRESULT ListSiteInfo(IAppHostElementCollection *pSitesCollection)
  6. {
  7.     HRESULT hr;
  8.     DWORD iCount;
  9.     DWORD jCount;
  10.     DWORD kCount;
  11.     VARIANT vtCount;
  12.     VARIANT vtPropertyName;
  13.     VARIANT value;
  14.     BSTR bstrPropValue = NULL;
  15.     BSTR bstrIdPropValue = NULL;
  16.     IAppHostElement *site = NULL;
  17.     IAppHostElement *appPath = NULL;
  18.     IAppHostElement *dirPath = NULL;
  19.     IAppHostProperty *prop = NULL;
  20.     IAppHostPropertyCollection  * pElemProps  = NULL;
  21.     IAppHostElementCollection *pAppPathCollection = NULL;
  22.     IAppHostElementCollection *pVirtualPathCollection = NULL;
  23.     BSTR bstrSiteName = SysAllocString(L"name");
  24.     BSTR bstrSiteId = SysAllocString(L"id");
  25.     BSTR bstrSiteRootPath = SysAllocString( L"path" );
  26.     BSTR bstrAppRootPath = SysAllocString( L"application" );
  27.     BSTR bstrVirtualDirPath = SysAllocString(L"virtualDirectory");
  28.     BSTR bstrPhysicalPath = SysAllocString(L"physicalPath");
  29.     hr = pSitesCollection->get_Count(&iCount);
  30.     if (FAILED(hr))
  31.     {
  32.         printf("ERROR: Unable to get site count.\n");
  33.         goto _END;
  34.     }
  35.     //依次列举每个site的信息
  36.     for (DWORD i = 0; i < iCount; i++)
  37.     {
  38.         VariantInit(&vtCount);
  39.         vtCount.lVal = i;
  40.         vtCount.vt = VT_I4;
  41.         hr = pSitesCollection->get_Item(vtCount, &site);
  42.         if (FAILED(hr))
  43.         {
  44.             goto _END;
  45.         }
  46.         //获取site的属性
  47.         hr = site->GetPropertyByName(bstrSiteName, &prop);
  48.         if (FAILED(hr))
  49.         {
  50.             goto _END;
  51.         }
  52.         hr = prop->get_StringValue(&bstrPropValue);
  53.         if (FAILED(hr))
  54.         {
  55.             goto _END;
  56.         }
  57.         prop->Release();
  58.         prop = NULL;
  59.         hr = site->GetPropertyByName(bstrSiteId, &prop);
  60.         if (FAILED(hr))
  61.         {
  62.             goto _END;
  63.         }
  64.         hr = prop->get_Value(&value);
  65.         if (FAILED(hr))
  66.         {
  67.             goto _END;
  68.         }
  69.         _tprintf(_T("Site name : %s, id : %d\n"), bstrPropValue, value.lVal);
  70.         SysFreeString(bstrPropValue);
  71.         bstrPropValue = NULL;
  72.         prop->Release();
  73.         prop = NULL;
  74.         //获取site的下层
  75.         hr = site->get_Collection(&pAppPathCollection);
  76.         if (FAILED(hr))
  77.         {
  78.             goto _END;
  79.         }
  80.         hr = pAppPathCollection->get_Count(&jCount);
  81.         if (FAILED(hr))
  82.         {
  83.             goto _END;
  84.         }
  85.         for (DWORD j = 0; j < jCount; j++)
  86.         {
  87.             VariantInit(&vtCount);
  88.             vtCount.lVal = j;
  89.             vtCount.vt = VT_I4;
  90.             hr = pAppPathCollection->get_Item(vtCount, &appPath);
  91.             if (FAILED(hr))
  92.             {
  93.                 goto _END;
  94.             }
  95.             hr = appPath->get_Collection(&pVirtualPathCollection);
  96.             if (FAILED(hr))
  97.             {
  98.                 goto _END;
  99.             }
  100.             hr = pVirtualPathCollection->get_Count(&kCount);
  101.             if (FAILED(hr))
  102.             {
  103.                 goto _END;
  104.             }
  105.             for (DWORD k = 0; k < kCount; k++)
  106.             {
  107.                 VariantInit(&vtCount);
  108.                 vtCount.lVal = k;
  109.                 vtCount.vt = VT_I4;
  110.                 hr = pVirtualPathCollection->get_Item(vtCount, &dirPath);
  111.                 if (FAILED(hr))
  112.                 {
  113.                     goto _END;
  114.                 }
  115.                 hr = dirPath->GetPropertyByName(bstrSiteRootPath, &prop);
  116.                 if (FAILED(hr))
  117.                 {
  118.                     goto _END;
  119.                 }
  120.                 hr = prop->get_StringValue(&bstrPropValue);
  121.                 if (FAILED(hr))
  122.                 {
  123.                     goto _END;
  124.                 }
  125.                 _tprintf(_T("Site root path:%s\n"), bstrPropValue);
  126.                 hr = dirPath->GetPropertyByName(bstrPhysicalPath, &prop);
  127.                 if (FAILED(hr))
  128.                 {
  129.                     goto _END;
  130.                 }
  131.                 hr = prop->get_StringValue(&bstrPropValue);
  132.                 if (FAILED(hr))
  133.                 {
  134.                     goto _END;
  135.                 }
  136.                 _tprintf(_T("Site phyical path:%s\n"), bstrPropValue);
  137.                 SysFreeString(bstrPropValue);
  138.                 bstrPropValue = NULL;
  139.                 prop->Release();
  140.                 prop = NULL;
  141.                 dirPath->Release();
  142.                 dirPath = NULL;
  143.             }
  144.             pVirtualPathCollection->Release();
  145.             pVirtualPathCollection = NULL;
  146.             appPath->Release();
  147.             appPath = NULL;
  148.         }
  149.         pAppPathCollection->Release();
  150.         pAppPathCollection = NULL;
  151.         site->Release();
  152.         site = NULL;
  153.     }
  154. _END:
  155.     if (NULL != bstrPropValue)
  156.     {
  157.         SysFreeString(bstrPropValue);
  158.     }
  159.     if (NULL != prop)
  160.     {
  161.         prop->Release();
  162.     }
  163.     if (NULL != dirPath)
  164.     {
  165.         dirPath->Release();
  166.     }
  167.     if (NULL != pVirtualPathCollection)
  168.     {
  169.         pVirtualPathCollection->Release();
  170.     }
  171.     if (NULL != appPath)
  172.     {
  173.         appPath->Release();
  174.     }
  175.     if (NULL != pAppPathCollection)
  176.     {
  177.         pAppPathCollection->Release();
  178.     }
  179.     if (NULL != site)
  180.     {
  181.         site->Release();
  182.     }
  183.     SysFreeString(bstrSiteName);
  184.     SysFreeString(bstrSiteId);
  185.     SysFreeString(bstrSiteRootPath);
  186.     SysFreeString(bstrAppRootPath);
  187.     SysFreeString(bstrVirtualDirPath);
  188.     SysFreeString(bstrPhysicalPath);
  189.     return hr;
  190. }
  191. int main(int argc, char **argv)
  192. {
  193.     HRESULT hr                      = S_OK;
  194.     IAppHostWritableAdminManager  *pMgr = NULL;
  195.     IAppHostElement *pElem = NULL;
  196.     IAppHostElementCollection *pSitesCollection = NULL;
  197.     BSTR    bstrMetadataName        = SysAllocString( L"system.applicationHost/sites" );
  198.     BSTR bstrConfigCommitPath = SysAllocString( L"MACHINE/WEBROOT/APPHOST" );
  199.     printf("Press any key to continue...");
  200.     getchar();
  201.     // Initialize
  202.     hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
  203.     if ( FAILED( hr ) )
  204.     {
  205.         printf_s( "ERROR: Unable to initialize\n" );
  206.         goto _OUT;
  207.     } 
  208.     // Create
  209.     hr = CoCreateInstance( __uuidof( AppHostWritableAdminManager  ), NULL
  210.         CLSCTX_INPROC_SERVER,
  211.         __uuidof( IAppHostWritableAdminManager  ), (void**) &pMgr );
  212.     if( FAILED( hr ) )
  213.     {
  214.         printf_s( "ERROR: Unable to create an IAppHostAdminManager instance\n" );
  215.         goto _OUT;
  216.     }
  217.     hr = pMgr->GetAdminSection(bstrMetadataName, bstrConfigCommitPath, &pElem);
  218.     if ( FAILED( hr ) )
  219.     {
  220.         printf("ERROR: Unable to get admin section.\n");
  221.         goto _OUT;
  222.     }
  223.     hr = pElem->get_Collection(&pSitesCollection);
  224.     if ( FAILED( hr ) )
  225.     {
  226.         printf("ERROR: Unable to get sites collection.\n");
  227.         goto _OUT;
  228.     }
  229.     hr = ListSiteInfo(pSitesCollection);
  230. _OUT:
  231.     if (NULL != pSitesCollection)
  232.     {
  233.         pSitesCollection->Release();
  234.     }
  235.     if (NULL != pElem)
  236.     {
  237.         pElem->Release();
  238.     }
  239.     if ( pMgr != NULL )
  240.     {
  241.         pMgr->Release();
  242.     }
  243.     SysFreeString( bstrMetadataName );
  244.     CoUninitialize();
  245.     return 0;
  246. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/252425
推荐阅读
相关标签
  

闽ICP备14008679号