This is sort of defeating the main point of using MSMQ – that is, being able to send a message without worrying if the destination is available or not – but there may be a good business reason for wanting to.
Just because a machine is on-line does not mean that the MSMQ service itself is up and running. So just sending an ICMP PING to the IP address of the machine is not as helpful as you’d think.
One option is to use MSMQ’s own MQPing system. As discussed here, MQPing sends a normal MSMQ message labelled “QM-Admin Commands” and there will be a corresponding message returned to the sender labelled “Ping Response”. So you could send and your receive your own MQPIng message from within your application.
‘ Author: Yoel Arnon (email@example.com)
‘ Date: 05-Jul-2001
‘ Purpose: MQping using DIRECT format name.
‘ Can be used for Workgroup, cross enterprise ping, as well as regular MQPing.
Set objArgs = WScript.Arguments
if (objArgs.Count = 0) Then
WScript.Echo “Usage: MQping <MSMQ Computer to Ping> [<Timeout in seconds>]”
if (objArgs.Count > 1) Then
TimeOut = objArgs(1) * 1000 ‘ Miliseconds
TimeOut = 30000 ‘ 30 seconds
ComputerToPing = objArgs(0)
‘ Ping messages are sent to the target’s admin queue
Set RemoteAdminQI = WScript.CreateObject(“MSMQ.MSMQQueueInfo”)
RemoteAdminQI.FormatName = “DIRECT=OS:” + ComputerToPing + “private$admin_queue$”
Set RemoteAdminQ = RemoteAdminQI.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
Set LocalRespQI = WScript.CreateObject(“MSMQ.MSMQQueueInfo”)
LocalRespQI.PathName = “.Private$PingRespQ”
On Error Resume Next
‘ Delete queue if exist – may contain old response messages
On Error Goto 0
Set WshNetwork = WScript.CreateObject(“WScript.Network”)
‘ Build a DIRECT format name on my computer for the response queue
‘ Note: although the response queue already contains a format name, it may not be a DIRECT one.
LocalRespQI.formatName = “DIRECT=OS:” + WshNetwork.ComputerName + “Private$PingRespQ”
Set LocalRespQ = LocalRespQI.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
‘ Send the Ping Message
Set AdminMsg = WScript.CreateObject(“MSMQ.MSMQMessage”)
‘ Ping message format:
‘ Label must be “QM-Admin Commands”
‘ Body contains “Ping=” and response string
‘ Ping reply will be sent to response queue
AdminMsg.Label = “QM-Admin Commands”
AdminMsg.Body = “Ping=” & ComputerToPing
AdminMsg.MaxTimeToReachQueue = TimeOut
Set AdminMsg.ResponseQueueInfo = LocalRespQI
‘ Wait for response
Set ReturnMsg = LocalRespQ.Receive(,,,TimeOut)
if ReturnMsg Is Nothing Then
WScript.Echo “MQPing Failed”
‘ Check reply
‘ Reply begins with one NULL byte, then unicode “=” and then the response string,
‘ but no NULL terminator. We want to see if this is equal to what we sent.
‘ We have no choice but comparing the prefix (we may, for example, confuse the
‘ reply from “comp” and “comp01”, but this is a risk we should take).
RespondingComputer = MidB(ReturnMsg.Body,4) ‘ Excluding a leading NULL byte and Unicode “=”
‘ Note – the response may contain extra characters. We should look at the prefix
if ComputerToPing = LeftB(RespondingComputer, lenB(ComputerToPing)) Then
WScript.Echo “MQPing Succeeded”
WScript.Echo “Someone else answered your MQPing – Please Re-Run MQPing”