C++ COM Code Example: Opening a Queue
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
This example presents a private function that can be called to open a local or remote application queue. To call this function, the caller must provide the path name of the queue, the access mode of the queue, and the share mode of the queue. The function returns a smart pointer to the MSMQQueue interface of the opened queue.
Note
Remote queues can be opened to send messages while offline. To do this, your application must either obtain and cache the format name of the remote queues while online, or have the information needed to construct a direct format name for the queue. This example creates a direct format name from the path name provided by the caller.
The initial properties of the opened queue are based on the properties of the MSMQQueueInfo object used to open the queue. You can always obtain these initial settings, even if the open queue's properties are changed, by looking at the queue's MSMQQueue.QueueInfo property.
This example uses smart pointers to the following Message Queuing interfaces.
MSMQQueueInfo: Represents the queue.
MSMQQueue: Represents the opened queue. In this example, it must be declared by the caller and passed to the function by reference.
MSMQMessage: Represents the message.
To use smart pointers, your application must import Mqoa.dll. You can import this DLL using the #import directive and specify 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. For more information, see Using Message Queuing COM Components in Visual C++ and C.
The following code example contains no version-specific Message Queuing calls.
To open a queue
- Create a buffer for the format name.
Note
wcslen properly handles only null-terminated strings. This code example does not verify that the string passed to it is null-terminated. It is the responsibility of the caller to ensure that the string passed is null-terminated.
Create the direct format name of the queue and set it in the MSMQQueueInfo object.
Call MSMQQueueInfo.Open to open the queue and pass the smart pointer returned back to the caller by reference.
Code Example
The following example code contains no version-specific Message Queuing calls.
HRESULT OpenMyQueue(
WCHAR *wszPathName,
DWORD dwAccessMode,
DWORD dwShareMode,
IMSMQQueuePtr& pQueue
)
{
// Validate the input string.
if (wszPathName == NULL)
{
return MQ_ERROR_INVALID_PARAMETER;
}
DWORD dwFormatNameLength = 0;
WCHAR *wszFormatName = NULL;
dwFormatNameLength = wcslen(wszPathName) + 11;
wszFormatName = new WCHAR[dwFormatNameLength];
try
{
IMSMQQueueInfoPtr pInfo(L"MSMQ.MSMQQueueInfo");
// Create and set the format name of the queue.
if (wszFormatName)
{
memset(wszFormatName, 0, dwFormatNameLength*sizeof(WCHAR));
// ************************************
// You must concatenate "DIRECT=OS" and wszPathName into the
// wszFormatName buffer.
// wszFormatName = "DIRECT=OS" + wszPathName
// If the format name is too long for the buffer. return FALSE.
// ************************************
pInfo->FormatName = wszFormatName;
delete [] wszFormatName;
}
// Open the queue and pass the smart pointer returned by reference.
pQueue = pInfo->Open(dwAccessMode, dwShareMode);
}
catch(const _com_error& comerr)
{
wprintf(L"Error Code: 0x%X\nError Description: %s\n",
comerr.Error(), (WCHAR *)comerr.Description() );
return comerr.Error();
}
return S_OK;
}