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