Computer Management can be used to inspect queues on Windows 2000, as well as to create new queues and look at queued messages. To run Computer Management the following steps are required:
Message queuing (Figure 15.1) allows four types of queue to be managed: Figure 15.1. Managing queues using Computer Management
Creating a Private QueuePrivate queues can be created programmatically or, more easily, can be created using Computer Management.
Once a private queue has been created, it can be accessed by applications running on any computer that has the appropriate security access. The code in the next sections shows how to read messages from a queue created on a Windows 2000 computer using Visual Basic and how to write messages to the queue from a Windows CE device using C++ (Figure 15.2). Figure 15.2. Reading and writing a queueReading Messages from a Queue in Windows 2000The Computer Manager does not allow messages to be added to or read from a queue you must write code to do this. You will find a Visual Basic project in the directory \QueueServer on the CDROM containing the code described in this chapter. On Windows 2000 Visual Basic can be used with the Microsoft Message Queue object model:
The code in the "QueueServer" application opens a private queue when the form is loaded. The form has a timer control that fires an event every two seconds, and this checks to see if a new message has arrived in the queue. In the next section you will find code that runs on a Windows CE device that adds messages to this queue. Finally, the queue is closed when the form unloads: Private qi As MSMQQueueInfo Private q As MSMQQueuePrivate Sub Form_Load() Set qi = New MSMQQueueInfo qi.PathName = ".\Private$\WinCEQueue" Set q = qi.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE) End Sub Private Sub Form_Unload(Cancel As Integer) q.Close End Sub Private Sub tmrMessage_Timer() Dim msg As MSMQMessage Dim s As String Dim sTime As String Dim sLabel As String Dim sBody As String Dim sSent As String ' check for message Set msg = q.Receive(ReceiveTimeout:=0) If (Not (msg Is Nothing)) Then ' have got a message sTime = msg.ArrivedTime sLabel = msg.Label sBody = msg.Body sSent = msg.SentTime s = s & "Sent:" & sSent & _ " Arrived: " & sTime & ":" & _ sLabel & vbCrLf & sBody & vbCrLf txtMessageLog.Text = s + txtMessageLog.Text End If End Sub Three Message Queue objects are used in this code:
The queue name is specified using the "PathName" property. In this case a private queue called "WinCEQueue" on the local machine (indicated by ".") is specified. The queue is opened using the "Open" method. This then is passed information about how the queue is to be accessed (MQ_RECEIVE_ACCESS indicates that messages are to be read from the queue) and queue-sharing options (MQ_DENY_NONE means that other applications can open the queue for reading and writing while this application has the queue open). The Receive method is called on an open queue in the timer event and checks to see if a message has arrived. This method is passed a single optional ReceiveTimeout parameter that specifies how long to wait before timing out. In this case the value 0 specifies no timeout value, so the call will return immediately if no message is waiting. This, together with the use of a timer, ensures that the Visual Basic application is not blocked waiting for messages. If a message is waiting, a MSMQMessage object is returned. The MSMQMessage object is used to access the message's label, body, time sent, and time received, and this information is added to a text box. In the next section some Windows CE code will be demonstrated that will write a message to the queue used by this Visual Basic application.
|