共用方式為


C-C++ Code Example: Requesting Encryption

 

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 encryption based on a given privacy level and destination queue.

This function sets the PROPID_M_PRIV_LEVEL property of the message using the privacy level supplied by the caller and then sends the message to a known destination queue. For information on how Message Queuing encrypts messages, see Message Encryption.

Note

This example requests 40-bit end-to-end encryption. Support for 128-bit end-to-end encryption is introduced in MSMQ 2.0.

To request encryption

  1. Before calling the function, specify the number of message properties that will be set and obtain the data needed for input parameters.

  2. Define the MQMSGPROPS structure.

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

  4. Initialize the MQMSGPROPS structure.

  5. Call MQOpenQueue to open the destination queue with send access. In this example the format name is provided by the caller.

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

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

Code Example

The following code example requires MSMQ 2.0 or later.

HRESULT Requesting40BitEncryption(  
                                  LPCWSTR wszQueueFormatName  
                                  )  
{  
  
  // Validate the input string.  
  if (wszQueueFormatName == NULL)  
  {  
    return MQ_ERROR_INVALID_PARAMETER;  
  }  
  
  // Define the maximum number of message properties.  
  const int NUMBEROFPROPERTIES = 2;  
  
  // Define MQMSGPROPS structure.  
  MQMSGPROPS MsgProps;  
  MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];  
  MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];   
  HRESULT aMsgStatus[NUMBEROFPROPERTIES];  
  HRESULT hr = MQ_OK;  
  
  // Specify message properties.  
  DWORD cPropId = 0;  
  aMsgPropId[cPropId] = PROPID_M_PRIV_LEVEL;  
  aMsgPropVar[cPropId].vt = VT_UI4;  
  aMsgPropVar[cPropId].ulVal = MQMSG_PRIV_LEVEL_BODY_BASE;  
  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 a 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;  
}