How to compare Microsoft Message Queuing identifiers using Visual Basic (255702)



The information in this article applies to:

  • Microsoft Message Queue Server (MSMQ) 1.0
  • Microsoft Message Queuing 2.0

This article was previously published under Q255702

SUMMARY

Microsoft Message Queuing messages can be individually identified by their message identifier property. These message identifiers can be used to determine if two messages are the same; to find a specific message when searching in a queue (for instance, when responding to an Microsoft Message Queuing Trigger), and anytime a specific message needs to be found.

MORE INFORMATION

The message identifier is a 20-byte long array. Following is an excerpt from the MSDN online Help for the MSMQMessage.Id property:

Microsoft Message Queuing generates a 20-byte message identifier when the message is sent. It 
is composed of the machine GUID of the computer that sent the message plus 
an identifier that is unique to the computer. By combining these two 
components, the message identifier is unique within your enterprise.
					


Because the message identifier is an array of bytes, then two message identifiers can be compared by doing a byte-by-byte comparison of each member of the arrays. If all the individual bytes match, then the identifiers are the same. To make this comparison in Visual Basic, simply cycle through the array and compare each member as the following sample code demonstrates:
Function IsMessageIdEqual(m As MSMQMessage, m2 As MSMQMessage) As Boolean
    Dim i As Integer
    For i = LBound(m.Id) To UBound(m.Id)
        If m.Id(i) <> m2.Id(i) Then
            IsMessageIdEqual = False
            Exit Function
        End If
    Next
    IsMessageIdEqual = True
End Function
				

The IsMessageIdEqual function can be demonstrated with a simple test program that sends two messages to a queue then tests the message identifiers of the messages. To create this simple test program complete the following steps:
  1. Start a new Visual Basic Standard EXE project.
  2. Add a reference to the MSMQ Object Library by doing the following:

    1. From the Project menu, select References.
    2. In the References dialog box, select Microsoft Message Queue Object Library.
    3. Click Ok.
  3. Add the following code to the default form code window:
    Function IsMessageIdEqual(m As MSMQMessage, m2 As MSMQMessage) As Boolean
        Dim i As Integer
        
        Debug.Print "=== IsMessageIdEqual === "
        Debug.Print "m.Id  = " & MessageIdToString(m)
        Debug.Print "m2.Id = " & MessageIdToString(m2)
        
        For i = LBound(m.Id) To UBound(m.Id)
            If m.Id(i) <> m2.Id(i) Then
                IsMessageIdEqual = False
                Exit Function
            End If
        Next
        IsMessageIdEqual = True
    End Function
    
    Function AlignHex(ByVal b As Byte) As String
        AlignHex = String(2 - Len(Hex$(b)), "0") + Hex$(b)
    End Function
    
    Function MessageIdToString(m As MSMQMessage) As String
        Dim i As Integer
        Dim s As String
        Dim dw As Long
        ' DWORD
        s = "{"
        For i = 3 To 0 Step -1
            s = s + AlignHex(m.Id(i))
        Next
        s = s + "-"
        ' WORD
        For i = 5 To 4 Step -1
            s = s + AlignHex(m.Id(i))
        Next
        s = s + "-"
        ' WORD
        For i = 7 To 6 Step -1
            s = s + AlignHex(m.Id(i))
        Next
        s = s + "-"
        ' WORD
        For i = 8 To 9
            s = s + AlignHex(m.Id(i))
        Next
        s = s + "-"
        ' DATA
        For i = 10 To 15
            s = s + AlignHex(m.Id(i))
        Next
        s = s + "}"
        ' message ordinal
        Dim ordinal As Long
        ordinal = 0
        For i = 19 To 16 Step -1
            ordinal = ordinal * 256 + m.Id(i)
        Next
        s = s & "\" & ordinal
        MessageIdToString = s
    End Function
    						
  4. Add a Command Button ("Command1") to the form.
  5. Add the following code to the Click Event for Command1. Note that the queue needs to be created prior to running this code. The default queue name is "ggmtestrequestqueue" and it needs to be created on the same computer that this code sample is run on. Alternatively, change the path name, or PathName, in the sample code.
    Private Sub Command1_Click()
    On Error GoTo ERRORHANDLER
        Dim QInfo As MSMQQueueInfo  ' Queue Info object
        Dim Q As MSMQQueue          ' The queue
        Dim message1 As MSMQMessage ' a message object
        Dim message2 As MSMQMessage ' and another message
        
        Set QInfo = New MSMQQueueInfo
        
        ' ***
        ' NOTE: Insert an appropriate queue name for your setup here.
        ' ***
        QInfo.Pathname = ".\ggmtestrequestqueue"
        
        ' Open the queue.
        Set Q = QInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
        
        ' Create the messages.
        Set message1 = New MSMQMessage
        message1.Label = "Test message 1"
        message1.Body = "Test"
        
        Set message2 = New MSMQMessage
        message2.Label = "Test Message 2"
        message2.Body = "Test"
        
        ' Send the messages.
        message1.Send Q
        message2.Send Q
        
        ' Now compare the identifiers.
        
        ' Compare message1 to message2.
        ' Because these are two different messages, we expect them to be
        ' different.
        If IsMessageIdEqual(message1, message2) Then
            MsgBox "The messages have the same message identifier", vbOKOnly, "Compare of Message1 & Message2"
        Else
            MsgBox "The message have different identifiers", vbOKOnly, "Compare of Message1 & Message2"
        End If
        
        ' Compare message1 to itself.  In this case, we expect the message
        ' identifiers to be the same.
        If IsMessageIdEqual(message1, message1) Then
            MsgBox "The messages have the same message identifier", vbOKOnly, "Compare of Message1 & Message1"
        Else
            MsgBox "The message have different identifiers", vbOKOnly, "Compare of Message1 & Message1"
        End If
        
        Exit Sub
    ERRORHANDLER:
        MsgBox "There was an error in Command1_Click " & vbCrLf _
               & "Error Number: " & Hex(Err.Number) & vbCrLf _
               & "Error Description: " & Err.Description, vbCritical
    End Sub
    						
  6. Press F5 or click Run to start the project.
  7. Click Command1 and note the output of the message boxes. Also, the message identifiers of the messages passed to the comparison function are printed to the debug window for visual comparison.

REFERENCES

  • Microsoft MSDN Help for the MSMQMessage.Id Property
  • The Visual Basic Programmers Guide

Modification Type:MajorLast Reviewed:4/20/2005
Keywords:kbhowto KB255702