Created
September 19, 2011 01:03
-
-
Save ovatsus/1225792 to your computer and use it in GitHub Desktop.
Counting the number of messages in a Message Queue in .NET - safe version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Messaging; | |
using System.Runtime.InteropServices; | |
public static class MessageQueueExtensions { | |
[DllImport("mqrt.dll")] | |
private static extern int MQMgmtGetInfo([MarshalAs(UnmanagedType.BStr)]string computerName, [MarshalAs(UnmanagedType.BStr)]string objectName, ref MQMGMTPROPS mgmtProps); | |
private const byte VT_NULL = 1; | |
private const byte VT_UI4 = 19; | |
private const int PROPID_MGMT_QUEUE_MESSAGE_COUNT = 7; | |
//size must be 16 | |
[StructLayout(LayoutKind.Sequential)] | |
private struct MQPROPVariant { | |
public byte vt; //0 | |
public byte spacer; //1 | |
public short spacer2; //2 | |
public int spacer3; //4 | |
public uint ulVal; //8 | |
public int spacer4; //12 | |
} | |
//size must be 16 in x86 and 28 in x64 | |
[StructLayout(LayoutKind.Sequential)] | |
private struct MQMGMTPROPS { | |
public uint cProp; | |
public IntPtr aPropID; | |
public IntPtr aPropVar; | |
public IntPtr status; | |
} | |
public static uint GetCount(this MessageQueue queue) { | |
return GetCount(queue.Path); | |
} | |
private static uint GetCount(string path) { | |
if (!MessageQueue.Exists(path)) { | |
return 0; | |
} | |
MQMGMTPROPS props = new MQMGMTPROPS { cProp = 1 }; | |
try { | |
props.aPropID = Marshal.AllocHGlobal(sizeof(int)); | |
Marshal.WriteInt32(props.aPropID, PROPID_MGMT_QUEUE_MESSAGE_COUNT); | |
props.aPropVar = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MQPROPVariant))); | |
Marshal.StructureToPtr(new MQPROPVariant { vt = VT_NULL }, props.aPropVar, false); | |
props.status = Marshal.AllocHGlobal(sizeof(int)); | |
Marshal.WriteInt32(props.status, 0); | |
int result = MQMgmtGetInfo(null, "queue=Direct=OS:" + path, ref props); | |
if (result != 0 || Marshal.ReadInt32(props.status) != 0) { | |
return 0; | |
} | |
MQPROPVariant propVar = (MQPROPVariant)Marshal.PtrToStructure(props.aPropVar, typeof(MQPROPVariant)); | |
if (propVar.vt != VT_UI4) { | |
return 0; | |
} else { | |
return propVar.ulVal; | |
} | |
} finally { | |
Marshal.FreeHGlobal(props.aPropID); | |
Marshal.FreeHGlobal(props.aPropVar); | |
Marshal.FreeHGlobal(props.status); | |
} | |
} | |
} | |
public class MessageQueueExtensionsTest { | |
public static void Main() { | |
string queueName = @".\Private$\MyQueue"; | |
if (MessageQueue.Exists(queueName)) { | |
MessageQueue.Delete(queueName); | |
} | |
MessageQueue queue = MessageQueue.Create(queueName); | |
Console.WriteLine("Count should be 0: " + queue.GetCount()); | |
queue.Send("ping", "ping"); | |
Console.WriteLine("Count should be 1: " + queue.GetCount()); | |
queue.Send("ping2", "ping2"); | |
Console.WriteLine("Count should be 2: " + queue.GetCount()); | |
queue.Receive(); | |
Console.WriteLine("Count should be 1: " + queue.GetCount()); | |
queue.Send("ping3", "ping3"); | |
Console.WriteLine("Count should be 2: " + queue.GetCount()); | |
MessageQueue.Delete(queueName); | |
Console.WriteLine("Count should be 0: " + queue.GetCount()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment