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

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

下面是一个简单的例子供参考;
 
ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifusing System;
  2None.gifusing System.Drawing;
  3None.gifusing System.Collections;
  4None.gifusing System.ComponentModel;
  5None.gifusing System.Windows.Forms;
  6None.gifusing MSMQ;
  7None.gifnamespace MyTest
  8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 10InBlock.gif    /// Form2 的摘要说明。
 11ExpandedSubBlockEnd.gif    /// </summary>

 12InBlock.gif    public class Form2 : System.Windows.Forms.Form
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 14ContractedSubBlock.gifExpandedSubBlockStart.gif        Forms#region Forms
 15InBlock.gif        private System.Windows.Forms.GroupBox groupBox1;
 16InBlock.gif        private System.Windows.Forms.Label label1;
 17InBlock.gif        private System.Windows.Forms.Label label2;
 18InBlock.gif        private System.Windows.Forms.Label label3;
 19InBlock.gif        private System.Windows.Forms.GroupBox groupBox2;
 20InBlock.gif        private System.Windows.Forms.TextBox txthost;
 21InBlock.gif        private System.Windows.Forms.TextBox txtpath;
 22InBlock.gif        private System.Windows.Forms.RadioButton trans1;
 23InBlock.gif        private System.Windows.Forms.RadioButton trans2;
 24InBlock.gif        private System.Windows.Forms.GroupBox groupBox3;
 25InBlock.gif        private System.Windows.Forms.Label label4;
 26InBlock.gif        private System.Windows.Forms.Button button1;
 27InBlock.gif        private System.Windows.Forms.Label label5;
 28InBlock.gif        private System.Windows.Forms.Button button2;
 29InBlock.gif        private System.Windows.Forms.TextBox txtmsg;
 30InBlock.gif        private System.Windows.Forms.TextBox txtrece;
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 32InBlock.gif        /// 必需的设计器变量。
 33ExpandedSubBlockEnd.gif        /// </summary>

 34InBlock.gif        private System.ComponentModel.Container components = null;
 35InBlock.gif
 36InBlock.gif        public Form2()
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38InBlock.gif            //
 39InBlock.gif            // Windows 窗体设计器支持所必需的
 40InBlock.gif            //
 41InBlock.gif            InitializeComponent();
 42InBlock.gif
 43InBlock.gif            //
 44InBlock.gif            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 45InBlock.gif            //
 46ExpandedSubBlockEnd.gif        }

 47InBlock.gif
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 49InBlock.gif        /// 清理所有正在使用的资源。
 50ExpandedSubBlockEnd.gif        /// </summary>

 51InBlock.gif        protected override void Dispose( bool disposing )
 52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 53InBlock.gif            if( disposing )
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                if(components != null)
 56ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 57InBlock.gif                    components.Dispose();
 58ExpandedSubBlockEnd.gif                }

 59ExpandedSubBlockEnd.gif            }

 60InBlock.gif            base.Dispose( disposing );
 61ExpandedSubBlockEnd.gif        }

 62ExpandedSubBlockEnd.gif        #endregion
 
 63InBlock.gif
 64ContractedSubBlock.gifExpandedSubBlockStart.gif        Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 66InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 67InBlock.gif        /// 此方法的内容。
 68ExpandedSubBlockEnd.gif        /// </summary>

 69InBlock.gif        private void InitializeComponent()
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            this.groupBox1 = new System.Windows.Forms.GroupBox();
 72InBlock.gif            this.groupBox2 = new System.Windows.Forms.GroupBox();
 73InBlock.gif            this.trans2 = new System.Windows.Forms.RadioButton();
 74InBlock.gif            this.trans1 = new System.Windows.Forms.RadioButton();
 75InBlock.gif            this.label3 = new System.Windows.Forms.Label();
 76InBlock.gif            this.txtpath = new System.Windows.Forms.TextBox();
 77InBlock.gif            this.label2 = new System.Windows.Forms.Label();
 78InBlock.gif            this.txthost = new System.Windows.Forms.TextBox();
 79InBlock.gif            this.label1 = new System.Windows.Forms.Label();
 80InBlock.gif            this.groupBox3 = new System.Windows.Forms.GroupBox();
 81InBlock.gif            this.button2 = new System.Windows.Forms.Button();
 82InBlock.gif            this.txtrece = new System.Windows.Forms.TextBox();
 83InBlock.gif            this.label5 = new System.Windows.Forms.Label();
 84InBlock.gif            this.button1 = new System.Windows.Forms.Button();
 85InBlock.gif            this.txtmsg = new System.Windows.Forms.TextBox();
 86InBlock.gif            this.label4 = new System.Windows.Forms.Label();
 87InBlock.gif            this.groupBox1.SuspendLayout();
 88InBlock.gif            this.groupBox2.SuspendLayout();
 89InBlock.gif            this.groupBox3.SuspendLayout();
 90InBlock.gif            this.SuspendLayout();
 91InBlock.gif            // 
 92InBlock.gif            // groupBox1
 93InBlock.gif            // 
 94InBlock.gif            this.groupBox1.Controls.Add(this.groupBox2);
 95InBlock.gif            this.groupBox1.Controls.Add(this.label3);
 96InBlock.gif            this.groupBox1.Controls.Add(this.txtpath);
 97InBlock.gif            this.groupBox1.Controls.Add(this.label2);
 98InBlock.gif            this.groupBox1.Controls.Add(this.txthost);
 99InBlock.gif            this.groupBox1.Controls.Add(this.label1);
100InBlock.gif            this.groupBox1.Location = new System.Drawing.Point(88);
101InBlock.gif            this.groupBox1.Name = "groupBox1";
102InBlock.gif            this.groupBox1.Size = new System.Drawing.Size(352120);
103InBlock.gif            this.groupBox1.TabIndex = 0;
104InBlock.gif            this.groupBox1.TabStop = false;
105InBlock.gif            this.groupBox1.Text = "MSMQ Configuration";
106InBlock.gif            // 
107InBlock.gif            // groupBox2
108InBlock.gif            // 
109InBlock.gif            this.groupBox2.Controls.Add(this.trans2);
110InBlock.gif            this.groupBox2.Controls.Add(this.trans1);
111InBlock.gif            this.groupBox2.Location = new System.Drawing.Point(10472);
112InBlock.gif            this.groupBox2.Name = "groupBox2";
113InBlock.gif            this.groupBox2.Size = new System.Drawing.Size(14440);
114InBlock.gif            this.groupBox2.TabIndex = 5;
115InBlock.gif            this.groupBox2.TabStop = false;
116InBlock.gif            // 
117InBlock.gif            // trans2
118InBlock.gif            // 
119InBlock.gif            this.trans2.Checked = true;
120InBlock.gif            this.trans2.Location = new System.Drawing.Point(7216);
121InBlock.gif            this.trans2.Name = "trans2";
122InBlock.gif            this.trans2.Size = new System.Drawing.Size(6419);
123InBlock.gif            this.trans2.TabIndex = 1;
124InBlock.gif            this.trans2.TabStop = true;
125InBlock.gif            this.trans2.Text = "true";
126InBlock.gif            // 
127InBlock.gif            // trans1
128InBlock.gif            // 
129InBlock.gif            this.trans1.Location = new System.Drawing.Point(816);
130InBlock.gif            this.trans1.Name = "trans1";
131InBlock.gif            this.trans1.Size = new System.Drawing.Size(4819);
132InBlock.gif            this.trans1.TabIndex = 0;
133InBlock.gif            this.trans1.Text = "true";
134InBlock.gif            // 
135InBlock.gif            // label3
136InBlock.gif            // 
137InBlock.gif            this.label3.Location = new System.Drawing.Point(888);
138InBlock.gif            this.label3.Name = "label3";
139InBlock.gif            this.label3.Size = new System.Drawing.Size(8823);
140InBlock.gif            this.label3.TabIndex = 4;
141InBlock.gif            this.label3.Text = "Transactional";
142InBlock.gif            // 
143InBlock.gif            // txtpath
144InBlock.gif            // 
145InBlock.gif            this.txtpath.Location = new System.Drawing.Point(7248);
146InBlock.gif            this.txtpath.Name = "txtpath";
147InBlock.gif            this.txtpath.Size = new System.Drawing.Size(17621);
148InBlock.gif            this.txtpath.TabIndex = 3;
149InBlock.gif            this.txtpath.Text = "Private$\\test";
150InBlock.gif            // 
151InBlock.gif            // label2
152InBlock.gif            // 
153InBlock.gif            this.label2.Location = new System.Drawing.Point(848);
154InBlock.gif            this.label2.Name = "label2";
155InBlock.gif            this.label2.Size = new System.Drawing.Size(4823);
156InBlock.gif            this.label2.TabIndex = 2;
157InBlock.gif            this.label2.Text = "Path";
158InBlock.gif            // 
159InBlock.gif            // txthost
160InBlock.gif            // 
161InBlock.gif            this.txthost.Location = new System.Drawing.Point(7224);
162InBlock.gif            this.txthost.Name = "txthost";
163InBlock.gif            this.txthost.Size = new System.Drawing.Size(17621);
164InBlock.gif            this.txthost.TabIndex = 1;
165InBlock.gif            this.txthost.Text = "127.0.0.1";
166InBlock.gif            // 
167InBlock.gif            // label1
168InBlock.gif            // 
169InBlock.gif            this.label1.Location = new System.Drawing.Point(824);
170InBlock.gif            this.label1.Name = "label1";
171InBlock.gif            this.label1.Size = new System.Drawing.Size(4823);
172InBlock.gif            this.label1.TabIndex = 0;
173InBlock.gif            this.label1.Text = "Host";
174InBlock.gif            // 
175InBlock.gif            // groupBox3
176InBlock.gif            // 
177InBlock.gif            this.groupBox3.Controls.Add(this.button2);
178InBlock.gif            this.groupBox3.Controls.Add(this.txtrece);
179InBlock.gif            this.groupBox3.Controls.Add(this.label5);
180InBlock.gif            this.groupBox3.Controls.Add(this.button1);
181InBlock.gif            this.groupBox3.Controls.Add(this.txtmsg);
182InBlock.gif            this.groupBox3.Controls.Add(this.label4);
183InBlock.gif            this.groupBox3.Location = new System.Drawing.Point(8136);
184InBlock.gif            this.groupBox3.Name = "groupBox3";
185InBlock.gif            this.groupBox3.Size = new System.Drawing.Size(352176);
186InBlock.gif            this.groupBox3.TabIndex = 1;
187InBlock.gif            this.groupBox3.TabStop = false;
188InBlock.gif            this.groupBox3.Text = "MSMQ Test";
189InBlock.gif            // 
190InBlock.gif            // button2
191InBlock.gif            // 
192InBlock.gif            this.button2.Location = new System.Drawing.Point(28056);
193InBlock.gif            this.button2.Name = "button2";
194InBlock.gif            this.button2.Size = new System.Drawing.Size(5623);
195InBlock.gif            this.button2.TabIndex = 6;
196InBlock.gif            this.button2.Text = "&Receive";
197InBlock.gif            this.button2.Click += new System.EventHandler(this.button2_Click);
198InBlock.gif            // 
199InBlock.gif            // txtrece
200InBlock.gif            // 
201InBlock.gif            this.txtrece.Location = new System.Drawing.Point(878);
202InBlock.gif            this.txtrece.Multiline = true;
203InBlock.gif            this.txtrece.Name = "txtrece";
204InBlock.gif            this.txtrece.Size = new System.Drawing.Size(33690);
205InBlock.gif            this.txtrece.TabIndex = 5;
206InBlock.gif            this.txtrece.Text = "Hello World ";
207InBlock.gif            // 
208InBlock.gif            // label5
209InBlock.gif            // 
210InBlock.gif            this.label5.Location = new System.Drawing.Point(856);
211InBlock.gif            this.label5.Name = "label5";
212InBlock.gif            this.label5.Size = new System.Drawing.Size(11223);
213InBlock.gif            this.label5.TabIndex = 4;
214InBlock.gif            this.label5.Text = "Receive Message";
215InBlock.gif            // 
216InBlock.gif            // button1
217InBlock.gif            // 
218InBlock.gif            this.button1.Location = new System.Drawing.Point(28024);
219InBlock.gif            this.button1.Name = "button1";
220InBlock.gif            this.button1.Size = new System.Drawing.Size(5623);
221InBlock.gif            this.button1.TabIndex = 3;
222InBlock.gif            this.button1.Text = "&Send";
223InBlock.gif            this.button1.Click += new System.EventHandler(this.button1_Click);
224InBlock.gif            // 
225InBlock.gif            // txtmsg
226InBlock.gif            // 
227InBlock.gif            this.txtmsg.Location = new System.Drawing.Point(10424);
228InBlock.gif            this.txtmsg.Name = "txtmsg";
229InBlock.gif            this.txtmsg.Size = new System.Drawing.Size(16821);
230InBlock.gif            this.txtmsg.TabIndex = 2;
231InBlock.gif            this.txtmsg.Text = "Hello World ";
232InBlock.gif            // 
233InBlock.gif            // label4
234InBlock.gif            // 
235InBlock.gif            this.label4.Location = new System.Drawing.Point(824);
236InBlock.gif            this.label4.Name = "label4";
237InBlock.gif            this.label4.Size = new System.Drawing.Size(9623);
238InBlock.gif            this.label4.TabIndex = 0;
239InBlock.gif            this.label4.Text = "Send Message";
240InBlock.gif            // 
241InBlock.gif            // Form2
242InBlock.gif            // 
243InBlock.gif            this.AutoScaleBaseSize = new System.Drawing.Size(614);
244InBlock.gif            this.ClientSize = new System.Drawing.Size(368318);
245InBlock.gif            this.Controls.Add(this.groupBox3);
246InBlock.gif            this.Controls.Add(this.groupBox1);
247InBlock.gif            this.Name = "Form2";
248InBlock.gif            this.Text = "Form2";
249InBlock.gif            this.Load += new System.EventHandler(this.Form2_Load);
250InBlock.gif            this.groupBox1.ResumeLayout(false);
251InBlock.gif            this.groupBox2.ResumeLayout(false);
252InBlock.gif            this.groupBox3.ResumeLayout(false);
253InBlock.gif            this.ResumeLayout(false);
254InBlock.gif
255ExpandedSubBlockEnd.gif        }

256ExpandedSubBlockEnd.gif        #endregion

257InBlock.gif
258InBlock.gif        MSMQ.MSMQQueueInfoClass mqinfor=new MSMQQueueInfoClass();
259InBlock.gif        private void Form2_Load(object sender, System.EventArgs e)
260ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
261InBlock.gif            
262InBlock.gif            
263ExpandedSubBlockEnd.gif        }

264InBlock.gif
265InBlock.gif        private void button1_Click(object sender, System.EventArgs e)
266ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
267InBlock.gif            mqinfor.FormatName="Direct=TCP:" + this.txthost.Text + "\\" + this.txtpath.Text; 
268InBlock.gif            MSMQ.MSMQQueue mq=mqinfor.Open(MSMQ.MQACCESS.MQ_SEND_ACCESS.GetHashCode(),MSMQ.MQSHARE.MQ_DENY_NONE.GetHashCode());
269InBlock.gif            MSMQ.MSMQMessageClass msg=new MSMQMessageClass();
270InBlock.gif            msg.Label="activeX Test";
271InBlock.gif            msg.Body=this.txtmsg.Text;
272InBlock.gif            
273InBlock.gif            MSMQ.MSMQTransactionClass MQ_NO_TRANSACTION=new MSMQTransactionClass();
274InBlock.gif            
275InBlock.gif            object a=MQ_NO_TRANSACTION;
276InBlock.gif            msg.Send(mq,ref a);
277InBlock.gif            
278InBlock.gif
279ExpandedSubBlockEnd.gif        }

280InBlock.gif
281InBlock.gif        private void button2_Click(object sender, System.EventArgs e)
282ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
283InBlock.gif            try
284ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
285InBlock.gif                mqinfor.FormatName="Direct=TCP:" + this.txthost.Text + "\\" + this.txtpath.Text; 
286InBlock.gif            
287InBlock.gif                MSMQ.MSMQQueue mq=mqinfor.Open(MSMQ.MQACCESS.MQ_RECEIVE_ACCESS.GetHashCode(),MSMQ.MQSHARE.MQ_DENY_NONE.GetHashCode());
288InBlock.gif            
289InBlock.gif                MSMQ.MSMQTransactionClass MQ_NO_TRANSACTION=new MSMQTransactionClass();
290InBlock.gif                object a=MQ_NO_TRANSACTION;
291InBlock.gif                bool WantDestinationQueue=false;
292InBlock.gif                object b=WantDestinationQueue;
293InBlock.gif                bool WantBody =true;
294InBlock.gif                object c=WantBody;
295InBlock.gif                long ReceiveTimeout =100;
296InBlock.gif                object d=ReceiveTimeout;
297InBlock.gif                bool WantConnectorType=false;
298InBlock.gif                object e1=WantConnectorType;
299InBlock.gif                MSMQ.MSMQMessage msg=mq.Receive(ref a,ref b,ref c,ref d,ref e1);
300InBlock.gif                this.txtrece.Text=DateTime.Now.ToString() + " " + msg.Body.ToString();
301ExpandedSubBlockEnd.gif            }

302InBlock.gif            catch(Exception e2)
303ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
304InBlock.gif                MessageBox.Show(e2.Message);
305ExpandedSubBlockEnd.gif            }

306ExpandedSubBlockEnd.gif        }

307ExpandedSubBlockEnd.gif    }

308ExpandedBlockEnd.gif}

309None.gif

转载于:https://www.cnblogs.com/neozhu/archive/2005/11/06/269889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值