Introduction
This class wrapper to encapsulate Message Queue.
- Features
- API reference
- Usage in application
Features
- Compatible with class MFC
- Ease and simple to use
- Ease to track error
API Reference
This class agsMSMQ
has features in header file like here :
struct MQmsg{
CString label;
CString msg;
};
class agsMSMQ
{
public:
BOOL closeMSMQ();
MQmsg getMessageQ();
int getTotalMessage();
HRESULT getMSMQmsg();
BOOL PrepapeGetMessage(CString srv,CString path,int modeMSMQ);
BOOL SendPrivateQ(CString serv, CString pathmsg,CString labelmsg,CString msg);
BOOL SendPublicQ(CString serv, CString pathmsg,CString labelmsg,CString msg);
BOOL DeletePrivateQ(CString serv, CString pathmsg);
BOOL DeletePublicQ(CString serv, CString pathmsg);
BOOL CreatePrivateQ(CString serv, CString pathmsg,CString label);
BOOL CreatePublicQ(CString serv, CString pathmsg,CString label);
CString getErrorMsg();
agsMSMQ();
virtual ~agsMSMQ();
private:
void InitialMQMSG();
void InitialPropidBody(CString label,CString msg);
void InitialPropidQ();
CString m_sError;
void InitialMQQueue();
DWORD dwIDprop;
DWORD dwBufferLength;
WCHAR wszBuffer[250];
CString m_sPathQ;
CString m_sMSMQserv;
CString m_sLabelQ;
unsigned short wszPath[256];
unsigned short wszLabel[1024];
DWORD dwDestFormatLength;
WCHAR wszDestFormat[256];
unsigned char pMessage[2048];
DWORD dwAccessMode;
DWORD dwShareMode;
DWORD dwReaction;
int m_nMessage;
MQmsg msgQ;
protected:
MQQUEUEPROPS QProps;
MQMSGPROPS QPropMsg;
MQPROPVARIANT QPropVar[NUMBER_OF_PROPERTIES];
QUEUEPROPID QPropID[NUMBER_OF_PROPERTIES];
MSGPROPID QMsg[NUMBER_OF_PROPERTIES];
HRESULT QStatus[NUMBER_OF_PROPERTIES];
HRESULT hr;
HANDLE hrTrack;
HANDLE hQue;
PSECURITY_DESCRIPTOR pSecurity;
};
Here's explanation of functions :
Usage in Application
This's GUI to implement agsMSMQ
class:
Don't forget to put header file (#include "agsMSMQ.h"
) on the top of your
application. Beside that, you must add library (mqrt.lib
) into your
project like this picture below:
Create public queue:
agsMSMQ mque;
UpdateData();
CWaitCursor wait;
if(!mque.CreatePublicQ(m_sServer,m_sPath,m_sLabel))
{
AfxMessageBox(mque.getErrorMsg());
return;
}
AfxMessageBox("Create Public Queue succesfully");
Create private queue:
agsMSMQ mque;
UpdateData();
CWaitCursor wait;
if(!mque.CreatePrivateQ(m_sServer,m_sPath,m_sLabel))
{
AfxMessageBox(mque.getErrorMsg());
return;
}
AfxMessageBox("Create Private Queue succesfully");
Delete public queue:
agsMSMQ mque;
UpdateData();
CWaitCursor wait;
if(!mque.DeletePublicQ(m_sServer,m_sPath))
{
AfxMessageBox(mque.getErrorMsg());
return;
}
AfxMessageBox("Delete Public Queue succesfully");
Delete private queue:
agsMSMQ mque;
UpdateData();
CWaitCursor wait;
if(!mque.DeletePrivateQ(m_sServer,m_sPath))
{
AfxMessageBox(mque.getErrorMsg());
return;
}
AfxMessageBox("Delete Private Queue succesfully");
Send public queue message:
agsMSMQ mque;
UpdateData();
CWaitCursor wait;
if(!mque.SendPublicQ(m_sServer,m_sPath,m_sMessageLabel,m_sMessage))
{
AfxMessageBox(mque.getErrorMsg());
return;
}
AfxMessageBox("Send Public Message succesfully");
Send private queue message:
agsMSMQ mque;
UpdateData();
CWaitCursor wait;
if(!mque.SendPrivateQ(m_sServer,m_sPath,m_sMessageLabel,m_sMessage))
{
AfxMessageBox(mque.getErrorMsg());
return;
}
AfxMessageBox("Send Public Message succesfully");
Get all public queue message:
agsMSMQ mque;
int current;
MQmsg msg;
HRESULT hr;
UpdateData();
CWaitCursor wait;
m_cMessage.DeleteAllItems();
if(!mque.PrepareGetMessage(m_sServer,m_sPath,0))
{
AfxMessageBox(mque.getErrorMsg());
return;
}
msg = mque.getMessageQ();
current = m_cMessage.InsertItem(0,msg.label);
m_cMessage.SetItemText(current,1, msg.msg);
do
{
hr = mque.getMSMQmsg();
if(hr!=0) break;
msg = mque.getMessageQ();
current = m_cMessage.InsertItem(0,msg.label);
m_cMessage.SetItemText(current,1, msg.msg);
}while(hr==0);
if(!mque.closeMSMQ())
{
AfxMessageBox(mque.getErrorMsg());
return;
}
if(mque.getTotalMessage()==0)
{
AfxMessageBox("No message in Message Queue");
else
{
CString mseg;
mseg.Format("Get %d message(s) from public queue successfully",
mque.getTotalMessage());
AfxMessageBox(mseg);
}
Get all private queue message:
agsMSMQ mque;
int current;
MQmsg msg;
HRESULT hr;
UpdateData();
CWaitCursor wait;
m_cMessage.DeleteAllItems();
if(!mque.PrepareGetMessage(m_sServer,m_sPath,1))
{
AfxMessageBox(mque.getErrorMsg());
return;
}
msg = mque.getMessageQ();
current = m_cMessage.InsertItem(0,msg.label);
m_cMessage.SetItemText(current,1, msg.msg);
do
{
hr = mque.getMSMQmsg();
if(hr!=0) break;
msg = mque.getMessageQ();
current = m_cMessage.InsertItem(0,msg.label);
m_cMessage.SetItemText(current,1, msg.msg);
}while(hr==0);
if(!mque.closeMSMQ())
{
AfxMessageBox(mque.getErrorMsg());
return;
}
if(mque.getTotalMessage()==0)
{
AfxMessageBox("No message in Message Queue");
}else
{
CString mseg;
mseg.Format("Get %d message(s) from private queue successfully",
mque.getTotalMessage());
AfxMessageBox(mseg);
}
Reference
Platform SDK for Message Queue (MSMQ).
History
Version 1.0 First release : November 16, 2001
Version 1.1 Modified on PrepareGetMessage() and getMSMQmsg() function
He gradueted from Sepuluh Nopember Institute of Technology (ITS) in Department of Electrical Engineering, Indonesia. His programming interest is VC++, C#, VB, VB.NET, .NET, VBScript, Delphi, C++ Builder, Assembly,and ASP/ASP.NET. He's founder for PE College(www.pecollege.net), free video tutorial about programming, infrastructure, and computer science. He's currently based in Depok, Indonesia. His blog is http://geeks.netindonesia.net/blogs/agus and http://blog.aguskurniawan.net