通过调用windows\system32\mqoa.dll 这个Message Queuing ActiveX Interface API函数就可以做到;
下面是一个简单的例子供参考;
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using MSMQ;
7
namespace MyTest
8![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
9![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
10
/// Form2 的摘要说明。
11
/// </summary>
12
public class Form2 : System.Windows.Forms.Form
13![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Forms#region Forms
15
private System.Windows.Forms.GroupBox groupBox1;
16
private System.Windows.Forms.Label label1;
17
private System.Windows.Forms.Label label2;
18
private System.Windows.Forms.Label label3;
19
private System.Windows.Forms.GroupBox groupBox2;
20
private System.Windows.Forms.TextBox txthost;
21
private System.Windows.Forms.TextBox txtpath;
22
private System.Windows.Forms.RadioButton trans1;
23
private System.Windows.Forms.RadioButton trans2;
24
private System.Windows.Forms.GroupBox groupBox3;
25
private System.Windows.Forms.Label label4;
26
private System.Windows.Forms.Button button1;
27
private System.Windows.Forms.Label label5;
28
private System.Windows.Forms.Button button2;
29
private System.Windows.Forms.TextBox txtmsg;
30
private System.Windows.Forms.TextBox txtrece;
31![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
32
/// 必需的设计器变量。
33
/// </summary>
34
private System.ComponentModel.Container components = null;
35![](/Images/OutliningIndicators/InBlock.gif)
36
public Form2()
37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
//
39
// Windows 窗体设计器支持所必需的
40
//
41
InitializeComponent();
42![](/Images/OutliningIndicators/InBlock.gif)
43
//
44
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
45
//
46
}
47![](/Images/OutliningIndicators/InBlock.gif)
48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
49
/// 清理所有正在使用的资源。
50
/// </summary>
51
protected override void Dispose( bool disposing )
52![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
if( disposing )
54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
if(components != null)
56![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
components.Dispose();
58
}
59
}
60
base.Dispose( disposing );
61
}
62
#endregion
63![](/Images/OutliningIndicators/InBlock.gif)
64![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
65![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
66
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
67
/// 此方法的内容。
68
/// </summary>
69
private void InitializeComponent()
70![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
this.groupBox1 = new System.Windows.Forms.GroupBox();
72
this.groupBox2 = new System.Windows.Forms.GroupBox();
73
this.trans2 = new System.Windows.Forms.RadioButton();
74
this.trans1 = new System.Windows.Forms.RadioButton();
75
this.label3 = new System.Windows.Forms.Label();
76
this.txtpath = new System.Windows.Forms.TextBox();
77
this.label2 = new System.Windows.Forms.Label();
78
this.txthost = new System.Windows.Forms.TextBox();
79
this.label1 = new System.Windows.Forms.Label();
80
this.groupBox3 = new System.Windows.Forms.GroupBox();
81
this.button2 = new System.Windows.Forms.Button();
82
this.txtrece = new System.Windows.Forms.TextBox();
83
this.label5 = new System.Windows.Forms.Label();
84
this.button1 = new System.Windows.Forms.Button();
85
this.txtmsg = new System.Windows.Forms.TextBox();
86
this.label4 = new System.Windows.Forms.Label();
87
this.groupBox1.SuspendLayout();
88
this.groupBox2.SuspendLayout();
89
this.groupBox3.SuspendLayout();
90
this.SuspendLayout();
91
//
92
// groupBox1
93
//
94
this.groupBox1.Controls.Add(this.groupBox2);
95
this.groupBox1.Controls.Add(this.label3);
96
this.groupBox1.Controls.Add(this.txtpath);
97
this.groupBox1.Controls.Add(this.label2);
98
this.groupBox1.Controls.Add(this.txthost);
99
this.groupBox1.Controls.Add(this.label1);
100
this.groupBox1.Location = new System.Drawing.Point(8, 8);
101
this.groupBox1.Name = "groupBox1";
102
this.groupBox1.Size = new System.Drawing.Size(352, 120);
103
this.groupBox1.TabIndex = 0;
104
this.groupBox1.TabStop = false;
105
this.groupBox1.Text = "MSMQ Configuration";
106
//
107
// groupBox2
108
//
109
this.groupBox2.Controls.Add(this.trans2);
110
this.groupBox2.Controls.Add(this.trans1);
111
this.groupBox2.Location = new System.Drawing.Point(104, 72);
112
this.groupBox2.Name = "groupBox2";
113
this.groupBox2.Size = new System.Drawing.Size(144, 40);
114
this.groupBox2.TabIndex = 5;
115
this.groupBox2.TabStop = false;
116
//
117
// trans2
118
//
119
this.trans2.Checked = true;
120
this.trans2.Location = new System.Drawing.Point(72, 16);
121
this.trans2.Name = "trans2";
122
this.trans2.Size = new System.Drawing.Size(64, 19);
123
this.trans2.TabIndex = 1;
124
this.trans2.TabStop = true;
125
this.trans2.Text = "true";
126
//
127
// trans1
128
//
129
this.trans1.Location = new System.Drawing.Point(8, 16);
130
this.trans1.Name = "trans1";
131
this.trans1.Size = new System.Drawing.Size(48, 19);
132
this.trans1.TabIndex = 0;
133
this.trans1.Text = "true";
134
//
135
// label3
136
//
137
this.label3.Location = new System.Drawing.Point(8, 88);
138
this.label3.Name = "label3";
139
this.label3.Size = new System.Drawing.Size(88, 23);
140
this.label3.TabIndex = 4;
141
this.label3.Text = "Transactional";
142
//
143
// txtpath
144
//
145
this.txtpath.Location = new System.Drawing.Point(72, 48);
146
this.txtpath.Name = "txtpath";
147
this.txtpath.Size = new System.Drawing.Size(176, 21);
148
this.txtpath.TabIndex = 3;
149
this.txtpath.Text = "Private$\\test";
150
//
151
// label2
152
//
153
this.label2.Location = new System.Drawing.Point(8, 48);
154
this.label2.Name = "label2";
155
this.label2.Size = new System.Drawing.Size(48, 23);
156
this.label2.TabIndex = 2;
157
this.label2.Text = "Path";
158
//
159
// txthost
160
//
161
this.txthost.Location = new System.Drawing.Point(72, 24);
162
this.txthost.Name = "txthost";
163
this.txthost.Size = new System.Drawing.Size(176, 21);
164
this.txthost.TabIndex = 1;
165
this.txthost.Text = "127.0.0.1";
166
//
167
// label1
168
//
169
this.label1.Location = new System.Drawing.Point(8, 24);
170
this.label1.Name = "label1";
171
this.label1.Size = new System.Drawing.Size(48, 23);
172
this.label1.TabIndex = 0;
173
this.label1.Text = "Host";
174
//
175
// groupBox3
176
//
177
this.groupBox3.Controls.Add(this.button2);
178
this.groupBox3.Controls.Add(this.txtrece);
179
this.groupBox3.Controls.Add(this.label5);
180
this.groupBox3.Controls.Add(this.button1);
181
this.groupBox3.Controls.Add(this.txtmsg);
182
this.groupBox3.Controls.Add(this.label4);
183
this.groupBox3.Location = new System.Drawing.Point(8, 136);
184
this.groupBox3.Name = "groupBox3";
185
this.groupBox3.Size = new System.Drawing.Size(352, 176);
186
this.groupBox3.TabIndex = 1;
187
this.groupBox3.TabStop = false;
188
this.groupBox3.Text = "MSMQ Test";
189
//
190
// button2
191
//
192
this.button2.Location = new System.Drawing.Point(280, 56);
193
this.button2.Name = "button2";
194
this.button2.Size = new System.Drawing.Size(56, 23);
195
this.button2.TabIndex = 6;
196
this.button2.Text = "&Receive";
197
this.button2.Click += new System.EventHandler(this.button2_Click);
198
//
199
// txtrece
200
//
201
this.txtrece.Location = new System.Drawing.Point(8, 78);
202
this.txtrece.Multiline = true;
203
this.txtrece.Name = "txtrece";
204
this.txtrece.Size = new System.Drawing.Size(336, 90);
205
this.txtrece.TabIndex = 5;
206
this.txtrece.Text = "Hello World ";
207
//
208
// label5
209
//
210
this.label5.Location = new System.Drawing.Point(8, 56);
211
this.label5.Name = "label5";
212
this.label5.Size = new System.Drawing.Size(112, 23);
213
this.label5.TabIndex = 4;
214
this.label5.Text = "Receive Message";
215
//
216
// button1
217
//
218
this.button1.Location = new System.Drawing.Point(280, 24);
219
this.button1.Name = "button1";
220
this.button1.Size = new System.Drawing.Size(56, 23);
221
this.button1.TabIndex = 3;
222
this.button1.Text = "&Send";
223
this.button1.Click += new System.EventHandler(this.button1_Click);
224
//
225
// txtmsg
226
//
227
this.txtmsg.Location = new System.Drawing.Point(104, 24);
228
this.txtmsg.Name = "txtmsg";
229
this.txtmsg.Size = new System.Drawing.Size(168, 21);
230
this.txtmsg.TabIndex = 2;
231
this.txtmsg.Text = "Hello World ";
232
//
233
// label4
234
//
235
this.label4.Location = new System.Drawing.Point(8, 24);
236
this.label4.Name = "label4";
237
this.label4.Size = new System.Drawing.Size(96, 23);
238
this.label4.TabIndex = 0;
239
this.label4.Text = "Send Message";
240
//
241
// Form2
242
//
243
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
244
this.ClientSize = new System.Drawing.Size(368, 318);
245
this.Controls.Add(this.groupBox3);
246
this.Controls.Add(this.groupBox1);
247
this.Name = "Form2";
248
this.Text = "Form2";
249
this.Load += new System.EventHandler(this.Form2_Load);
250
this.groupBox1.ResumeLayout(false);
251
this.groupBox2.ResumeLayout(false);
252
this.groupBox3.ResumeLayout(false);
253
this.ResumeLayout(false);
254![](/Images/OutliningIndicators/InBlock.gif)
255
}
256
#endregion
257![](/Images/OutliningIndicators/InBlock.gif)
258
MSMQ.MSMQQueueInfoClass mqinfor=new MSMQQueueInfoClass();
259
private void Form2_Load(object sender, System.EventArgs e)
260![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
261
262
263
}
264![](/Images/OutliningIndicators/InBlock.gif)
265
private void button1_Click(object sender, System.EventArgs e)
266![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
267
mqinfor.FormatName="Direct=TCP:" + this.txthost.Text + "\\" + this.txtpath.Text;
268
MSMQ.MSMQQueue mq=mqinfor.Open(MSMQ.MQACCESS.MQ_SEND_ACCESS.GetHashCode(),MSMQ.MQSHARE.MQ_DENY_NONE.GetHashCode());
269
MSMQ.MSMQMessageClass msg=new MSMQMessageClass();
270
msg.Label="activeX Test";
271
msg.Body=this.txtmsg.Text;
272
273
MSMQ.MSMQTransactionClass MQ_NO_TRANSACTION=new MSMQTransactionClass();
274
275
object a=MQ_NO_TRANSACTION;
276
msg.Send(mq,ref a);
277
278![](/Images/OutliningIndicators/InBlock.gif)
279
}
280![](/Images/OutliningIndicators/InBlock.gif)
281
private void button2_Click(object sender, System.EventArgs e)
282![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
283
try
284![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
285
mqinfor.FormatName="Direct=TCP:" + this.txthost.Text + "\\" + this.txtpath.Text;
286
287
MSMQ.MSMQQueue mq=mqinfor.Open(MSMQ.MQACCESS.MQ_RECEIVE_ACCESS.GetHashCode(),MSMQ.MQSHARE.MQ_DENY_NONE.GetHashCode());
288
289
MSMQ.MSMQTransactionClass MQ_NO_TRANSACTION=new MSMQTransactionClass();
290
object a=MQ_NO_TRANSACTION;
291
bool WantDestinationQueue=false;
292
object b=WantDestinationQueue;
293
bool WantBody =true;
294
object c=WantBody;
295
long ReceiveTimeout =100;
296
object d=ReceiveTimeout;
297
bool WantConnectorType=false;
298
object e1=WantConnectorType;
299
MSMQ.MSMQMessage msg=mq.Receive(ref a,ref b,ref c,ref d,ref e1);
300
this.txtrece.Text=DateTime.Now.ToString() + " " + msg.Body.ToString();
301
}
302
catch(Exception e2)
303![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
304
MessageBox.Show(e2.Message);
305
}
306
}
307
}
308
}
309![](/Images/OutliningIndicators/None.gif)