Partager via


Writing a Message Queuing COM Application using C++

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

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;  
    

    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.

    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.

    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.

    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.

    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.