/// <summary> /// Gets the count. /// http://blog.codebeside.org/archive/2008/08/27/counting-the-number-of-messages-in-a-message-queue-in.aspx /// </summary> /// <param name="self">The self.</param> /// <returns></returns> public static int GetCount(this MessageQueue self) { if (!MessageQueue.Exists(self.MachineName + @"\" + self.QueueName)) { return(0); } var props = new NativeMethods.MQMGMTPROPS { cProp = 1 }; try { props.aPropID = Marshal.AllocHGlobal(sizeof(int)); Marshal.WriteInt32(props.aPropID, NativeMethods.PROPID_MGMT_QUEUE_MESSAGE_COUNT); props.aPropVar = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.MQPROPVariant))); Marshal.StructureToPtr(new NativeMethods.MQPROPVariant { vt = NativeMethods.VT_NULL }, props.aPropVar, false); props.status = Marshal.AllocHGlobal(sizeof(int)); Marshal.WriteInt32(props.status, 0); int result = NativeMethods.MQMgmtGetInfo(null, "queue=" + self.FormatName, ref props); if (result != 0) { throw new Win32Exception(result); } if (Marshal.ReadInt32(props.status) != 0) { return(0); } var propVar = (NativeMethods.MQPROPVariant) Marshal.PtrToStructure(props.aPropVar, typeof(NativeMethods.MQPROPVariant)); if (propVar.vt != NativeMethods.VT_UI4) { return(0); } else { return(Convert.ToInt32(propVar.ulVal)); } } finally { Marshal.FreeHGlobal(props.aPropID); Marshal.FreeHGlobal(props.aPropVar); Marshal.FreeHGlobal(props.status); } }
/// <summary> /// Gets the count. /// http://blog.codebeside.org/archive/2008/08/27/counting-the-number-of-messages-in-a-message-queue-in.aspx /// </summary> /// <param name="self">The self.</param> /// <returns></returns> public static int GetCount(this MessageQueue self) { if (!MessageQueue.Exists(self.MachineName + @"\" + self.QueueName)) { return 0; } var props = new NativeMethods.MQMGMTPROPS { cProp = 1 }; try { props.aPropID = Marshal.AllocHGlobal(sizeof(int)); Marshal.WriteInt32(props.aPropID, NativeMethods.PROPID_MGMT_QUEUE_MESSAGE_COUNT); props.aPropVar = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.MQPROPVariant))); Marshal.StructureToPtr(new NativeMethods.MQPROPVariant { vt = NativeMethods.VT_NULL }, props.aPropVar, false); props.status = Marshal.AllocHGlobal(sizeof(int)); Marshal.WriteInt32(props.status, 0); int result = NativeMethods.MQMgmtGetInfo(null, "queue=" + self.FormatName, ref props); if (result != 0) throw new Win32Exception(result); if (Marshal.ReadInt32(props.status) != 0) { return 0; } var propVar = (NativeMethods.MQPROPVariant)Marshal.PtrToStructure(props.aPropVar, typeof(NativeMethods.MQPROPVariant)); if (propVar.vt != NativeMethods.VT_UI4) { return 0; } else { return Convert.ToInt32(propVar.ulVal); } } finally { Marshal.FreeHGlobal(props.aPropID); Marshal.FreeHGlobal(props.aPropVar); Marshal.FreeHGlobal(props.status); } }