Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / MFC
Article

agsMSMQ v1.1 - A Message Queue Implementation

Rate me:
Please Sign up or sign in to vote.
4.80/5 (5 votes)
19 Jan 20022 min read 130K   3.5K   39   24
Class wrapper for Message Queue (MSMQ)

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 :

  • BOOL CreatePublicQ(CString serv, CString pathmsg,CString label)<br>
        <br>
    Use to create public queue on server queue serv with name of queue pathmsg and label queue label
  • BOOL CreatePrivateQ(CString serv, CString pathmsg,CString label)<br>
        <br>
    Use to create private queue on server queue serv with name of queue pathmsg and label queue label.
  • BOOL SendPublicQ(CString serv, CString pathmsg,CString labelmsg,CString
        msg);<br>
        <br>
    Use to send public message queue  on serv queue server, pathmsg path queue with message label labelmsg and content message msg.
  • BOOL SendPrivateQ(CString serv, CString pathmsg,CString labelmsg,CString
        msg);<br>
        <br>
    Use to send private message queue  on serv queue server, pathmsg path queue with message label labelmsg and content message msg.
  • BOOL PrepapeGetMessage(CString srv,CString path,int modeMSMQ);<br>
        <br>
    Prepare to get message queue on serv with path queue path, modeMSMQ parameter identify type of queue. if value of modeMSMQ parameter is 0 then it's public queue. Otherwise, if value of modeMSMQ is 1, then it's private queue.
  • MQmsg getMessageQ(void)

    Get data queue message. MQmsg is a structure :

    struct MQmsg{
       CString label;
       CString msg;
    };
    
  • int getTotalMessage(void)<br>
        <br>
    Get number of messages.
  • HRESULT getMSMQmsg(void)<br>
        <br>
    Use to check error or not while it's getting queue message. 
  • BOOL closeMSMQ(void)<br>
        <br>
    Use to close queue server.
  • CString getErrorMsg(void)<br>
        <br>
    Use to get error message.

Usage in Application

This's GUI to implement agsMSMQ class:

Image 1

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:

Image 2

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder PE College
Indonesia Indonesia
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

Comments and Discussions

 
QuestionChanged Pointer in MSMQ?? Pin
gracialLee14-Jun-04 1:22
gracialLee14-Jun-04 1:22 
GeneralTransactional receives from remote PC's in MSMQ 1.0 Pin
Cris Brockwell11-Jun-04 5:07
Cris Brockwell11-Jun-04 5:07 
GeneralTalking to myself Pin
Chris Bee14-Jun-04 1:16
sussChris Bee14-Jun-04 1:16 
GeneralLocating queues Pin
Cris Brockwell11-Jun-04 4:53
Cris Brockwell11-Jun-04 4:53 
Questioncan agsMSMQ be used in win2003 server? Pin
joyli28-Dec-03 4:51
joyli28-Dec-03 4:51 
GeneralMSMQ on NT Service level Pin
Moch. Taufik11-Nov-03 16:21
Moch. Taufik11-Nov-03 16:21 
GeneralRe: MSMQ on NT Service level Pin
Agus Kurniawan12-Nov-03 15:23
Agus Kurniawan12-Nov-03 15:23 
QuestionCan u give me a clue to open a private queue? Pin
Allen Chen1-Jul-03 17:49
Allen Chen1-Jul-03 17:49 
AnswerRe: Can u give me a clue to open a private queue? Pin
Chris Bee14-Jun-04 0:12
sussChris Bee14-Jun-04 0:12 
QuestionWhy i can't send to or recv from different machine ? Pin
silentsky8-Dec-02 20:00
silentsky8-Dec-02 20:00 
AnswerRe: Why i can't send to or recv from different machine ? Pin
MarkusAvall14-Jan-03 0:03
MarkusAvall14-Jan-03 0:03 
GeneralRe: Why i can't send to or recv from different machine ? Pin
Anand Paranjpe18-Sep-03 1:53
Anand Paranjpe18-Sep-03 1:53 
GeneralRe: Why i can't send to or recv from different machine ? Pin
andja2608-Aug-07 20:57
andja2608-Aug-07 20:57 
GeneralPlease, show me how it works Pin
TaeKKen-V8-Nov-02 4:00
TaeKKen-V8-Nov-02 4:00 
GeneralRe: Please, show me how it works Pin
Agus Kurniawan11-Nov-02 14:48
Agus Kurniawan11-Nov-02 14:48 
QuestionRe: Please, show me how it works Pin
joseacl7924-Nov-06 7:07
joseacl7924-Nov-06 7:07 
GeneralTransaction Queue Pin
mbr_denmark18-Oct-02 3:40
mbr_denmark18-Oct-02 3:40 
GeneralIBM MQ Pin
12-Mar-02 9:39
suss12-Mar-02 9:39 
GeneralRe: IBM MQ Pin
C-J Berg14-Sep-02 7:21
C-J Berg14-Sep-02 7:21 
GeneralMQRT.DLL Pin
Ron Heller21-Jan-02 21:19
Ron Heller21-Jan-02 21:19 
GeneralRe: MQRT.DLL Pin
Agus Kurniawan21-Jan-02 22:13
Agus Kurniawan21-Jan-02 22:13 
QuestionAdd an event listener? Pin
philip andrew20-Jan-02 17:17
philip andrew20-Jan-02 17:17 
GeneralBug! Pin
Victor Vogelpoel15-Jan-02 3:03
Victor Vogelpoel15-Jan-02 3:03 
GeneralRe: Bug! Pin
Agus Kurniawan20-Jan-02 17:27
Agus Kurniawan20-Jan-02 17:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.