C-C++ COM Code Example: Creating 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 provides an application-defined function that creates a public or private queue using Message Queuing COM objects in C++ based on a given queue path name. For creating transactional queues, see C/C++ COM Code Example: Creating a Transactional Queue.
This example uses a smart pointer to the MSMQQueueInfo interface.
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.
Note
Public queues cannot be created if there is no connection to the directory service. This restriction applies to dependent client computers, independent client computers that are working offline, and Message Queuing servers that have routing services enabled (for MSMQ 1.0, these servers are referred to as FRS servers).
The following procedure shows how the function is used to create a queue.
To create a queue
Create a smart pointer to an MSMQQueueInfo interface.
Set the queue path name and label.
(Optional.) Set additional queue properties.
Call MSMQQueueInfo.Create with its IsTransactional parameter set to FALSE (the default).
Example
The following code example contains no version-specific Message Queuing calls.
HRESULT CreateQueue(
WCHAR *wszPathName
)
{
HRESULT hr = S_OK;
// Validate the input string.
if (wszPathName == NULL)
{
return MQ_ERROR_INVALID_PARAMETER;
}
try
{
IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");
// Set the queue's path name and label.
pInfo->PathName = wszPathName;
pInfo->Label = "TestQueue";
// Create the queue.
pInfo->Create();
// Optional. Display the success message.
WCHAR wszMessage[1024] = {0};
// ************************************
// You must concatenate "The queue", wszPathName, and "was created
// successfully." into the wszMessage buffer.
// wszMessage = "The queue " + wszPathName + " was created
// successfully."
// If the message is too long for the buffer, return FALSE.
// Otherwise, show the message
// MessageBoxW(NULL, wszMessage, NULL, MB_OK);
// ************************************
}
catch (const _com_error& comerr)
{
hr = comerr.Error();
// Display the error message.
WCHAR wszMessage[2048] = {0};
// ************************************
// You must concatenate "Error Code: ", hr, "\nError
// Description: ", (WCHAR *)comerr.Description(), and "\n"
// into the wszMessage buffer.
// wszMessage = "Error Code: " + hr + "\nError
// Description: " + (WCHAR *)comerr.Description()+ "\n"
// If the message is too long for the buffer, return FALSE.
// Otherwise, show the message
// MessageBoxW(NULL, wszMessage, NULL, MB_OK);
// ************************************
}
return hr;
}