WebDAV, IIS, & ISAPI Filters
by Martin Hallerdal 


Listing One
/* Returns a mime type for a file NOTE: Not threadsafe */
BOOL GetMimeType(char* szFile, char* szBuff) {
//Find extension
    char* szExt = szFile;
    while(*szExt != '\0' && *szExt != '.') {
        szExt++;
    }
    //If we found it, search the MetaBase for it
    if(*szExt != '\0') {
        _bstr_t bstrExt(szExt);
        static CComPtr<IADs> adMimeMap;
        static bool bDidInit = false;
        static bool bGoodObject = false;
        static VARIANT vMimeMap;
        HRESULT hr;
        //Look up the mimemap object that IIS has
        if(!bDidInit) {
            bDidInit = true;
            //Reference MimeMap object through a URL with "IIS" as provider
            hr = ::ADsGetObject(L"IIS://LocalHost/MimeMap", 
                                             IID_IADs, (void**)&adMimeMap);
            if(SUCCEEDED(hr)) {
                //Get the "MimeMap" property
                ::VariantInit(&vMimeMap);
                hr = adMimeMap->GetEx(L"MimeMap", &vMimeMap);
                if(SUCCEEDED(hr)) {
                    //It should be an array
                    if(vMimeMap.vt & VT_ARRAY) {
                        bGoodObject = true;
                    }
                }
            }
        }
            //Only proceed if we have a valid reference
        if(bGoodObject) {
            //Declare some nifty variables
            long lIdx;
            long lHigh;
           _bstr_t bstrBuff;
            BSTR bstrRaw;
            CComQIPtr<IISMimeType> MimeType;                    
            VARIANT vElem;
            ::VariantInit(&vElem);
            SAFEARRAY* pArrMimeMap = vMimeMap.parray;
            //Get the upper and lower bounds of the array
            ::SafeArrayGetLBound(pArrMimeMap, 1, &lIdx);
            ::SafeArrayGetUBound(pArrMimeMap, 1, &lHigh);
            //Loop through the array
            while(lIdx <= lHigh) {
                hr = ::SafeArrayGetElement(pArrMimeMap, &lIdx , &vElem);
                if(SUCCEEDED(hr)) {
                    if(vElem.vt & VT_DISPATCH) {
                        //Each element in array should be of type IISMimeType
                        MimeType = vElem.pdispVal;
                        if(MimeType != NULL) {
                            MimeType->get_Extension(&bstrRaw);
                            bstrBuff = bstrRaw;
                            //Check if the extension matches
                            if(bstrExt == bstrBuff) {
                                MimeType->get_MimeType(&bstrRaw);
                                bstrBuff = bstrRaw;
                                strcpy(szBuff, (char*)bstrBuff);
                                MimeType.Release();
                             return TRUE;
                            }
                            
                        }
                        MimeType.Release();
                    }
                }
                lIdx++;
            }   // Loop through elements
        } //bGoodObject
    }   //szExt != \0
    *szBuff = '\0';
    return FALSE;
}


2


