Delen via


C-C++ Code Example: Opening a Private Queue Using a Computer Identifier and Queue Number

 

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 opens a private queue based on the local computer using its computer identifier and a queue number provided by the caller.

The caller can obtain the queue number from the names of the LQSfiles stored in the Lqs folder (the Lqs folder is created when MSMQ is installed) on the computer. The queue number is the first part of the name of each file.

The following procedure shows how the function opens the queue based on the information provided by the caller.

To open a queue using a computer identifier and queue number

  1. Define the maximum number of properties to be specified, property counter and the MQQMPROPS structure. In this example the computer identifier property is the only property specified.

  2. Specify the PROPID_QM_MACHINE_ID computer property

  3. Initialize the MQQMPROPS structure.

  4. Call MQGetMachineProperties to retrieve the globally unique identifier (GUID) of the computer. The following code example retrieves the GUID of the local computer.

  5. Construct a private format name for the queue using the computer GUID returned by MQGetMachineProperties and the provided queue number. (This example uses the RPC function UuidToString to construct the format of the private queue.)

    The syntax of the private format name is shown below:

    "PRIVATE=ComputerGUID\QueueNumber"  
    
  6. Using the constructed format name and the access and share modes provided in the call, call MQOpenQueue to open the queue.

Code Example

The library and header files required for the RPC calls made by this function are rpcrt4.lib and rpc.h, respectively. This function contains no version-specific Message Queuing calls.

HRESULT OpenMyQueue(  
                    CHAR szQueueNumber[]  
                    )  
{  
  
  HRESULT hr = MQ_OK;                  // Return code  
  
  // Define the maximum number of properties, a property counter, and   
  // an MQQMPROPS structure.  
  const int NUMBEROFPROPERTIES = 1;                   // Number of computer properties  
  DWORD cPropId=0;                                    // Property counter  
  
  MQQMPROPS qmprops;  
  QMPROPID aQMPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aQMPropVar[NUMBEROFPROPERTIES];  
  HRESULT aQMStatus[NUMBEROFPROPERTIES];  
  
  // Specify PROPID_QM_MACHINE_ID.  
  CLSID guidMachineId;                                // Computer GUID buffer  
  aQMPropId[cPropId] = PROPID_QM_MACHINE_ID;          // Property ID  
  aQMPropVar[cPropId].vt = VT_CLSID;                  // Type indicator  
  aQMPropVar[cPropId].puuid = &guidMachineId;         // Computer GUID buffer  
  cPropId++;  
  
  // Initialize the MQQMPROPS structure.  
  qmprops.cProp = cPropId;                            // Number of properties  
  qmprops.aPropID = aQMPropId;                        // IDs of the properties  
  qmprops.aPropVar = aQMPropVar;                      // Values of the properties  
  qmprops.aStatus = aQMStatus;                        // Error reports  
  
  // Call MQGetMachineProperties to retrieve the   
  // GUID of the computer where the queue is registered.  
  hr = MQGetMachineProperties(NULL,  
                              NULL,  
                              &qmprops);  
  if (FAILED(hr))  
  {  
     fprintf(stderr, "An error occurred in MQGetMachineProperties (error: 0x%x).\n",hr);  
     return hr;  
  }  
  
  // Construct the format name of the private queue. The returned   
  // computer GUID is translated into a string and then combined   
  // with the queue number.  
  UCHAR *pszUuid = 0;                     // Computer GUID string  
  CHAR szFormatNameBuffer[256];           // Format name buffer  
  WCHAR wszFormatNameBuffer[256];         // Wide-character format name buffer  
  
  if (UuidToString(&guidMachineId, &pszUuid) != RPC_S_OK)  
  {  
     fprintf(stderr, "An error occurred in UuidToString.\n");  
     return E_FAIL;  
  }  
  else  
  {  
    // Combine the computer ID and queue number.  
  // ************************************  
  // You must concatenate "PRIVATE=", pszUuid, "\", pszUuid, and  
  // szQueueNumber into the szFormatNameBuffer buffer.  
  // szFormatNameBuffer = "PRIVATE=" + pszUuid + "\" + pszUuid +   
  // szQueueNumber  
  // If the format name is too long for the buffer, return S_FALSE.  
  // ************************************  
  
   // Convert the format name to a wide-character string.  
     mbstowcs(wszFormatNameBuffer,  
             szFormatNameBuffer,  
             sizeof(szFormatNameBuffer));  
  RpcStringFree(&pszUuid);  
  }  
  
  // Open the queue.  
  QUEUEHANDLE hQueue = NULL;               // Handle of the created queue  
  DWORD dwAccess = MQ_SEND_ACCESS;         // Access mode of the queue  
  DWORD dwShareMode = MQ_DENY_NONE;        // Share mode of the queue  
  
  hr = MQOpenQueue(  
                   wszFormatNameBuffer,    // Format name of the queue  
                   dwAccess,               // Access mode  
                   dwShareMode,            // Share mode  
                   &hQueue                 // OUT: Queue handle  
                   );  
  if (FAILED(hr))  
  {  
     fprintf(stderr, "An error occurred in MQOpenQueue (error: 0x%x).\n",hr);  
     return hr;  
  }  
  puts("The queue is opened to send messages.");  
  return hr;  
}