Writing a Message Queuing COM Application using C++
The following list describes what is needed to start writing a sending or receiving application using COM components. Note that not all the possible tasks that can be implemented by your application are covered here. However, this topic does provide a basic understanding of what DLLs, objects, and methods are used to perform the tasks that most sending and receiving applications will need to perform.
- Make sure that you know how Message Queuing is deployed. For example: are the queues you are sending messages to or reading messages from local or remote, is your application going to have access to the directory service at all times, is Message Queuing deployed across different enterprises, is Message Queuing deployed in a mixed mode environment.
- To use smart pointers, Mqoa.dll must be imported to your application. All the C++ COM code examples provided by the Message Queuing SDK assume that the application imports this DLL using the #import directive and specifies the MSMQ namespace.
#import "mqoa.dll" using namespace MSMQ;
Before using any smart pointer, your application must call CoInitialize or CoInitializeEx to initialize the COM library. After the COM library is no longer needed, your application must call CoUnitialize. - To create a queue, create a smart pointer to the MSMQQueueInfo interface.
IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");
- To send a message to a single destination queue, create smart pointers to the MSMQQueueInfo and MSMQMessage interfaces and declare a smart pointer to the MSMQQueue interface.
IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo"); // Destination queue IMSMQQueuePtr pQueue; // Instance of the opened queue. IMSMQMessagePtr pMsg("MSMQ.MSMQMessage"); // Message to be sent
- To open a queue, create a smart pointer to the MSMQQueueInfo interface and declare a smart pointer to the MSMQQueue interface.
IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo"); // Destination queue IMSMQQueuePtr pQueue; // Instance of the opened queue
- To read a message from a queue, create smart pointers to the MSMQQueueInfo and MSMQMessage interfaces and declare a smart pointer to the MSMQQueue interface.
IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo"); // Destination queue IMSMQQueuePtr pQueue; // Instance of the opened queue. IMSMQMessagePtr pMsg("MSMQ.MSMQMessage"); // Message to be read