Jump to content

My latest IIS: COM Confusion...


 Share

Recommended Posts

OK, I'm trying to add a couple MIME Mappings to the IIS 6.0 Metabase.xml. I can see the mappings using an ADSI COM object:

(Don't laugh, this COM stuff comes hard to me! :whistle: )

; Test access to IIsMimeMap

; Declare COM Object error handler:
Global $oComError = ObjEvent("AutoIt.Error", "_ComErrFunc")

$oMimeMap = ObjGet("IIS://" & @ComputerName & "/MimeMap")
$colMimeMap = $oMimeMap.GetEx ("MimeMap")
$Msg = "Mime Types at the IISComputer level:" & @CRLF
For $oMapping In $colMimeMap
    $Msg &= $oMapping.MimeType & " = " & $oMapping.Extension & ", "
Next
MsgBox(32, "Test", $Msg)

;--------------------------------------
; Function _ComErrFunc()
;   Custom COM object error handler
;--------------------------------------
Func _ComErrFunc()
    Local $HexNumber = Hex($oComError.number, 8)
    MsgBox(16, "Test", "AutoIT COM Error Occured!" & @CRLF & _
            @TAB & "Error Number: " & $HexNumber & @CRLF & _
            @TAB & "Line Number: " & $oComError.scriptline & @CRLF & _
            @TAB & "Description: " & $oComError.description & @CRLF & _
            @TAB & "WinDescription: " & $oComError.windescription)
    SetError(1, -1, ""); something to check for when this function returns
EndFunc   ;==>_ComErrFuncoÝ÷ ØÚ0#§¶ÚuÖ¢êezf©¦)à±ú+{¦¦W¡×±Ë;^Æßñ_Ü¡× n¶Ø^~e£§)æÊ׬¶Ç+ZºÚ"µÍÈXÛHQÈÜHÛÛÝ[ÈÜÙHÚ]]^ÛÛÝ    ÌÍÐQ×ÔÔTWÐÓPTHK ÌÍÐQ×ÔÔTWÕTUHH   ÌÍÐQ×ÔÔTWÐTSHË  ÌÍÐQ×ÔÔTWÑSUHH
ÈYH]ÈX[ÂÌÍÛÓZ[YSXHØÙ]
    ][ÝÒRTÎËÉ][ÝÈ    [ÈÛÛ][YH [È ][ÝËÓZ[YSX   ][ÝÊBÌÍÜÓ]ÓXH    ][ÝË^][ÝÂÌÍÜÓ]ÕHH  ][ÝÝ^Þ[  ][ÝÂÌÍÛÓZ[YSX]^
    ÌÍÐQ×ÔÔTWÐTS ÌÍÜÓ]ÓX    ÌÍÜÓ]ÕJBÌÍÛÓZ[YSXÙ][oÝ÷ Ø­¶­ë-'$± 1ê뢼"¶¡×Ùèuë®*mÈj[(¶¸vØ^~e£§«­¢+ØìÑ¡¹ÜµÁÁ¥¹(ÀÌØí½5¥µ5Àô=©Ð ÅÕ½Ðí%%Lè¼¼ÅÕ½ÐìµÀì
½µÁÕÑÉ9µµÀìÅÕ½Ðì½5¥µ5ÀÅÕ½Ðì¤(ÀÌØíÍ9Ý5ÀôÅÕ½Ðì¹áåèÅÕ½Ðì(ÀÌØíÍ9ÝQåÁôÅÕ½ÐíÑáнᵰÅÕ½Ðì(ÀÌØí½5¥µ5À¹ ÀÌØíÍ9Ý5À°ÀÌØíÍ9ÝQåÁ¤(ÀÌØí½5¥µ5À¹MÑ%¹¼

But that kicks an "Invalid Name" COM error. This is getting over my head on the IIsMimeMap ADSI object. I can get the collection object that represents the MIME Map, but how do I add a new item to the collection? The examples I have been able to find on the internet have all listed, but not changed, these objects.

Wan'na throw a brother a hint...?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Scott Hanselman posted an example:

Dim LocalMimeMap, MimeMap
Dim ExtensionToAdd, MimeTypeToAdd
Dim i
Const ADS_PROPERTY_UPDATE = 2
Set LocalMimeMap = GetObject("IIS://localhost/MimeMap")
MimeMap = LocalMimeMap.GetEx("MimeMap")
ExtensionToAdd = InputBox("Extension:","IIS") 'TODO Take this from the Command Line
MimeTypeToAdd = InputBox("MIME Type:","IIS") 'TODO Take this from the Command Line
i = UBound(MimeMap)+1
Redim Preserve MimeMap(i) 'Make it bigger and maintain its contents
Set MimeMap(i) = CreateObject("MimeMap") 'Add onto the end
MimeMap(i).Extension = ExtensionToAdd
MimeMap(i).MimeType = MimeTypeToAdd
LocalMimeMap.PutEx ADS_PROPERTY_UPDATE,"MimeMap",MimeMap 'Poke it back in
LocalMimeMap.SetInfo

I see that he is adding this step that you are not: LocalMimeMap.GetEx("MimeMap")

See if that points you in the right direction.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Scott Hanselman posted an example:

I see that he is adding this step that you are not: LocalMimeMap.GetEx("MimeMap")

See if that points you in the right direction.

Dale

Hmm... OK, that looks like the collection MimeMap is treated as an array, gets ReDim'ed to add a new index, a new object is created in the new index at the endof the array, and then properties are set on that object. I did a quick check with IsArray($colMimeMap), Ubound($colMimeMap, 0), and _ArrayDisplay($colMimeMap) to prove it really is a one dimensional array - and it is. Here is the latest attempt to do this in native AutoIT:

; Test access to IIsMimeMap

; Declare COM Object error handler:
Global $oComError = ObjEvent("AutoIt.Error", "_ComErrFunc")

; Declare ADS property constants for use with PutEx
Const $ADS_PROPERTY_CLEAR = 1, $ADS_PROPERTY_UPDATE = 2, $ADS_PROPERTY_APPEND = 3, $ADS_PROPERTY_DELETE = 4

; Add the new mapping
Local $sNewMap = ".xyz", $sNewType = "text/xml", $i
$oIIsMimeMap = ObjGet("IIS://" & @ComputerName & "/MimeMap")
$colMimeMap = $oIIsMimeMap.GetEx ("MimeMap")
If IsArray($colMimeMap) Then
    $i = UBound($colMimeMap)
    MsgBox(64, "Test", "$colMimeMap is an array with " & UBound($colMimeMap, 0) & " dimensions.", 10)
    ReDim $colMimeMap[$i + 1]
    $colMimeMap[$i] = ObjCreate("MimeMap")
    MsgBox(64, "Test", "Created new object.", 10)
    $colMimeMap[$i].Extension ($sNewMap)
    $colMimeMap[$i].MimeType ($sNewType)
    MsgBox(64, "Test", "Properties set on new object", 10)
    $oIIsMimeMap.PutEx ($ADS_PROPERTY_UPDATE, "MimeMap", $colMimeMap)
    $oIIsMimeMap.SetInfo
    MsgBox(64, "Test", "Saved new MimeMap", 10)
Else
    MsgBox(64, "Test", "$colMimeMap is not an array.")
EndIf


;--------------------------------------
; Function _ComErrFunc()
;   Custom COM object error handler
;--------------------------------------
Func _ComErrFunc()
    Local $HexNumber = Hex($oComError.number, 8)
    MsgBox(16, "Test", "AutoIT COM Error Occured!" & @CRLF & _
            @TAB & "Error Number: " & $HexNumber & @CRLF & _
            @TAB & "Line Number: " & $oComError.scriptline & @CRLF & _
            @TAB & "Description: " & $oComError.description & @CRLF & _
            @TAB & "WinDescription: " & $oComError.windescription)
    SetError(1, -1, ""); something to check for when this function returns
EndFunc   ;==>_ComErrFunc

This throws two COM errors with (see attached screen shot):

Error Number: 80020011

WinDescription: Does not support a collection.

at the point where it is setting the properties of the new object:

$colMimeMap[$i].Extension ($sNewMap)

$colMimeMap[$i].MimeType ($sNewType)

Interestingly, the ObjCreate() and $oIIsMimeMap.PutEx() parts seem to have worked, or at least didn't throw any errors.

Does this translation of the .vbs to .au3 look correct?

:whistle:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

:whistle:AHA! :)

Stupid mistake translating...

MimeMap(i).Extension = ExtensionToAdd
MimeMap(i).MimeType = MimeTypeToAdd

Into...

$colMimeMap[$i].Extension ($sNewMap)
$colMimeMap[$i].MimeType ($sNewType)oÝ÷ Ø(Z¶­¶®¶­sbb33c¶6öÄÖÖTÖ²b33c¶ÒäWFVç6öâÒb33c·4æWtÖ¢b33c¶6öÄÖÖTÖ²b33c¶ÒäÖÖUGRÒb33c·4æWuGP

...and now it works!

I'll post a new version once I make it a function and put some sanity checking in... :lol:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

OK, here is the complete function, _IIS_MimeMap(), including the test code used to try it out on a Windows 2003 server with IIS 6.0.

The function _IIS_MimeMap() can read the Type value of a given extension from the IIsMimeMap object (which is displayed in text in the Metabase.xml file under IIsMimeMap). It can also optionally change an extension or add it to the IIsMimeMap.

_IIS_MimeMap(".ext"[, "Type/Handler"])

The first parameter is the MIME file extension (i.e. .xml, .odt, or .ods), which must start with the dot "." and not contain a comma "," or forward slash "/".

The second parameter is the optional MimeType value to add/change for the given extension (i.e. txt/xml, application/OpenOfficeWriter, application/OpenOfficeCalc).

If MimeType is not given, the function performs a read-only return of the current MimeType value for the given extension.

If the extension exists and the MimeType is given, the MimeType will be changed.

If the extension does not exist and the MimeType is given, it will be appended to the end of the IIsMimeMap.

Note: Changes will not appear instantly in the Metabase.xml file. Retstart IIS to see the changes there.

On success, the function returns the MimeType string for the extension and @error = 0.

On failure, @error = 1 and @extended is set to various values to indicate what went wrong:

@extended = -1 = parameter failed sanity checking

@extended = 0 = the extension was not found during read-only (no Type given) operation

@extended = 1 or greater = COM error occured (assumes an AutoIT COM error function is defined that sets @error)

Thanks to DaleHohm for helping me out with this!

Any remaining screwups are mine - PLEASE POINT THEM OUT! :(

; Test access to IIsMimeMap

; Declare COM Object error handler:
Global $oComError = ObjEvent("AutoIt.Error", "_ComErrFunc")

; Get test values
Global $sNewMap, $sNewType = "", $sResult
$sNewMap = InputBox("Test", 'Input extension (i.e. ".xyz")  ')
If @error Then Exit
$sNewType = InputBox("Test", 'Input MIME Type (i.e. "txt/xml")  ')
If @error Then Exit

; See if mapping is already there
$sResult = _IIS_MimeMap($sNewMap)
If @error Then
    MsgBox(64, "Test", "IIS MIME mapping not found for extention: " & $sNewMap & @CRLF & _
            "@error: " & @error & "   @extended: " & @extended, 30)
Else
    MsgBox(64, "Test", "IIS MIME mapping exits: " & $sNewMap & "," & $sResult, 30)
EndIf

If $sNewType <> "" Then
    ; Add/Change the mapping
    $sResult = _IIS_MimeMap($sNewMap, $sNewType)
    If @error Then
        MsgBox(16, "Test", "Error!  Error adding MIME mapping: " & $sNewMap & "," & $sNewType & @CRLF & _
                "@error: " & @error & "   @extended: " & @extended, 30)
    Else
        MsgBox(64, "Test", "Successfully added MIME mapping: " & $sNewMap & "," & $sResult, 30)
    EndIf

    ; Read-after-write check
    $sResult = _IIS_MimeMap($sNewMap)
    If @error Then
        MsgBox(16, "Test", "IIS MIME mapping not found for extention: " & $sNewMap & @CRLF & _
                "@error: " & @error & "   @extended: " & @extended)
    Else
        MsgBox(64, "Test", "IIS MIME mapping exits: " & $sNewMap & "," & $sResult)
    EndIf
EndIf

;--------------------------------------
; Function _IIS_MimeMap()
;   Reads/Adds/Changes IIS MIME Mappings (only tested on IIS 6)
;   Call with:  _IIS_MimeMap($sExtension[, $sType])
;   Where:  $sExtension = string extension to read/add/change, must start with "." (i.e. ".xyz")
;           $sType = (Optional) string MIME Type to apply for the extension.  If given, $sType
;               must be in the form "type/handler".  If not given, a read-only operation is performed.
;   On success returns the MIME Type value of the given extension.
;   On error sets @error = 1 and
;       @extended:  -1 = $sExtension or $sType failed sanity checks
;                   0  = $sExtension not found during read-only operation
;                   1 thru n = Various specific errors (see code below)
;--------------------------------------
Func _IIS_MimeMap($sExtension, $sType = "")
    Local $oIIsMimeMap, $colMimeMap, $oMimeExt, $bolFound, $i, $avTypeSplit
    
    ; Declare ADS property constants for use with PutEx
    Local $ADS_PROPERTY_CLEAR = 1, $ADS_PROPERTY_UPDATE = 2, $ADS_PROPERTY_APPEND = 3, $ADS_PROPERTY_DELETE = 4
    
    ; Sanity check extension
    If IsString($sExtension) And StringLeft($sExtension, 1) = "." And StringLen($sExtension) > 1 And Not StringInStr($sExtension, ",") And Not StringInStr($sExtension, "/") Then
        ; Read current entry, if extent
        $oIIsMimeMap = ObjGet("IIS://" & @ComputerName & "/MimeMap")
        If IsObj($oIIsMimeMap) Then
            $colMimeMap = $oIIsMimeMap.GetEx ("MimeMap")
            If IsArray($colMimeMap) Then
                ; See if extension already exits
                $bolFound = False
                For $oMimeExt In $colMimeMap
                    If $oMimeExt.Extension = $sExtension Then
                        $bolFound = True
                        ExitLoop
                    EndIf
                Next
                
                ; Check if read or add/change by whether Type was passed
                If $sType = "" Then
                    ; Read only
                    If $bolFound Then
                        Return $oMimeExt.MimeType
                    Else
                        ; Extension to read was not found
                        Return SetError(1, 0, 0)
                    EndIf
                Else
                    ; Add or change
                    ; Sanity check Type value
                    $avTypeSplit = StringSplit($sType, "/")
                    If IsString($sType) And $avTypeSplit[0] = 2 Then
                        ; Check if add or change by whether Extension was found
                        If $bolFound Then
                            ; Change existing entry
                            $oMimeExt.MimeType = $sType
                            If @error Then Return SetError(1, 3, 0)
                        Else
                            ; Append new entry
                            $i = UBound($colMimeMap)
                            ReDim $colMimeMap[$i + 1]
                            $colMimeMap[$i] = ObjCreate("MimeMap")
                            If @error Then Return SetError(1, 4, 0)
                            $colMimeMap[$i].Extension = $sExtension
                            If @error Then Return SetError(1, 5, 0)
                            $colMimeMap[$i].MimeType = $sType
                            If @error Then Return SetError(1, 6, 0)
                        EndIf
                        $oIIsMimeMap.PutEx ($ADS_PROPERTY_UPDATE, "MimeMap", $colMimeMap)
                        If @error Then Return SetError(1, 7, 0)
                        $oIIsMimeMap.SetInfo
                        If @error Then
                            Return SetError(1, 8, 0)
                        Else
                            Return $sType
                        EndIf
                    Else
                        ; $sType failed sanity check
                        Return SetError(1, -1, 0)
                    EndIf
                EndIf
            Else
                ; Failed to get MIME mappings collection
                Return SetError(1, 2, 0)
            EndIf
        Else
            ; Failed to get IIsMimeMap object
            Return SetError(1, 1, 0)
        EndIf
    Else
        ; $sExtension failed sanity check
        Return SetError(1, -1, 0)
    EndIf
EndFunc   ;==>_IIS_MimeMap


;--------------------------------------
; Function _ComErrFunc()
;   Custom COM object error handler
;--------------------------------------
Func _ComErrFunc()
    Local $HexNumber = Hex($oComError.number, 8)
    MsgBox(16, "Test", "AutoIT COM Error Occured!" & @CRLF & _
            @TAB & "Error Number: " & $HexNumber & @CRLF & _
            @TAB & "Line Number: " & $oComError.scriptline & @CRLF & _
            @TAB & "Description: " & $oComError.description & @CRLF & _
            @TAB & "WinDescription: " & $oComError.windescription)
    SetError(1, -1, ""); something to check for when this function returns
EndFunc   ;==>_ComErrFunc

:whistle::):lol::)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...