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会话超时的机制做心跳接口验证 ...
随机推荐
- 浅谈c语言的线性表的基本操作———基于严蔚敏的数据结构(c语言版)
主要讲的是线性表的创建,插入及删除: 0. 线性表的建立,对于这类操作主要是利用了结构体的性质,对于定义的线性表的特性主要有三点:首先 Typedef struct { ElemType *ele ...
- WPF Canvas转换为位图 (RenderTargetBitmap)
使用 RenderTargetBitmap 的注意事项: 1. 要渲染的Canvas元素要放在Border元素内,并且此Border元素不能设置边框宽度(BorderThickness),不然生成的位 ...
- centos7-根据端口号查看所属应用
[root@cent7-zuoys ~]# netstat -lnp | grep 8080 [root@cent7-zuoys ~]# ps 4735
- Invalid prop: type check failed for prop "XXX". Expected String, got Object.
项目是Vue的,基于elementUI的后台管理系统. Invalid prop: type check failed for prop "total". Expected Str ...
- Mybatis学习笔记11 - 动态sql之trim标签
trim标签体中是整个字符串拼串后的结果.prefix="" 前缀: prefix给拼串后的整个字符串加一个前缀prefixOverrides="" 前缀覆盖: ...
- Android微信开放平台,申请移动应用的 应用签名 如何获取
在微信开放平台,申请移动应用的时候: https://open.weixin.qq.com/cgi-bin/appcreate?t=manage/createMobile&type=app&a ...
- SpringMVC restful风格
1.Spring对REST的支持 Spring3(这里讨论Spring3.2+)对Spring MVC的一些增强功能为REST提供了良好的支持.Spring对开发REST资源提供以下支持: 操作方式: ...
- DEDE文章列表加上序号效果
在文章列表上面加上序号列表的形式,使得文章列表表现得没那么单调,更加丰富一点. {dede:arclist orderby=pubdate type='commend.' titlelen='26' ...
- Unicode汉字编码表以及参考源码分享
1 Unicode编码表 Unicode只有一个字符集,中.日.韩的三种文字占用了Unicode中0x3000到0x9FFF的部分 Unicode目前普遍采用的是UCS-2,它用两个字节来编码一个 ...
- 上传文件,使用FormData进行Ajax请求,jsoncallback跨域
通过传统的form表单提交的方式上传文件: <form id= "uploadForm" action= "http://localhost:8080/cfJAX_ ...