解决msmq接收远程主机私有队列消息的问题!
通过调用windows\system32\mqoa.dll 这个Message Queuing ActiveX Interface API函数就可以做到;
下面是一个简单的例子供参考;


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

{
9
/**//// <summary>
10
/// Form2 的摘要说明。
11
/// </summary>
12
public class Form2 : System.Windows.Forms.Form
13
{
14
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
/**//// <summary>
32
/// 必需的设计器变量。
33
/// </summary>
34
private System.ComponentModel.Container components = null;
35
36
public Form2()
37
{
38
//
39
// Windows 窗体设计器支持所必需的
40
//
41
InitializeComponent();
42
43
//
44
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
45
//
46
}
47
48
/**//// <summary>
49
/// 清理所有正在使用的资源。
50
/// </summary>
51
protected override void Dispose( bool disposing )
52
{
53
if( disposing )
54
{
55
if(components != null)
56
{
57
components.Dispose();
58
}
59
}
60
base.Dispose( disposing );
61
}
62
#endregion
63
64
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
65
/**//// <summary>
66
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
67
/// 此方法的内容。
68
/// </summary>
69
private void InitializeComponent()
70
{
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
255
}
256
#endregion
257
258
MSMQ.MSMQQueueInfoClass mqinfor=new MSMQQueueInfoClass();
259
private void Form2_Load(object sender, System.EventArgs e)
260
{
261
262
263
}
264
265
private void button1_Click(object sender, System.EventArgs e)
266
{
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
279
}
280
281
private void button2_Click(object sender, System.EventArgs e)
282
{
283
try
284
{
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
{
304
MessageBox.Show(e2.Message);
305
}
306
}
307
}
308
}
309
下面是一个简单的例子供参考;


1

2

3

4

5

6

7

8



9


10

11

12

13



14


15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31


32

33

34

35

36

37



38

39

40

41

42

43

44

45

46

47

48


49

50

51

52



53

54



55

56



57

58

59

60

61

62

63

64


65


66

67

68

69

70



71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260



261

262

263

264

265

266



267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282



283

284



285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303



304

305

306

307

308

309

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix