Click here to Skip to main content
11,479,412 members (43,900 online)
Click here to Skip to main content

agsMSMQ v1.1 - A Message Queue Implementation

, 19 Jan 2002 108.5K 3.2K 39
Rate this:
Please Sign up or sign in to vote.
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)
    
        
    
        
    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)
    
        
    
        
    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);
    
        
    
        
    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);
    
        
    
        
    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);
    
        
    
        
    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)
    
        
    
        
    Get number of messages.
  • HRESULT getMSMQmsg(void)
    
        
    
        
    Use to check error or not while it's getting queue message. 
  • BOOL closeMSMQ(void)
    
        
    
        
    Use to close queue server.
  • CString getErrorMsg(void)
    
        
    
        
    Use to get error message.

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

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

Share

About the Author

Agus Kurniawan
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 2:22
membergracialLee14-Jun-04 2:22 
GeneralTransactional receives from remote PC's in MSMQ 1.0 Pin
Chris Bee11-Jun-04 6:07
sussChris Bee11-Jun-04 6:07 
GeneralTalking to myself Pin
Chris Bee14-Jun-04 2:16
sussChris Bee14-Jun-04 2:16 
GeneralLocating queues Pin
Chris Bee11-Jun-04 5:53
sussChris Bee11-Jun-04 5:53 
Questioncan agsMSMQ be used in win2003 server? Pin
my_fasion28-Dec-03 5:51
membermy_fasion28-Dec-03 5:51 
GeneralMSMQ on NT Service level Pin
Moch. Taufik, (IPB)11-Nov-03 17:21
sussMoch. Taufik, (IPB)11-Nov-03 17:21 
GeneralRe: MSMQ on NT Service level Pin
Agus Kurniawan12-Nov-03 16:23
memberAgus Kurniawan12-Nov-03 16:23 
QuestionCan u give me a clue to open a private queue? Pin
Allen Chen1-Jul-03 18:49
memberAllen Chen1-Jul-03 18:49 
AnswerRe: Can u give me a clue to open a private queue? Pin
Chris Bee14-Jun-04 1:12
sussChris Bee14-Jun-04 1:12 
QuestionWhy i can't send to or recv from different machine ? Pin
silentsky8-Dec-02 21:00
membersilentsky8-Dec-02 21:00 
AnswerRe: Why i can't send to or recv from different machine ? Pin
MarkusAvall14-Jan-03 1:03
memberMarkusAvall14-Jan-03 1:03 
GeneralRe: Why i can't send to or recv from different machine ? Pin
anandparanjpe18-Sep-03 2:53
memberanandparanjpe18-Sep-03 2:53 
GeneralRe: Why i can't send to or recv from different machine ? Pin
andja2608-Aug-07 21:57
memberandja2608-Aug-07 21:57 
GeneralPlease, show me how it works Pin
TaeKKen-v8-Nov-02 5:00
memberTaeKKen-v8-Nov-02 5:00 
GeneralRe: Please, show me how it works Pin
Agus Kurniawan11-Nov-02 15:48
memberAgus Kurniawan11-Nov-02 15:48 
QuestionRe: Please, show me how it works Pin
JA15924-Nov-06 8:07
memberJA15924-Nov-06 8:07 
GeneralTransaction Queue Pin
mbr_denmark18-Oct-02 4:40
membermbr_denmark18-Oct-02 4:40 
GeneralIBM MQ Pin
Paul Swanson12-Mar-02 10:39
memberPaul Swanson12-Mar-02 10:39 
GeneralRe: IBM MQ Pin
Carl Berg14-Sep-02 8:21
memberCarl Berg14-Sep-02 8:21 
GeneralMQRT.DLL Pin
Ron Heller21-Jan-02 22:19
memberRon Heller21-Jan-02 22:19 
GeneralRe: MQRT.DLL Pin
Agus Kurniawan21-Jan-02 23:13
memberAgus Kurniawan21-Jan-02 23:13 
QuestionAdd an event listener? Pin
philip andrew20-Jan-02 18:17
memberphilip andrew20-Jan-02 18:17 
GeneralBug! Pin
Victor Vogelpoel15-Jan-02 4:03
memberVictor Vogelpoel15-Jan-02 4:03 
GeneralRe: Bug! Pin
Agus Kurniawan20-Jan-02 18:27
memberAgus Kurniawan20-Jan-02 18:27 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 20 Jan 2002
Article Copyright 2001 by Agus Kurniawan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid