0

I am currently creating IIS site using c++, with below code. I would like to set the server variables and Rewrite URL for this site. Can anyone help me to do this? Thanks in advance.

    // IADs and IADsContainer is from C:\Program Files (x86)\Windows Kits\8.1\Include\um\Iads.h

    CComPtr< IADs > spADsWebServer;
    CComPtr< IADsContainer > spADsContainer;
    M_HR( ADsGetObject( 
            L"IIS://localhost/w3svc", 
            IID_IADsContainer, OUT ( void** )&spADsContainer ) );

        CComBSTR bstrIndex( {UNIQUE SITE ID} );
    CComPtr< IDispatch > spIDispatchWebServer;
    M_HR( spADsContainer->Create( IIS_CLASS_WEB_SERVER_W, 
            bstrIndex, OUT &spIDispatchWebServer ) );
    M_HR( spIDispatchWebServer->QueryInterface( 
            IID_IADs, OUT ( void** )&spADsWebServer ) );

    CString szIPAddress;
    CString szHostName;
    vector< CString > vecBindings( 1 );
    vecBindings[ 0 ] = szIPAddress + 
                       L":" + CString( pwszPort ) + 
                       L":" + szHostName;
    CMComVariant mvarBindings; 
    M_HR( VariantHelper::MakeSafeArray( vecBindings, OUT &mvarBindings ) );
    M_HR( spADsWebServer->Put( L"ServerBindings", mvarBindings ) );

    M_HR( pIADsWebServer->Put( L"ServerComment", CMComVariant( pwszComment ) ) );

    // Specify the default document.
    M_HR( pIADsWebServer->Put( L"DefaultDoc", CMComVariant( pwszDefaultDoc ) ) );


 // MY CODE LOGIC TO CREATE VIRTUAL DIRECTORY 
  • IIS 7 and above allow you to access the new configuration system in C++, https://stackoverflow.com/questions/4765647/getting-iis-7-site-properties So you shouldn't use the legacy ADS interfaces. – Lex Li Apr 18 '22 at 14:01

1 Answers1

0

you could try the below c++ code:

add site:

//.h file code:

using namespace Microsoft::Web::Administration;

class Sample final
{

public:
    static void Main();
};
      

int main(int argc, char **argv)
{
    Sample::Main();
}

//.cpp file code:

#include "snippet.h"

using namespace Microsoft::Web::Administration;

void Sample::Main()
{

//ORIGINAL LINE: using(ServerManager serverManager = new ServerManager())
    {
        ServerManager serverManager = ServerManager();
        Configuration *config = serverManager.GetApplicationHostConfiguration();

        ConfigurationSection *sitesSection = config->GetSection(L"system.applicationHost/sites");

        ConfigurationElementCollection *sitesCollection = sitesSection->GetCollection();

        ConfigurationElement *siteElement = sitesCollection->CreateElement(L"site");
        siteElement[L"name"] = LR"(test)";

        ConfigurationElementCollection *bindingsCollection = siteElement->GetCollection(L"bindings");

        ConfigurationElement *bindingElement = bindingsCollection->CreateElement(L"binding");
        bindingElement[L"protocol"] = LR"(89)";
        bindingsCollection->Add(bindingElement);

        ConfigurationElement *applicationDefaultsElement = siteElement->GetChildElement(L"applicationDefaults");
        applicationDefaultsElement[L"path"] = LR"(C:\myapp)";
        applicationDefaultsElement[L"applicationPool"] = LR"(test)";
        sitesCollection->Add(siteElement);

        serverManager.CommitChanges();
    }
}

To add URL rewrite rule:

//.h file code:

using namespace Microsoft::Web::Administration;

class Sample final
{

public:
    static void Main();
};


int main(int argc, char **argv)
{
    Sample::Main();
}

//.cpp file code:

#include "snippet.h"

using namespace Microsoft::Web::Administration;

void Sample::Main()
{


//ORIGINAL LINE: using(ServerManager serverManager = new ServerManager())
    {
        ServerManager serverManager = ServerManager();
        Configuration *config = serverManager.GetWebConfiguration(L"test");

        ConfigurationSection *rulesSection = config->GetSection(L"system.webServer/rewrite/rules");

        ConfigurationElementCollection *rulesCollection = rulesSection->GetCollection();

        ConfigurationElement *ruleElement = rulesCollection->CreateElement(L"rule");
        ruleElement[L"name"] = LR"(redirect rule)";

        ConfigurationElement *conditionsElement = ruleElement->GetChildElement(L"conditions");

        ConfigurationElementCollection *conditionsCollection = conditionsElement->GetCollection();

        ConfigurationElement *addElement = conditionsCollection->CreateElement(L"add");
        addElement[L"input"] = LR"({REQUEST_URI})";
        addElement[L"pattern"] = LR"(^/test/(.*))";
        conditionsCollection->Add(addElement);

        ConfigurationElement *actionElement = ruleElement->GetChildElement(L"action");
        actionElement[L"type"] = LR"(Redirect)";
        actionElement[L"url"] = LR"(https://www.example.com/{C:1})";
        actionElement[L"logRewrittenUrl"] = true;
        rulesCollection->Add(ruleElement);

        serverManager.CommitChanges();
    }
}
Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26