MSMQ学习笔记二——创建Message Queue队列
一、创建Message Queue队列的主要流程
1、定义MQQUEUEPROPS 结构;
2、设置消息队列属性;
3、初始化MQQUEUEPROPS 结构;
4、调用MQCreateQueue创建队列。
下面对MSDN上的创建Message Queue队列示例函数:
HRESULT CreateMSMQQueue(
LPWSTR wszPathName,
PSECURITY_DESCRIPTOR pSecurityDescriptor,
LPWSTR wszOutFormatName,
DWORD *pdwOutFormatNameLength
)
{
// Define the maximum number of queue properties.
const int NUMBEROFPROPERTIES = ;
const int BUFLEN = ; // Define a queue property structure and the structures needed to initialize it.
MQQUEUEPROPS QueueProps;
MQPROPVARIANT aQueuePropVar[NUMBEROFPROPERTIES];
QUEUEPROPID aQueuePropId[NUMBEROFPROPERTIES];
HRESULT aQueueStatus[NUMBEROFPROPERTIES];
HRESULT hr = MQ_OK; // Validate the input parameters.
if (wszPathName == NULL || wszOutFormatName == NULL || pdwOutFormatNameLength == NULL)
{
return MQ_ERROR_INVALID_PARAMETER;
} // Set queue properties.
DWORD cPropId = ;
aQueuePropId[cPropId] = PROPID_Q_PATHNAME;
aQueuePropVar[cPropId].vt = VT_LPWSTR;
aQueuePropVar[cPropId].pwszVal = wszPathName;
cPropId++; WCHAR wszLabel[MQ_MAX_Q_LABEL_LEN] = L"Test Queue";
aQueuePropId[cPropId] = PROPID_Q_LABEL;
aQueuePropVar[cPropId].vt = VT_LPWSTR;
aQueuePropVar[cPropId].pwszVal = wszLabel;
cPropId++; // Initialize the MQQUEUEPROPS structure.
QueueProps.cProp = cPropId; // Number of properties
QueueProps.aPropID = aQueuePropId;// IDs of the queue properties
QueueProps.aPropVar = aQueuePropVar;// Values of the queue properties
QueueProps.aStatus = aQueueStatus;// Pointer to the return status // Call MQCreateQueue to create the queue.
WCHAR wszFormatNameBuffer[BUFLEN];
DWORD dwFormatNameBufferLength = BUFLEN;
hr = MQCreateQueue(pSecurityDescriptor, // Security descriptor
&QueueProps, // Address of queue property structure
wszFormatNameBuffer, // Pointer to format name buffer
&dwFormatNameBufferLength);// Pointer to receive the queue's format name length in Unicode characters not bytes. // Return the format name if the queue is created successfully.
if (hr == MQ_OK || hr == MQ_INFORMATION_PROPERTY)
{
if (*pdwOutFormatNameLength >= dwFormatNameBufferLength)
{
wcsncpy_s(wszOutFormatName, *pdwOutFormatNameLength - , wszFormatNameBuffer, _TRUNCATE);
// ************************************
// You must copy wszFormatNameBuffer into the
// wszOutFormatName buffer.
// ************************************
wszOutFormatName[*pdwOutFormatNameLength - ] = L'\0';
*pdwOutFormatNameLength = dwFormatNameBufferLength;
}
else
{
wprintf(L"The queue was created, but its format name cannot be returned.\n");
}
}
return hr;
}
注意:需要包含头文件windows.h、mq.h、stdio.h和lib库mqrt.lib。
二、示例
以下为测试示例:
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t name[]=L".\\PRIVATE$\\ZHXL.121";
DWORD bufferLength = ;
wchar_t formattedQueueName[]; HRESULT returnValue = CreateMSMQQueue(name,NULL,formattedQueueName,&bufferLength); if(returnValue != MQ_OK)
wprintf(L"Creating a Queue failed\n");
else
{
wprintf(L"Queue was successfully created..Formatted QueueName =%s\n",formattedQueueName);
wprintf(L"BufferLength returned is %d\n", bufferLength);
} return ;
}
运行结果为:

三、补充
1、MQQUEUEPROPS结构体
该结构体定义为:
typedef struct tagMQQUEUEPROPS
{
DWORD cProp;
__field_ecount(cProp) QUEUEPROPID* aPropID;
__field_ecount(cProp) MQPROPVARIANT* aPropVar;
__field_ecount_opt(cProp) HRESULT* aStatus;
} MQQUEUEPROPS;
cProp:属性总个数;
aPropID:属性标识符,Message Queue必需属性的标识符有PROPID_Q_PATHNAME和PROPID_Q_LABEL两个,分别为指定队列的路径和描述性标签;
aPropVar:消息队列属性的值;
2、消息队列创建之后一直存在,创建同名消息队列会失败,需要用到指定队列时所需操作为打开制定队列,利用完事后关闭队列。
MSMQ学习笔记二——创建Message Queue队列的更多相关文章
- angular学习笔记(二)-创建angular模块
如果在页面的html标签(或任意标签)中添加ng-app,表示对整个页面应用angular来管理. 他是一个模块. 模块有助于把东西从全局命名空间中隔离. 今天学习如何自定义创建模块: <!DO ...
- MSMQ学习笔记一——概述
一.MSMQ是什么 Message Queuing(MSMQ) 是微软开发的消息中间件,可应用于程序内部或程序之间的异步通信.主要的机制是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为M ...
- InterSystems Ensemble学习笔记(二) Ensemble创建镜像, 实现自动故障转移
系列目录 InterSystems Ensemble学习笔记(一) Ensemble介绍及安装InterSystems Ensemble学习笔记(二) Ensemble创建镜像, 实现自动故障转移 一 ...
- JMX学习笔记(二)-Notification
Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...
- Linux内核学习笔记二——进程
Linux内核学习笔记二——进程 一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器 ...
- 并发编程学习笔记(13)----ConcurrentLinkedQueue(非阻塞队列)和BlockingQueue(阻塞队列)原理
· 在并发编程中,我们有时候会需要使用到线程安全的队列,而在Java中如果我们需要实现队列可以有两种方式,一种是阻塞式队列.另一种是非阻塞式的队列,阻塞式队列采用锁来实现,而非阻塞式队列则是采用cas ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- springmvc学习笔记---idea创建springmvc项目
前言: 真的是很久没搞java的web服务开发了, 最近一次搞还是读研的时候, 想来感慨万千. 英雄没落, Eclipse的盟主地位隐隐然有被IntelliJ IDEA超越的趋势. Spring从2. ...
- [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计
源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...
随机推荐
- Codeforce-A-Two distinct points(暴力)
output standard output You are given two segments [l1;r1][l1;r1] and [l2;r2][l2;r2] on the xx-axis. ...
- 工具类_IsNull
import java.util.List; /** * 判断是否为空 2015-08-17 * * @author lipanquan * */public final class IsNull ...
- Linux系统lvm管理
pv: 物理卷,被pv命令处理过的物理分区vg:物理卷组 被组装到一起的物理卷pe: 物理扩展 lvm设备的最小存储单元 lvm是pe的整数倍lvm:逻辑卷 ...
- MYSQL生成两个日期之间的所有日期数据
set @i = -1; set @sql = repeat(" select 1 union all",-datediff('2021-01-01','2030-12-31')+ ...
- python3 多线程笔记
import threadingimport time #继承 class threading.Threadclass MyThread(threading.Thread): #类做初始化 def _ ...
- vue的watch详细用法
https://www.cnblogs.com/shiningly/p/9471067.html https://www.jb51.net/article/139282.htm
- linux终端没有GUI时python使用matplotlib如何画图
import matplotlib as mpl mpl.use('Agg') #而且必须添加在import matplotlib.pyplot之前,否则无效 ======== ======== == ...
- C# 连接 Exchange 发送邮件
C#连接Exchange 发送邮件代码如下 /// <summary> /// exchange群发邮件 /// </summary> /// <param name=& ...
- 命令行模式运行jmeter,主从方式运行jmeter
jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式: sh jmeter.sh -n -t my-script.jmx -R 10.6.5.31,10.6.5 ...
- 获取tomcat路径
String serverPath = System.getProperty("catalina.home");