前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >解决msmq接收远程主机私有队列消息的问题!

解决msmq接收远程主机私有队列消息的问题!

作者头像
阿新
发布2018-04-13 10:25:36
2.3K0
发布2018-04-13 10:25:36
举报
文章被收录于专栏:c#开发者

通过调用windows\system32\mqoa.dll 这个Message Queuing ActiveX Interface API函数就可以做到;

下面是一个简单的例子供参考;

代码语言:javascript
复制
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using MSMQ;
namespace MyTest
{
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
        #region Forms
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.TextBox txthost;
        private System.Windows.Forms.TextBox txtpath;
        private System.Windows.Forms.RadioButton trans1;
        private System.Windows.Forms.RadioButton trans2;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.TextBox txtmsg;
        private System.Windows.Forms.TextBox txtrece;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form2()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #endregion 

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.trans2 = new System.Windows.Forms.RadioButton();
            this.trans1 = new System.Windows.Forms.RadioButton();
            this.label3 = new System.Windows.Forms.Label();
            this.txtpath = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txthost = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.button2 = new System.Windows.Forms.Button();
            this.txtrece = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.txtmsg = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.groupBox2);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.txtpath);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.txthost);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new System.Drawing.Point(, );
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(, );
            this.groupBox1.TabIndex = ;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "MSMQ Configuration";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.trans2);
            this.groupBox2.Controls.Add(this.trans1);
            this.groupBox2.Location = new System.Drawing.Point(, );
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(, );
            this.groupBox2.TabIndex = ;
            this.groupBox2.TabStop = false;
            // 
            // trans2
            // 
            this.trans2.Checked = true;
            this.trans2.Location = new System.Drawing.Point(, );
            this.trans2.Name = "trans2";
            this.trans2.Size = new System.Drawing.Size(, );
            this.trans2.TabIndex = ;
            this.trans2.TabStop = true;
            this.trans2.Text = "true";
            // 
            // trans1
            // 
            this.trans1.Location = new System.Drawing.Point(, );
            this.trans1.Name = "trans1";
            this.trans1.Size = new System.Drawing.Size(, );
            this.trans1.TabIndex = ;
            this.trans1.Text = "true";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(, );
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(, );
            this.label3.TabIndex = ;
            this.label3.Text = "Transactional";
            // 
            // txtpath
            // 
            this.txtpath.Location = new System.Drawing.Point(, );
            this.txtpath.Name = "txtpath";
            this.txtpath.Size = new System.Drawing.Size(, );
            this.txtpath.TabIndex = ;
            this.txtpath.Text = "Private$\\test";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(, );
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(, );
            this.label2.TabIndex = ;
            this.label2.Text = "Path";
            // 
            // txthost
            // 
            this.txthost.Location = new System.Drawing.Point(, );
            this.txthost.Name = "txthost";
            this.txthost.Size = new System.Drawing.Size(, );
            this.txthost.TabIndex = ;
            this.txthost.Text = "127.0.0.1";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(, );
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(, );
            this.label1.TabIndex = ;
            this.label1.Text = "Host";
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.button2);
            this.groupBox3.Controls.Add(this.txtrece);
            this.groupBox3.Controls.Add(this.label5);
            this.groupBox3.Controls.Add(this.button1);
            this.groupBox3.Controls.Add(this.txtmsg);
            this.groupBox3.Controls.Add(this.label4);
            this.groupBox3.Location = new System.Drawing.Point(, );
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(, );
            this.groupBox3.TabIndex = ;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "MSMQ Test";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(, );
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(, );
            this.button2.TabIndex = ;
            this.button2.Text = "&Receive";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // txtrece
            // 
            this.txtrece.Location = new System.Drawing.Point(, );
            this.txtrece.Multiline = true;
            this.txtrece.Name = "txtrece";
            this.txtrece.Size = new System.Drawing.Size(, );
            this.txtrece.TabIndex = ;
            this.txtrece.Text = "Hello World ";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(, );
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(, );
            this.label5.TabIndex = ;
            this.label5.Text = "Receive Message";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(, );
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(, );
            this.button1.TabIndex = ;
            this.button1.Text = "&Send";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // txtmsg
            // 
            this.txtmsg.Location = new System.Drawing.Point(, );
            this.txtmsg.Name = "txtmsg";
            this.txtmsg.Size = new System.Drawing.Size(, );
            this.txtmsg.TabIndex = ;
            this.txtmsg.Text = "Hello World ";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(, );
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(, );
            this.label4.TabIndex = ;
            this.label4.Text = "Send Message";
            // 
            // Form2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(, );
            this.ClientSize = new System.Drawing.Size(, );
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.Load += new System.EventHandler(this.Form2_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox3.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        MSMQ.MSMQQueueInfoClass mqinfor=new MSMQQueueInfoClass();
        private void Form2_Load(object sender, System.EventArgs e)
        {
            
            
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            mqinfor.FormatName="Direct=TCP:" + this.txthost.Text + "\\" + this.txtpath.Text; 
            MSMQ.MSMQQueue mq=mqinfor.Open(MSMQ.MQACCESS.MQ_SEND_ACCESS.GetHashCode(),MSMQ.MQSHARE.MQ_DENY_NONE.GetHashCode());
            MSMQ.MSMQMessageClass msg=new MSMQMessageClass();
            msg.Label="activeX Test";
            msg.Body=this.txtmsg.Text;
            
            MSMQ.MSMQTransactionClass MQ_NO_TRANSACTION=new MSMQTransactionClass();
            
            object a=MQ_NO_TRANSACTION;
            msg.Send(mq,ref a);
            

        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            try
            {
                mqinfor.FormatName="Direct=TCP:" + this.txthost.Text + "\\" + this.txtpath.Text; 
            
                MSMQ.MSMQQueue mq=mqinfor.Open(MSMQ.MQACCESS.MQ_RECEIVE_ACCESS.GetHashCode(),MSMQ.MQSHARE.MQ_DENY_NONE.GetHashCode());
            
                MSMQ.MSMQTransactionClass MQ_NO_TRANSACTION=new MSMQTransactionClass();
                object a=MQ_NO_TRANSACTION;
                bool WantDestinationQueue=false;
                object b=WantDestinationQueue;
                bool WantBody =true;
                object c=WantBody;
                long ReceiveTimeout =;
                object d=ReceiveTimeout;
                bool WantConnectorType=false;
                object e1=WantConnectorType;
                MSMQ.MSMQMessage msg=mq.Receive(ref a,ref b,ref c,ref d,ref e1);
                this.txtrece.Text=DateTime.Now.ToString() + " " + msg.Body.ToString();
            }
            catch(Exception e2)
            {
                MessageBox.Show(e2.Message);
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2005-11-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档