Problems creating Website with IAppHost interfaces in C++ [Answered]RSS

4 replies

Last post Oct 23, 2007 04:05 AM by kustt110

  • Problems creating Website with IAppHost interfaces in C++

    Oct 18, 2007 03:44 AM|kustt110|LINK

    Hello everyone, I have to create a website using IAppHost from C++ code. I modeled it after the JScript sample found on Kanwaljeet Singla's Blog. However, my code always fails with error 0x80070585 Invalid index when adding the binding to the bindings element. The following is my code, I have marked the places where my code fails with the comment "this one just failed with 0x80070585 Invalid index.". It is still rather large and unoptimized, but before improving that I first want to get it running without problems. So can anyone give me a slight hint what's wrong with the way I try to add the binding information? If this is the wrong forum to ask this question, please point me to the right forum. Thanks, Stefan #include "stdafx.h" #include #include #include #include static HRESULT CreateWebSite(LPCWSTR szName, LPCWSTR szPath, LPCWSTR szProtocol, LPCWSTR szBindingInfo, LPCWSTR szAppPool, LPCWSTR szHostHeader, BOOL bStartImmediately); #define WEBSITE_NAME L"test2" int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(NULL); HRESULT hr = CreateWebSite(WEBSITE_NAME, L"C:\\website", L"http", L"*:82:", NULL, NULL, FALSE); if(FAILED(hr)) _tprintf(_T("Web site creation failed with %lu (0x%.8x)\n"), hr, hr); else _tprintf(_T("Success...\n")); CoUninitialize(); return 0; } static HRESULT CreateWebSite(LPCWSTR szName, LPCWSTR szPath, LPCWSTR szProtocol, LPCWSTR szBindingInfo, LPCWSTR szAppPool, LPCWSTR szHostHeader, BOOL bStartImmediately) { IAppHostWritableAdminManager *pMgr = NULL; IAppHostElement *pElem = NULL; IAppHostElementCollection *pCollection = NULL; IAppHostElement *pNewElement = NULL; IAppHostProperty *pProperty = NULL; IAppHostElementCollection *pElementCollection = NULL; IAppHostElementCollection *pApplicationElementCollection = NULL; IAppHostElementCollection *pBindingsCollection = NULL; IAppHostElement *pApplication = NULL; IAppHostElement *pBindings = NULL; IAppHostPropertyCollection *pApplicationPropertiesCollection = NULL; IAppHostPropertyCollection *pVDirPropertiesCollection = NULL; IAppHostPropertyCollection *pBindingElementProperties = NULL; IAppHostChildElementCollection *pChildElems = NULL; BSTR bstrConfigCommitPath = SysAllocString( L"MACHINE/WEBROOT/APPHOST" ); BSTR bstrSectionName = SysAllocString( L"system.applicationHost/sites" ); BSTR bstrElementName = SysAllocString( L"site" ); BSTR bstrNameConst = SysAllocString( L"name" ); BSTR bstrPathConst = SysAllocString( L"path" ); BSTR bstrIdConst = SysAllocString( L"id" ); BSTR bstrId = SysAllocString( L"3" ); BSTR bstrAutoStartConst = SysAllocString( L"serverAutoStart" ); BSTR bstrApplicationConst = SysAllocString( L"application"); BSTR bstrVDirConst = SysAllocString( L"virtualDirectory"); BSTR bstrPhysPathConst = SysAllocString( L"physicalPath" ); BSTR bstrPhysicalPath = NULL; BSTR bstrBindingsConst = SysAllocString( L"Bindings" ); BSTR bstrBindingConst = SysAllocString( L"Binding" ); BSTR bstrProtocolConst = SysAllocString( L"Protocol" ); BSTR bstrBindingInformationConst = SysAllocString( L"BindingInformation" ); BSTR bstrProtocol = NULL; BSTR bstrBindingInformation = NULL; BSTR bstrSlashConst = SysAllocString( L"/"); IAppHostElement *pVDirElement = NULL; IAppHostElement *pBindingElement = NULL; BSTR bstrName = NULL; VARIANT vtPut; VARIANT vtPropertyName; HRESULT hr = S_OK; if(!szName || !szPath || !szProtocol || !szBindingInfo) { hr = E_INVALIDARG; goto CLEANUP; } bstrName = SysAllocString(szName); bstrPhysicalPath = SysAllocString(szPath); bstrProtocol = SysAllocString(szProtocol); bstrBindingInformation = SysAllocString(szBindingInfo); if(!bstrConfigCommitPath || !bstrSectionName || !bstrElementName || !bstrNameConst || !bstrName || !bstrAutoStartConst || !bstrApplicationConst || !bstrId || !bstrPathConst || !bstrVDirConst || !bstrPhysPathConst || !bstrPhysicalPath || !bstrProtocol || !bstrBindingInformation) { hr = E_OUTOFMEMORY; goto CLEANUP; } // // Create a writable admin manager: // hr = CoCreateInstance( __uuidof(AppHostWritableAdminManager), NULL, CLSCTX_INPROC_SERVER, __uuidof(IAppHostWritableAdminManager), (void**) &pMgr); if(FAILED(hr)) goto CLEANUP; _ASSERT(pMgr); hr = pMgr->GetAdminSection(bstrSectionName, bstrConfigCommitPath, &pElem); if (FAILED(hr)) goto CLEANUP; if(!pElem) { hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); goto CLEANUP; } hr = pElem->get_Collection(&pCollection); if (FAILED(hr)) goto CLEANUP; _ASSERT(pCollection); hr = pCollection->CreateNewElement(bstrElementName, &pNewElement); if (FAILED(hr)) goto CLEANUP; _ASSERT(pNewElement); hr = pNewElement->GetPropertyByName(bstrNameConst, &pProperty); if (FAILED(hr)) goto CLEANUP; _ASSERT(pProperty); vtPut.vt = VT_BSTR; vtPut.bstrVal = bstrName; hr = pProperty->put_Value(vtPut); if (FAILED(hr)) goto CLEANUP; pProperty->Release(); pProperty = NULL; hr = pNewElement->GetPropertyByName(bstrIdConst, &pProperty); if (FAILED(hr)) goto CLEANUP; _ASSERT(pProperty); vtPut.vt = VT_BSTR; vtPut.bstrVal = bstrId; hr = pProperty->put_Value(vtPut); if (FAILED(hr)) goto CLEANUP; pProperty->Release(); pProperty = NULL; hr = pNewElement->GetPropertyByName(bstrAutoStartConst, &pProperty); if (FAILED(hr)) goto CLEANUP; _ASSERT(pProperty); vtPut.vt = VT_BOOL; #pragma warning (push) #pragma warning (disable:4310) /// /// VARIANT_TRUE is defined as ((VARIANT_BOOL)-1). There is no way we can fix this /// warning without changing behaviour and risking unwanted side effects. Therefore /// we bite the bullet and use #pragma warning here: /// vtPut.boolVal = bStartImmediately ? VARIANT_TRUE : VARIANT_FALSE; #pragma warning (pop) hr = pProperty->put_Value(vtPut); if (FAILED(hr)) goto CLEANUP; pProperty->Release(); pProperty = NULL; /// /// now the element itself is done, we can continue with its child elements: /// hr = pNewElement->get_Collection(&pElementCollection); if (FAILED(hr)) goto CLEANUP; _ASSERT(pElementCollection); hr = pElementCollection->CreateNewElement(bstrApplicationConst, &pApplication); if (FAILED(hr)) goto CLEANUP; _ASSERT(pApplication); hr = pApplication->get_Properties(&pApplicationPropertiesCollection); if (FAILED(hr)) goto CLEANUP; _ASSERT(pApplicationPropertiesCollection); vtPropertyName.vt = VT_BSTR; vtPropertyName.bstrVal = bstrPathConst; hr = pApplicationPropertiesCollection->get_Item(vtPropertyName, &pProperty); if (FAILED(hr)) goto CLEANUP; _ASSERT(pProperty); vtPut.vt = VT_BSTR; vtPut.bstrVal = bstrSlashConst; hr = pProperty->put_Value(vtPut); if (FAILED(hr)) goto CLEANUP; pProperty->Release(); pProperty = NULL; hr = pApplication->get_Collection(&pApplicationElementCollection); if (FAILED(hr)) goto CLEANUP; _ASSERT(pApplicationElementCollection); hr = pApplicationElementCollection->CreateNewElement(bstrVDirConst, &pVDirElement); if (FAILED(hr)) goto CLEANUP; _ASSERT(pVDirElement); hr = pVDirElement->get_Properties(&pVDirPropertiesCollection); if (FAILED(hr)) goto CLEANUP; _ASSERT(pVDirPropertiesCollection); vtPropertyName.vt = VT_BSTR; vtPropertyName.bstrVal = bstrPathConst; hr = pVDirPropertiesCollection->get_Item(vtPropertyName, &pProperty); if (FAILED(hr)) goto CLEANUP; _ASSERT(pProperty); vtPut.vt = VT_BSTR; vtPut.bstrVal = bstrSlashConst; hr = pProperty->put_Value(vtPut); if (FAILED(hr)) goto CLEANUP; pProperty->Release(); pProperty = NULL; vtPropertyName.vt = VT_BSTR; vtPropertyName.bstrVal = bstrPhysPathConst; hr = pVDirPropertiesCollection->get_Item(vtPropertyName, &pProperty); if (FAILED(hr)) goto CLEANUP; _ASSERT(pProperty); vtPut.vt = VT_BSTR; vtPut.bstrVal = bstrPhysicalPath; hr = pProperty->put_Value(vtPut); if (FAILED(hr)) goto CLEANUP; pProperty->Release(); pProperty = NULL; hr = pApplicationElementCollection->AddElement(pVDirElement); if (FAILED(hr)) goto CLEANUP; hr = pElementCollection->AddElement(pApplication); if (FAILED(hr)) goto CLEANUP; hr = pCollection->AddElement(pNewElement); if (FAILED(hr)) goto CLEANUP; hr = pMgr->CommitChanges(); /// /// now add the binding information: /// hr = pNewElement->get_ChildElements(&pChildElems); if (FAILED(hr)) goto CLEANUP; vtPropertyName.vt = VT_BSTR; vtPropertyName.bstrVal = bstrBindingsConst; hr = pChildElems->get_Item(vtPropertyName, &pBindings); if (FAILED(hr)) goto CLEANUP; _ASSERT(pBindings); hr = pBindings->GetElementByName(bstrBindingConst, &pBindingElement); /// /// this one just failed with 0x80070585 Invalid index. /// hr = pBindings->get_Collection(&pBindingsCollection); if (FAILED(hr)) goto CLEANUP; _ASSERT(pBindingsCollection); ///// ///// vtPropertyName.vt = VT_BSTR; vtPropertyName.bstrVal = bstrBindingConst; hr = pBindingsCollection->CreateNewElement(bstrBindingConst, &pBindingElement); /// /// this one just failed with 0x80070585 Invalid index. /// if (FAILED(hr)) goto CLEANUP; _ASSERT(pBindingElement); hr = pBindingElement->get_Properties(&pBindingElementProperties); if (FAILED(hr)) goto CLEANUP; _ASSERT(pBindingElementProperties); vtPropertyName.vt = VT_BSTR; vtPropertyName.bstrVal = bstrProtocolConst; hr = pBindingElementProperties->get_Item(vtPropertyName, &pProperty); if (FAILED(hr)) goto CLEANUP; _ASSERT(pProperty); vtPut.vt = VT_BSTR; vtPut.bstrVal = bstrProtocol; hr = pProperty->put_Value(vtPut); if (FAILED(hr)) goto CLEANUP; pProperty->Release(); pProperty = NULL; vtPropertyName.vt = VT_BSTR; vtPropertyName.bstrVal = bstrBindingInformationConst; hr = pBindingElementProperties->get_Item(vtPropertyName, &pProperty); if (FAILED(hr)) goto CLEANUP; _ASSERT(pProperty); vtPut.vt = VT_BSTR; vtPut.bstrVal = bstrBindingInformation; hr = pProperty->put_Value(vtPut); if (FAILED(hr)) goto CLEANUP; pProperty->Release(); pProperty = NULL; hr = pBindingsCollection->AddElement(pBindingElement); if (FAILED(hr)) goto CLEANUP; hr = pMgr->CommitChanges(); CLEANUP: if(pBindingElementProperties) { pBindingElementProperties->Release(); pBindingElementProperties = NULL; } if(pBindingElement) { pBindingElement->Release(); pBindingElement = NULL; } if(pBindingsCollection) { pBindingsCollection->Release(); pBindingsCollection = NULL; } if(pBindings) { pBindings->Release(); pBindings = NULL; } if(pChildElems) { pChildElems->Release(); pChildElems = NULL; } if(pVDirPropertiesCollection) { pVDirPropertiesCollection->Release(); pVDirPropertiesCollection = NULL; } if(pApplicationElementCollection) { pApplicationElementCollection->Release(); pApplicationElementCollection = NULL; } if(pApplicationPropertiesCollection) { pApplicationPropertiesCollection->Release(); pApplicationPropertiesCollection = NULL; } if(pApplication) { pApplication->Release(); pApplication = NULL; } if(pElementCollection) { pElementCollection->Release(); pElementCollection = NULL; } if(pProperty) { pProperty->Release(); pProperty = NULL; } if(pNewElement) { pNewElement->Release(); pNewElement = NULL; } if(pCollection) { pCollection->Release(); pCollection = NULL; } if (pElem) { pElem->Release(); pElem = NULL; } if (pMgr) { pMgr->Release(); pMgr = NULL; } SysFreeString(bstrConfigCommitPath); SysFreeString(bstrSectionName); return hr; }
  • Re: Problems creating Website with IAppHost interfaces in C++

    Oct 18, 2007 03:48 AM|kustt110|LINK

    Hello everyone,

    OMG, the post lost all its formatting. I will try again:

     

    Hello everyone,

    I have to create a website using IAppHost from C++ code. I modeled it after the JScript sample found on Kanwaljeet Singla's Blog. However, my code always fails with error 0x80070585 Invalid index when adding the binding to the bindings element. The following is my code, I have marked the places where my code fails with the comment "this one just failed with 0x80070585 Invalid index.". It is still rather large and unoptimized, but before improving that I first want to get it running without problems. So can anyone give me a slight hint what's wrong with the way I try to add the binding information? If this is the wrong forum to ask this question, please point me to the right forum.

    Thanks, Stefan



    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>
    #include <ahadmin.h>
    #include <crtdbg.h>


    static HRESULT CreateWebSite(LPCWSTR szName, LPCWSTR szPath, LPCWSTR szProtocol,
      LPCWSTR szBindingInfo, LPCWSTR szAppPool, LPCWSTR szHostHeader, BOOL bStartImmediately);

    #define WEBSITE_NAME L"test2"

    int _tmain(int argc, _TCHAR* argv[])
    {

      CoInitialize(NULL);

      HRESULT hr = CreateWebSite(WEBSITE_NAME, L"C:\\website", L"http", L"*:82:", NULL, NULL, FALSE);

      if(FAILED(hr))
        _tprintf(_T("Web site creation failed with %lu (0x%.8x)\n"), hr, hr);
      else
        _tprintf(_T("Success...\n"));

      CoUninitialize();

      return 0;
    }

    static HRESULT CreateWebSite(LPCWSTR szName, LPCWSTR szPath, LPCWSTR szProtocol,
                          LPCWSTR szBindingInfo, LPCWSTR szAppPool, LPCWSTR szHostHeader, BOOL bStartImmediately)
    {
      IAppHostWritableAdminManager *pMgr = NULL; 
      IAppHostElement *pElem = NULL;
      IAppHostElementCollection *pCollection = NULL;
      IAppHostElement *pNewElement = NULL;
      IAppHostProperty *pProperty = NULL;
      IAppHostElementCollection *pElementCollection = NULL;
      IAppHostElementCollection *pApplicationElementCollection = NULL;
      IAppHostElementCollection *pBindingsCollection = NULL;
      IAppHostElement *pApplication = NULL;
      IAppHostElement *pBindings = NULL;
      IAppHostPropertyCollection *pApplicationPropertiesCollection = NULL;
      IAppHostPropertyCollection *pVDirPropertiesCollection = NULL;
      IAppHostPropertyCollection *pBindingElementProperties = NULL;
      IAppHostChildElementCollection *pChildElems = NULL;
      BSTR bstrConfigCommitPath = SysAllocString( L"MACHINE/WEBROOT/APPHOST" );
      BSTR bstrSectionName = SysAllocString( L"system.applicationHost/sites" );
      BSTR bstrElementName = SysAllocString( L"site" );
      BSTR bstrNameConst = SysAllocString( L"name" );
      BSTR bstrPathConst = SysAllocString( L"path" );
      BSTR bstrIdConst = SysAllocString( L"id" );
      BSTR bstrId = SysAllocString( L"3" );
      BSTR bstrAutoStartConst = SysAllocString( L"serverAutoStart" );
      BSTR bstrApplicationConst  = SysAllocString( L"application");
      BSTR bstrVDirConst = SysAllocString( L"virtualDirectory");
      BSTR bstrPhysPathConst = SysAllocString( L"physicalPath" );
      BSTR bstrPhysicalPath = NULL;
      BSTR bstrBindingsConst = SysAllocString( L"Bindings" );
      BSTR bstrBindingConst = SysAllocString( L"Binding" );
      BSTR bstrProtocolConst = SysAllocString( L"Protocol" );
      BSTR bstrBindingInformationConst = SysAllocString( L"BindingInformation" );
      BSTR bstrProtocol = NULL;
      BSTR bstrBindingInformation = NULL;

      BSTR bstrSlashConst = SysAllocString( L"/");
      IAppHostElement *pVDirElement = NULL;
      IAppHostElement *pBindingElement = NULL;


      BSTR bstrName = NULL;
      VARIANT vtPut;
      VARIANT vtPropertyName;

      HRESULT hr = S_OK;

      if(!szName || !szPath || !szProtocol || !szBindingInfo)
      {
        hr = E_INVALIDARG;
        goto CLEANUP;
      } 

      bstrName = SysAllocString(szName);
      bstrPhysicalPath = SysAllocString(szPath);
      bstrProtocol =  SysAllocString(szProtocol);

      bstrBindingInformation = SysAllocString(szBindingInfo);


      if(!bstrConfigCommitPath || !bstrSectionName || !bstrElementName || !bstrNameConst ||
        !bstrName || !bstrAutoStartConst || !bstrApplicationConst || !bstrId || !bstrPathConst ||
        !bstrVDirConst || !bstrPhysPathConst || !bstrPhysicalPath || !bstrProtocol || !bstrBindingInformation)
      {
        hr = E_OUTOFMEMORY;
        goto CLEANUP;
      }


      //
      // Create a writable admin manager:
      //
      hr = CoCreateInstance( __uuidof(AppHostWritableAdminManager), NULL,
        CLSCTX_INPROC_SERVER, __uuidof(IAppHostWritableAdminManager), (void**) &pMgr);

      if(FAILED(hr))
        goto CLEANUP;

      _ASSERT(pMgr);

      hr = pMgr->GetAdminSection(bstrSectionName, bstrConfigCommitPath, &pElem);
      if (FAILED(hr))
        goto CLEANUP;

      if(!pElem)
      {
        hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
        goto CLEANUP;
      }

      hr = pElem->get_Collection(&pCollection);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pCollection);

      hr = pCollection->CreateNewElement(bstrElementName, &pNewElement);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pNewElement);


      hr = pNewElement->GetPropertyByName(bstrNameConst, &pProperty);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pProperty);

      vtPut.vt = VT_BSTR;
      vtPut.bstrVal = bstrName;
      hr = pProperty->put_Value(vtPut);
      if (FAILED(hr))
        goto CLEANUP;

      pProperty->Release();
      pProperty = NULL;

      hr = pNewElement->GetPropertyByName(bstrIdConst, &pProperty);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pProperty);

      vtPut.vt = VT_BSTR;
      vtPut.bstrVal = bstrId;
      hr = pProperty->put_Value(vtPut);
      if (FAILED(hr))
        goto CLEANUP;

      pProperty->Release();
      pProperty = NULL;


      hr = pNewElement->GetPropertyByName(bstrAutoStartConst, &pProperty);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pProperty);

      vtPut.vt = VT_BOOL;

    #pragma warning (push)
    #pragma warning (disable:4310)
      ///
      /// VARIANT_TRUE is defined as ((VARIANT_BOOL)-1). There is no way we can fix this
      /// warning without changing behaviour and risking unwanted side effects. Therefore
      /// we bite the bullet and use #pragma warning here:
      ///
      vtPut.boolVal = bStartImmediately ? VARIANT_TRUE : VARIANT_FALSE;
    #pragma warning (pop)
      hr = pProperty->put_Value(vtPut);
      if (FAILED(hr))
        goto CLEANUP;

      pProperty->Release();
      pProperty = NULL;

      ///
      /// now the element itself is done, we can continue with its child elements:
      ///

      hr = pNewElement->get_Collection(&pElementCollection);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pElementCollection);

      hr = pElementCollection->CreateNewElement(bstrApplicationConst, &pApplication);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pApplication);

      hr = pApplication->get_Properties(&pApplicationPropertiesCollection);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pApplicationPropertiesCollection);

      vtPropertyName.vt             = VT_BSTR;
      vtPropertyName.bstrVal        = bstrPathConst;

      hr = pApplicationPropertiesCollection->get_Item(vtPropertyName, &pProperty);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pProperty); 
      vtPut.vt = VT_BSTR;
      vtPut.bstrVal = bstrSlashConst;
      hr = pProperty->put_Value(vtPut);
      if (FAILED(hr))
        goto CLEANUP;

      pProperty->Release();
      pProperty = NULL;


      hr = pApplication->get_Collection(&pApplicationElementCollection);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pApplicationElementCollection);

      hr = pApplicationElementCollection->CreateNewElement(bstrVDirConst, &pVDirElement);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pVDirElement);

      hr = pVDirElement->get_Properties(&pVDirPropertiesCollection);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pVDirPropertiesCollection);

      vtPropertyName.vt      = VT_BSTR;
      vtPropertyName.bstrVal = bstrPathConst;

      hr = pVDirPropertiesCollection->get_Item(vtPropertyName, &pProperty);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pProperty); 
      vtPut.vt = VT_BSTR;
      vtPut.bstrVal = bstrSlashConst;
      hr = pProperty->put_Value(vtPut);
      if (FAILED(hr))
        goto CLEANUP;

      pProperty->Release();
      pProperty = NULL;


      vtPropertyName.vt      = VT_BSTR;
      vtPropertyName.bstrVal = bstrPhysPathConst;

      hr = pVDirPropertiesCollection->get_Item(vtPropertyName, &pProperty);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pProperty); 
      vtPut.vt = VT_BSTR;
      vtPut.bstrVal = bstrPhysicalPath;
      hr = pProperty->put_Value(vtPut);
      if (FAILED(hr))
        goto CLEANUP;

      pProperty->Release();
      pProperty = NULL;

      hr = pApplicationElementCollection->AddElement(pVDirElement);
      if (FAILED(hr))
        goto CLEANUP;

      hr = pElementCollection->AddElement(pApplication);
      if (FAILED(hr))
        goto CLEANUP;

      hr = pCollection->AddElement(pNewElement);
      if (FAILED(hr))
        goto CLEANUP;

      hr = pMgr->CommitChanges();

      ///  
      /// now add the binding information:
      ///  

      hr = pNewElement->get_ChildElements(&pChildElems); 
      if (FAILED(hr))
        goto CLEANUP;

      vtPropertyName.vt      = VT_BSTR;
      vtPropertyName.bstrVal = bstrBindingsConst;

      hr = pChildElems->get_Item(vtPropertyName, &pBindings);

      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pBindings);

      hr = pBindings->GetElementByName(bstrBindingConst, &pBindingElement);
      ///
      /// this one just failed with 0x80070585 Invalid index.    
      ///

      hr = pBindings->get_Collection(&pBindingsCollection);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pBindingsCollection);
      /////
      /////
      vtPropertyName.vt             = VT_BSTR;
      vtPropertyName.bstrVal        = bstrBindingConst;

      hr = pBindingsCollection->CreateNewElement(bstrBindingConst, &pBindingElement);
      ///
      /// this one just failed with 0x80070585 Invalid index.    
      ///

      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pBindingElement);

      hr = pBindingElement->get_Properties(&pBindingElementProperties);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pBindingElementProperties);

      vtPropertyName.vt      = VT_BSTR;
      vtPropertyName.bstrVal = bstrProtocolConst;

      hr = pBindingElementProperties->get_Item(vtPropertyName, &pProperty);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pProperty); 
      vtPut.vt = VT_BSTR;
      vtPut.bstrVal = bstrProtocol;
      hr = pProperty->put_Value(vtPut);
      if (FAILED(hr))
        goto CLEANUP;

      pProperty->Release();
      pProperty = NULL;

      vtPropertyName.vt      = VT_BSTR;
      vtPropertyName.bstrVal = bstrBindingInformationConst;

      hr = pBindingElementProperties->get_Item(vtPropertyName, &pProperty);
      if (FAILED(hr))
        goto CLEANUP;

      _ASSERT(pProperty); 
      vtPut.vt = VT_BSTR;
      vtPut.bstrVal = bstrBindingInformation;
      hr = pProperty->put_Value(vtPut);
      if (FAILED(hr))
        goto CLEANUP;

      pProperty->Release();
      pProperty = NULL;

      hr = pBindingsCollection->AddElement(pBindingElement);
      if (FAILED(hr))
        goto CLEANUP;

      hr = pMgr->CommitChanges();

    CLEANUP: 

      if(pBindingElementProperties)
      {
        pBindingElementProperties->Release();
        pBindingElementProperties = NULL;
      }


      if(pBindingElement)
      {
        pBindingElement->Release();
        pBindingElement = NULL;
      } 

      if(pBindingsCollection)
      {
        pBindingsCollection->Release();
        pBindingsCollection = NULL;
      }


      if(pBindings)
      {
        pBindings->Release();
        pBindings = NULL;
      }
      if(pChildElems)
      {
        pChildElems->Release();
        pChildElems = NULL;
      }

      if(pVDirPropertiesCollection)
      {
        pVDirPropertiesCollection->Release();
        pVDirPropertiesCollection = NULL;
      }

      if(pApplicationElementCollection)
      {
        pApplicationElementCollection->Release();
        pApplicationElementCollection = NULL;
      }

      if(pApplicationPropertiesCollection)
      {
        pApplicationPropertiesCollection->Release();
        pApplicationPropertiesCollection = NULL;
      }

      if(pApplication)
      {
        pApplication->Release();
        pApplication = NULL;
      }

      if(pElementCollection)
      {
        pElementCollection->Release();
        pElementCollection = NULL;
      }

      if(pProperty)
      {
        pProperty->Release();
        pProperty = NULL;
      }

      if(pNewElement)
      {
        pNewElement->Release();
        pNewElement = NULL;
      }

      if(pCollection)
      {
        pCollection->Release();
        pCollection = NULL;
      }

      if (pElem)
      {
        pElem->Release();
        pElem = NULL;
      }

      if (pMgr)
      {
        pMgr->Release();
        pMgr = NULL;
      }

      SysFreeString(bstrConfigCommitPath);
      SysFreeString(bstrSectionName);

      return hr;
    }
     

     

  • Re: Problems creating Website with IAppHost interfaces in C++

    Oct 21, 2007 10:53 PM|CarlosAg|LINK

    It looks like you have a line of code that is not really required:

      hr = pBindings->GetElementByName(bstrBindingConst, &pBindingElement);
     

    pBindings is the <bindings> element which is a collection, so the remaining code should work where you get the collection and add elements to it:

      hr = pBindings->get_Collection(&pBindingsCollection);
      if (FAILED(hr))
        goto CLEANUP;

  • Re: Problems creating Website with IAppHost interfaces in C++

    Oct 21, 2007 10:59 PM|CarlosAg|LINK

    Also, I think we actually have a bug where case sensitivity is not done correctly when CreateNewElement is called. So try modifying your "Binding" to "binding" instead.

  • Re: Problems creating Website with IAppHost interfaces in C++

    Oct 23, 2007 04:05 AM|kustt110|LINK

    Hello CarlosAg,

    that's what I found as well, thanks for taking your time to skim over my code. I found that I can even initialize the variable bstrBindingConst to an empty string like

    BSTR bstrBindingConst = SysAllocString( L"" );

     
    and then everything will work correctly.

     Thanks again,

     

    --

    Stefan