一个简单的MSMQ的发送和接收类

本文介绍了如何创建一个简单的MSMQ消息发送类以及同步和异步接收类,包括使用WinAPI进行操作的方法和关键属性的设置。
摘要由CSDN通过智能技术生成

1 发送类

#define MAX_FORMATNAME_LEN 2048
class CMyMsmqSend
{
public:
	CMyMsmqSend(WCHAR* wszFormatName);
	~CMyMsmqSend();

	long sendData(WCHAR* szLable, const char* data, int dataSize,unsigned char priority);

private:
	WCHAR _wszFormatName[MAX_FORMATNAME_LEN];
	bool _isOpened;
	QUEUEHANDLE _hQueue;
};


CMyMsmqSend::CMyMsmqSend(WCHAR* wszFormatName)
{
	wcscpy(_wszFormatName,wszFormatName);
	_isOpened = false;
	_hQueue = NULL;
}

CMyMsmqSend::~CMyMsmqSend()
{
	if (_isOpened)
	{
		MQCloseQueue(_hQueue);
	}
}

long CMyMsmqSend::sendData(WCHAR* szLable, const char* data, int dataSize,unsigned char priority)
{
	HRESULT hr = MQ_OK;  
	if(!_isOpened)
	{
		hr = MQOpenQueue(_wszFormatName,MQ_SEND_ACCESS,MQ_DENY_NONE,&_hQueue);
		if (FAILED(hr))
		{	return hr;	}
		_isOpened = true;
	}

	const int NUMBEROFPROPERTIES = 4;                   // Number of properties
	DWORD cPropId = 0;                                  // Property counter
	MQMSGPROPS msgProps;
	MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
	MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
	HRESULT aMsgStatus[NUMBEROFPROPERTIES];

	aMsgPropId[cPropId] = PROPID_M_PRIORITY;                   // Property ID
	aMsgPropVar[cPropId].vt = VT_UI1;                      // Type indicator
	aMsgPropVar[cPropId].bVal = priority;     // The message label
	cPropId++;

	// Specify the message properties to be sent.
	aMsgPropId[cPropId] = PROPID_M_LABEL;                   // Property ID
	aMsgPropVar[cPropId].vt = VT_LPWSTR;                      // Type indicator
	aMsgPropVar[cPropId].pwszVal = (LPWSTR)szLable;     // The message label
	cPropId++;

	aMsgPropId[cPropId] = PROPID_M_BODY;                      // Property ID
	aMsgPropVar[cPropId].vt = VT_VECTOR | VT_UI1;             // Type indicator
	string sBody;
	sBody.append(data,dataSize);
	aMsgPropVar[cPropId].caub.pElems = (LPBYTE)sBody.data();
	aMsgPropVar[cPropId].caub.cElems = dataSize;
	cPropId++;

	DWORD dwBodyType =  VT_VECTOR | VT_UI1; ;
	aMsgPropId[cPropId] = PROPID_M_BODY_TYPE;                  // Property ID
	aMsgPropVar[cPropId].vt = VT_UI4;                          // Type indicator
	aMsgPropVar[cPropId].ulVal = dwBodyType;
	cPropId++;
	// Initialize the MQMSGPROPS structure.
	msgProps.cProp = cPropId;
	msgProps.aPropID = aMsgPropId;
	msgProps.aPropVar = aMsgPropVar;
	msgProps.aStatus = aMsgStatus;

	// Call MQSendMessage to send the message to the queue.
	hr = MQSendMessage(	_hQueue,&msgProps,MQ_NO_TRANSACTION);

	r
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值