您的位置:首页 > 移动开发

Writing a Message Queuing COM Application using C++

2009-04-20 14:56 435 查看
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.



Copy Code

#import "mqoa.dll"
using namespace MSMQ;
The #import directive generates two files that contain smart-pointer declarations for all Message Queuing objects: Mqoa.tlh and Mqoa.tli. The .tli file is included by an #include directive in the .tlh file, which is processed by the compiler as if it were named by an #include directive in your application. Do not include the Mqoai.h header file when you import Mqoa.dll.
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.



Copy Code

IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");
For examples of creating queues, see C/C++ COM Code Example: Creating a Queue and C/C++ COM Code Example: Creating a Transactional Queue.

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.



Copy Code

IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");  // Destination queue
IMSMQQueuePtr pQueue;                           // Instance of the opened queue.
IMSMQMessagePtr pMsg("MSMQ.MSMQMessage");       // Message to be sent
For an example of sending a message to a specific queue, see C/C++ COM Code Example: Sending Messages to a Destination Queue.

To open a queue, create a smart pointer to the MSMQQueueInfo interface and declare a smart pointer to the MSMQQueue interface.



Copy Code

IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");  // Destination queue
IMSMQQueuePtr pQueue;                           // Instance of the opened queue
For an example of opening and closing a queue, see C++ COM Code Example: Opening a 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.



Copy Code

IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");  // Destination queue
IMSMQQueuePtr pQueue;                           // Instance of the opened queue.
IMSMQMessagePtr pMsg("MSMQ.MSMQMessage"); // Message to be read
For an example of synchronously reading message in a queue, see C/C++ COM Code Example: Reading Messages Synchronously.

Send comments about this topic to Microsoft.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: