Sign In
Kanwaljeet Singla's Weblog
Translate This Page
Translate this page
Powered by
Microsoft® Translator
Options
Blog Home
About
Email Blog Author
Share this
RSS for posts
Atom
RSS for comments
Search
Advanced search options...
Search In:
Everything
Blogs
Forums
People
Groups
Places
Pages
Date range:
All Time
Last Year
Last 6 Months
Last 3 Months
Last Month
Last Week
Last Two Days
Tags
AdvancedLogging
Appcmd
Configuration
Custom Errors
FastCGI
IIS 7.5
IIS7
MSDeploy
PHP
Tracing
WinCache
Archive
Archives
October 2009
(1)
September 2009
(2)
April 2009
(3)
March 2009
(1)
February 2009
(1)
January 2009
(2)
December 2008
(2)
June 2008
(3)
February 2008
(2)
December 2007
(2)
July 2007
(2)
June 2007
(1)
May 2007
(2)
April 2007
(1)
February 2007
(2)
December 2006
(2)
November 2006
(2)
August 2006
(3)
June 2006
(3)
Implementing IAppHostPathMapper in C
MSDN Blogs
>
Kanwaljeet Singla's Weblog
>
Implementing IAppHostPathMapper in C
Implementing IAppHostPathMapper in C
Kanwaljeet Singla
18 Apr 2009 7:20 PM
Comments
1
Few days ago I was required to implement IAppHostPathMapper interface in native C to map configuration path MACHINE/WEBROOT/APPHOST to DefaultAppPool.config and struggled with finding good documentation. With help of some incomplete, hard to find documentation and some head banging here is what worked for me. Hopefully this will be useful for few others
J
.
#include
<ahadmin.h>
//
// PathMapper object structure with VTable pointer and reference counter
// Add other private data in this structure
//
typedef
struct
TestPathMapper
{
IAppHostPathMapperVtbl * lpVtbl;
ULONG
cRef;
} TestPathMapper;
//
// Method definitions
//
STDMETHODIMP TestPathMapper_QueryInterface(TestPathMapper *, REFIID, LPVOID FAR *);
STDMETHODIMP_(ULONG) TestPathMapper_AddRef(TestPathMapper *);
STDMETHODIMP_(ULONG) TestPathMapper_Release(TestPathMapper *);
STDMETHODIMP TestPathMapper_MapPath(TestPathMapper *, BSTR, BSTR, BSTR *);
//
// IAppHostPathMapper VTable structure
//
static
const
IAppHostPathMapperVtbl vtblTestPathMapper =
{
TestPathMapper_QueryInterface,
TestPathMapper_AddRef,
TestPathMapper_Release,
TestPathMapper_MapPath
};
STDMETHODIMP
TestPathMapper_QueryInterface(
TestPathMapper * pThis,
REFIID
riid,
LPVOID FAR *
lppvObj
)
{
if
( !pThis || pThis->lpVtbl != &vtblTestPathMapper || !lppvObj )
{
return
E_INVALIDARG;
}
if
( !memcmp(riid, &IID_IUnknown,
sizeof
( IID ) ) ||
!memcmp(riid, &IID_IAppHostPathMapper,
sizeof
( IID ) ) )
{
pThis->lpVtbl->AddRef( pThis );
*lppvObj = pThis;
return
S_OK;
}
*lppvObj = NULL;
return
E_NOINTERFACE;
}
STDMETHODIMP_(ULONG)
TestPathMapper_AddRef(
TestPathMapper * pThis
)
{
if
(!pThis || pThis->lpVtbl != &vtblTestPathMapper)
{
return
1;
}
return
InterlockedIncrement( &pThis->cRef );
}
STDMETHODIMP_(ULONG)
TestPathMapper_Release(
TestPathMapper * pThis
)
{
LONG cRef;
if
( !pThis || pThis->lpVtbl != &vtblTestPathMapper )
{
return
1;
}
cRef = InterlockedDecrement( &pThis->cRef );
if
(cRef == 0)
{
pThis->lpVtbl->Release(pThis);
pThis->lpVtbl = NULL;
free( pThis );
}
return
cRef;
}
STDMETHODIMP
TestPathMapper_MapPath(
TestPathMapper * pThis,
BSTR
bstrConfigPath,
BSTR
bstrMappedPhysicalPath,
BSTR *
pbstrNewPhysicalPath
)
{
BSTR bstrNewPath
= NULL;
if
( !pThis || pThis->lpVtbl != &vtblTestPathMapper )
{
return
E_INVALIDARG;
}
if
( wcscmp( bstrConfigPath, L
"MACHINE/WEBROOT/APPHOST"
) == 0 )
{
bstrNewPath = SysAllocString( L
"%systemdrive%\\inetpub\\temp\\apppools\\DefaultAppPool.config"
);
}
else
{
bstrNewPath = SysAllocString( bstrMappedPhysicalPath );
}
if
( bstrNewPath == NULL )
{
return
E_OUTOFMEMORY;
}
*pbstrNewPhysicalPath = bstrNewPath;
return
S_OK;
}
int
_tmain(
int
argc,
_TCHAR* argv[]
)
{
HRESULT
hr
= S_OK;
DWORD
dwCount
= 0;
VARIANT
varUnknown;
TestPathMapper *
pTestPathMapper
= NULL;
IAppHostAdminManager *
pAMgr
= NULL;
IAppHostElement *
pElement
= NULL;
IAppHostElementCollection * pElementCollection = NULL;
BSTR bstrPathMapper
= SysAllocString( L
"pathMapper"
);
BSTR bstrConfigPath
= SysAllocString( L
"MACHINE/WEBROOT/APPHOST"
);
BSTR bstrSitesSectionName = SysAllocString( L
"system.webServer/caching"
);
if
( bstrPathMapper == NULL || bstrConfigPath == NULL || bstrSitesSectionName == NULL )
{
hr = E_OUTOFMEMORY;
goto
Finished;
}
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if
( FAILED(hr) )
{
goto
Finished;
}
hr = CoCreateInstance( &CLSID_AppHostAdminManager,
NULL,
CLSCTX_INPROC_SERVER,
&IID_IAppHostAdminManager,
(
void
**) &pAMgr );
if
( FAILED( hr ) )
{
goto
Finished;
}
//
// Create pathMapper structure
//
pTestPathMapper = (TestPathMapper *)malloc(
sizeof
( TestPathMapper ) );
if
( pTestPathMapper == NULL )
{
hr = E_OUTOFMEMORY;
goto
Finished;
}
pTestPathMapper->lpVtbl = &vtblTestPathMapper;
pTestPathMapper->cRef
= 1;
VariantInit(&varUnknown);
V_VT(&varUnknown)
= VT_UNKNOWN;
V_UNKNOWN(&varUnknown) = pTestPathMapper;
//
// Set pathMapper metadata
//
hr = pAMgr->lpVtbl->SetMetadata( pAMgr, bstrPathMapper, varUnknown );
if
( FAILED( hr ) )
{
goto
Finished;
}
//
// Get the sites section and get number of sites
//
hr = pAMgr->lpVtbl->GetAdminSection( pAMgr, bstrSitesSectionName, bstrConfigPath, &pElement );
if
( FAILED( hr ) || pElement == NULL )
{
goto
Finished;
}
hr = pElement->lpVtbl->get_Collection( pElement, &pElementCollection );
if
( FAILED( hr ) || pElementCollection == NULL )
{
goto
Finished;
}
hr = pElementCollection->lpVtbl->get_Count( pElementCollection, &dwCount );
if
( FAILED( hr ) )
{
goto
Finished;
}
wprintf( L
"%d\n"
, dwCount );
Finished:
VariantClear(&varUnknown);
if
( pElementCollection != NULL )
{
pElementCollection->lpVtbl->Release( pElementCollection );
pElementCollection = NULL;
}
if
( pElement != NULL )
{
pElement->lpVtbl->Release( pElement );
pElement = NULL;
}
if
( pAMgr != NULL )
{
pAMgr->lpVtbl->Release( pAMgr );
pAMgr = NULL;
}
SysFreeString( bstrPathMapper );
SysFreeString( bstrConfigPath );
SysFreeString( bstrSitesSectionName );
CoUninitialize();
return
0;
}
Thanks.
Kanwal
1 Comments
Configuration
Blog - Comment List MSDN TechNet
Comments
Loading...
Leave a Comment
Name
Comment
Please add 2 and 7 and type the answer here:
Post