共用方式為


C-C++ Code Example: Requesting Tracing

 

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 requests report messages based on a tracing level value. This function sets the PROPID_M_TRACE property of the message using the tracing level value supplied and then sends the message to the destination queue specified.

For information on how Message Queuing traces messages, see Tracing Messages.

To request tracing

  1. Define constants, variables, and message property structure.

  2. Specify message properties. This example sets the following message properties.

  3. Initialize the MQMSGPROPS structure.

  4. Call MQOpenQueue to open the destination queue with send access.

  5. Call MQSendMessage to send the message to the destination queue.

  6. Call MQCloseQueue to close the destination queue and free resources.

Code Example

The following code example can be run on all versions of Message Queuing.

HRESULT RequestingTracing(  
                          LPCWSTR wszQueueFormatName,  
                          unsigned char bTrace  
                          )  
{  
  
  // Validate the input string.  
  if (wszQueueFormatName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define constants, variables, and a message property structure.  
  const int NUMBEROFPROPERTIES = 2;  
  DWORD cPropId = 0;  
  
  MQMSGPROPS MsgProps;  
  MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];   
  HRESULT aMsgStatus[NUMBEROFPROPERTIES];  
  
  HRESULT hr;  
  
  // Specify message properties.  
  aMsgPropId[cPropId] = PROPID_M_TRACE;  
  aMsgPropVar[cPropId].vt = VT_UI1;  
  aMsgPropVar[cPropId].bVal = bTrace;  
  cPropId++;  
  
  aMsgPropId[cPropId] = PROPID_M_LABEL;  
  aMsgPropVar[cPropId].vt = VT_LPWSTR;  
  aMsgPropVar[cPropId].pwszVal = L"Test Message: Tracing";  
  cPropId++;  
  
  // Initialize the MQMSGPROPS structure.  
  MsgProps.cProp = cPropId;  
  MsgProps.aPropID = aMsgPropId;  
  MsgProps.aPropVar = aMsgPropVar;  
  MsgProps.aStatus = aMsgStatus;  
  
  // Call MQOpenQueue to open the destination queue.  
  HANDLE hQueue = NULL;  
  hr = MQOpenQueue(  
                   wszQueueFormatName,  
                   MQ_SEND_ACCESS,  
                   MQ_DENY_NONE,  
                   &hQueue  
                   );  
  if (FAILED(hr))  
  {  
    return hr;  
  }  
  
  // Call MQSendMessage to send the message to the destination queue.  
  hr = MQSendMessage(  
                     hQueue,  
                     &MsgProps,  
                     MQ_NO_TRANSACTION  
                     );  
  if (FAILED(hr))  
  {  
    MQCloseQueue(hQueue);  
    return hr;  
  }  
  
  // Call MQCloseQueue to close the destination queue.  
  hr = MQCloseQueue(hQueue);  
  
  return hr;  
}