Visual Basic Code Example: Reading Messages in the Computer Journal
This example provides a Sub procedure that synchronously reads all the messages in the computer journal of a known computer.
This example receives the computer name and generates the machine format name needed to open the computer journal for reading messages from the computer GUID obtained by calling the MSMQApplication.MachineIdOfMachineName method. This method cannot be used if there is no connection to the directory service. This restriction applies to dependent clients, independent clients that are working offline, and Message Queuing routing servers.
For information on when Message Queuing sends messages to the computer journal, see Source Journaling.
In MSMQ 2.0 and later, this procedure can be modified to generate the direct format name of the computer journal instead of retrieving its machine format name.
To read message in a computer journal
Declare the objects needed to retrieve the message.
Note When declaring the MSMQMessage object when reading messages, the New keyword cannot be used.
Obtain the identifier of the computer provided by the caller. This identifier is needed to create a format name for the computer journal.
Set the MSMQQueueInfo.FormatName property. The syntax for referencing the computer journal is as follows:
MSMQQueueInfo.FormatName = "DIRECT=OS:" & wszComputerName & "\SYSTEM$;JOURNAL"
Call MSMQQueueInfo.Open to open the computer journal with receive access. When opening a queue with receive access the application can peek at or retrieve the messages in the queue.
Using a loop, call MSMQQueue.Receive to read each messages in the computer journal.
When there are no messages left, call MSMQQueue.Close to release resources used to open journal and exit the Sub procedure.
The following code example can be run on all versions of Message Queuing.
Sub ReadingComputerJournal( _ strComputerName As String _ ) Dim qinfo As New MSMQQueueInfo Dim q As MSMQQueue Dim msg As MSMQMessage Dim strMachineId As String ' Obtain the computer GUID for the format name. On Error GoTo ErrorHandler strMachineId = MachineIdOfMachineName(strComputerName) ' Set the format name of computer journal. qinfo.FormatName = "MACHINE=" & strMachineId & ";JOURNAL" ' Open the computer journal. Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, _ ShareMode:=MQ_DENY_NONE) ' Read the first message in the computer journal. Set msg = q.Receive(ReceiveTimeout:=1000) ' Read the remaining messages in the computer journal. Do While Not msg Is Nothing MsgBox "A message was removed from the computer journal." Set msg = q.Receive(ReceiveTimeout:=1000) Loop MsgBox "There are no more messages. The computer journal will be closed." q.Close Exit Sub ErrorHandler: MsgBox "Error " + Hex(Err.Number) + " was returned." _ + Chr(13) + Err.Description If Not q Is Nothing And q.IsOpen2 Then q.Close EndIf End Sub