一、创建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队列的更多相关文章

  1. angular学习笔记(二)-创建angular模块

    如果在页面的html标签(或任意标签)中添加ng-app,表示对整个页面应用angular来管理. 他是一个模块. 模块有助于把东西从全局命名空间中隔离. 今天学习如何自定义创建模块: <!DO ...

  2. MSMQ学习笔记一——概述

    一.MSMQ是什么 Message Queuing(MSMQ) 是微软开发的消息中间件,可应用于程序内部或程序之间的异步通信.主要的机制是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为M ...

  3. InterSystems Ensemble学习笔记(二) Ensemble创建镜像, 实现自动故障转移

    系列目录 InterSystems Ensemble学习笔记(一) Ensemble介绍及安装InterSystems Ensemble学习笔记(二) Ensemble创建镜像, 实现自动故障转移 一 ...

  4. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

  5. Linux内核学习笔记二——进程

    Linux内核学习笔记二——进程   一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器 ...

  6. 并发编程学习笔记(13)----ConcurrentLinkedQueue(非阻塞队列)和BlockingQueue(阻塞队列)原理

    · 在并发编程中,我们有时候会需要使用到线程安全的队列,而在Java中如果我们需要实现队列可以有两种方式,一种是阻塞式队列.另一种是非阻塞式的队列,阻塞式队列采用锁来实现,而非阻塞式队列则是采用cas ...

  7. AJax 学习笔记二(onreadystatechange的作用)

    AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...

  8. springmvc学习笔记---idea创建springmvc项目

    前言: 真的是很久没搞java的web服务开发了, 最近一次搞还是读研的时候, 想来感慨万千. 英雄没落, Eclipse的盟主地位隐隐然有被IntelliJ IDEA超越的趋势. Spring从2. ...

  9. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...

随机推荐

  1. chkconfig命令详细介绍

    命令介绍 chkconfig命令用来更新.查询.修改不同运行级上的系统服务.比如安装了httpd服务,并且把启动的脚本放在了/etc/rc.d/init.d目录下,有时候需要开机自动启动它,而有时候则 ...

  2. I2C(smbus pmbus)和SPI分析

    2C和SPI作为两种非常常用的低速外部总线 I2C I2C是以前的飞利浦半导体制定的标准,也就是如今的NXP. I2C总线由一条数据线(SDA)和一条时钟线(SCL)组成.设备分主从,主设备提供时钟, ...

  3. 毕业设计 python opencv实现车牌识别 预处理

    主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...

  4. 修改阿里云ESC Centos 7.4 防火墙开放端口

    例如系统:Centos 7.4操作如下 1,进入 cd /etc/firewalld/zones/ 目录 2,编辑 vim public.xml 3,按i或insert键进入编辑模式 4,在<z ...

  5. 对Vue.js的认知

    一.什么是MVVM? MVVM是Model-View-ViewModel的缩写.MVVM是一种设计思想.Model 层代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑:View 代表UI ...

  6. js实现放大镜效果

    原理: 鼠标在小图片上移动时,通过捕捉鼠标在小图片上的位置,定位大图片的相应位置: 放大镜的移动方向和大图片的移动方向:横向和纵向都是相反,才可以保证同步: 需要元素:大图和小图,存放大图和小图的容器 ...

  7. Apache重定向URL

    (1)去除httpd.conf文件中"#LoadModule rewrite_module modules/mod_rewrite.so"前面的"#"号; (2 ...

  8. PHP jsonencode unicode 存储问题

    首先是这样的,因为输入的字符串的里面有德语的字符,如下: 当我存储到数据库之后,再用json_encode获取到数据库内的这些字符时,出问题了. 直接encode一个字符串"püüäöä&q ...

  9. ctrip-apollo

    云端多网卡问题: 参考:https://blog.csdn.net/buyaore_wo/article/details/79847404

  10. Serializable深入理解

    1.什么是序列化,解决什么问题 序列化可以对象的状态信息转换成可以持久化或者可以传输形式的过程.一般是转为字节数据.而把字节数组还原成原来同等对象的过程成为反序列化. 在Java中,对象的序列化与反序 ...