Getting a Clustered MSMQ Queue Length with .Net and COM

So in reference to my previous post, I finally found a way to extract the queue length of an MSMQ Queue behind a clustered server, and it involves, of all things, COM. There doesn’t seem to be any other non-hackish way to get a queue length fast from a clustered server, the links discussed in my previous post work fine for non clustered environments, but this is the solution for a clustered environment.

  • First:
    Add a COM Reference to the Microsoft Message Object 3.0 Type Library (if on Windows XP or Server 2003).If you’re stuck on vista then manually browse to C:\windows\system32\mqoa.dll and add that as a reference.
  • Second:
    Use the following code and pay special attention to the format of the FormatName parameter and the server parameter. It took me a long time of fiddling with string formats to get the correct one and no one else on the lazy web seems to have mentioned the specific format needed.
    int queueCount;
    MSMQ.MSMQManagement mgmt = new MSMQ.MSMQManagement();
    object oMissing = Type.Missing;
    object server = (object) "MSMQServer";
    object formatName = (object) "Direct=OS:MSMQServer\private$\queueName";
    mgmt.Init(ref server, ref oMissing, ref formatName);
    Console.WriteLine("response " + mgmt.MessageCount.ToString() + Environment.NewLine);
    int.TryParse(mgmt.MessageCount.ToString(), out queueCount);

Apologies for the bad vertical spacing, wordpress keeps on insisting on closing my <code> section early if I insert empty newlines in it.

1 Response to “Getting a Clustered MSMQ Queue Length with .Net and COM”


  • Getting FormatNames right is one of the main stumbling blocks in MSAQ programming. If there is one thing I could change in the product it would be to scrap the PathName/Formatname mess and create something simpler.
    I do, though, have the advantage of hindsight here – the design goals for MSMQ in the early 90s would not be the same now as customer environments and needs have changed/evolved so much – so I can cut out the parts that I know don’t work or get used.

    Here’s a good resource for FormatName examples.
    MSMQQueueInfo.FormatName
    http://msdn.microsoft.com/en-us/library/ms705703(VS.85).aspx

    Cheers
    John

Leave a Reply