Sending Private Messages

To send a private message , the sending application must set the privacy level of the message. Once the privacy level is set, the functions used to send the message are no different from those used to send other messages. When sending (and receiving) private messages, the application has no part in encrypting (or decrypting) the message.

Note For information on how MSMQ encrypts and decrypts messages, see Private Messages.

To send a private message
  1. Obtain the format name of the destination queue. The example below calls MQPathNameToFormatName to obtain a format name from a known pathname.
    WCHAR szFormatName[128];
    DWORD dwFormatNameLen = sizeof(szFormatName) / sizeof(WCHAR);
        
    hr = MQPathNameToFormatName(L"dest_machine\\secrets", 
                                szFormatName, 
                                &dwFormatNameLen);
     
  2. Call MQOpenQueue to open the destination queue with send access.
    QUEUEHANDLE hQueue;
    hr = MQOpenQueue(szFormatName, MQ_SEND_ACCESS, 0, &hQueue);
        
  3. Set message properties. The value of PROPID_M_PRIV_LEVEL indicates that the message is private.
    #define NMSGPROPS 10
    MSGPROPID aMsgPropId[NMSGPROPS];
    MQPROPVARIANT aMsgPropVar[NMSGPROPS];
    HRESULT aMsgStatus[NMSGPROPS];
    MQMSGPROPS MsgProps;
    DWORD PropIdCount = 0;
        
    //Set PROPID_M_LABEL
    aMsgPropId[PropIdCount] = PROPID_M_LABEL;            //PropId
    aMsgPropVar[PropIdCount].vt = VT_LPWSTR;             //Type
    aMsgPropVar[PropIdCount].pwszVal = L"Hash hash";     //Value
    PropIdCount++
        
    //Set PROPID_M_BODY
    #define MESSAGE_BODY L"Secret matters"
    aMsgPropId[PropIdCount] = PROPID_M_BODY;             //PropId
    aMsgPropVar[PropIdCount].vt = VT_VECTOR | VT_UI1;
    aMsgPropVar[PropIdCount].caub.pElems = (LPBYTE)MESSAGE_BODY;
    aMsgPropVar[PropIdCount].caub.cElems = sizeof(MESSAGE_BODY);
    PropIdCount++
        
    //Set PROPID_M_PRIV_LEVEL
    aMsgPropId[PropIdCount] = PROPID_M_PRIV_LEVEL;       //PropId
    aMsgPropVar[PropIdCount].vt = VT_UI4;                //Type
    aMsgPropVar[PropIdCount].ulVal = MQMSG_PRIV_LEVEL_BODY;
     PropIdCount++
        
    //Optional. Set PROPID_M_ENCRYPTION_ALG. 
    aMsgPropId[PropIdCount] = PROPID_M_ENCRYPTION_ALG;   //PropId
    aMsgPropVar[PropIdCount].vt = VT_UI4;                //Type
    aMsgPropVar[PropIdCount].ulVal = CALG_RC4;    //Default is RC2.
     PropIdCount++
        
    //Set the MQMSGPROPS structure.
    MsgProps.cProp = PropIdCount;       //Number of properties.
    MsgProps.aPropID = aMsgPropId;      //Id of properties.
    MsgProps.aPropVar = aMsgVariant;    //Value of properties.
    MsgProps.aStatus  = aMsgStatus;     //Error report.
        
  4. Call MQSendMessage to send the message.
    hr = MQSendMessage(hQueue, &MsgProps, NULL);
     
  5. Call MQCloseQueue to close the destination queue.
    hr = MQCloseQueue(hQueue);
        

Example

This example starts with a known pathname for the destination queue, translates the pathname to a format name, opens the destination queue with send access, sets the properties of the message (including the privacy level of the message), then sends the message to the destination queue.

#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <mq.h>
 
int main(int argc, char *argv[])
{
    HRESULT hr;
    
    
    //////////////////////////
    // Get the format name of 
    // the destination queue.
    //////////////////////////
    
    WCHAR szFormatName[128];
    DWORD dwFormatNameLen = sizeof(szFormatName) / sizeof(WCHAR);
    
    hr = MQPathNameToFormatName(L"dest_machine\\secrets", 
                                szFormatName, 
                                &dwFormatNameLen);
    if (FAILED(hr))
    {
        fprintf(stderr, "Failed in MQPathNameToFormatName, error = 0x%x\n", hr);
        return -1;
    }
    
    //////////////////////////////
    // Open the destination queue
    // with send access.
    //////////////////////////////
    
    QUEUEHANDLE hQueue;
    hr = MQOpenQueue(szFormatName, MQ_SEND_ACCESS, 0, &hQueue);
    if (FAILED(hr))
    {
        fprintf(stderr, "Failed in MQOpenQueue, error = 0x%x\n", hr);
        return -1;
    }
     
     
    ////////////////////////////////////
    // Define the MQMSGPROPS structure.
    ////////////////////////////////////
    
    #define NMSGPROPS 10
    MSGPROPID aMsgPropId[NMSGPROPS];
    MQPROPVARIANT aMsgPropVar[NMSGPROPS];
    HRESULT aMsgStatus[NMSGPROPS];
    MQMSGPROPS MsgProps;
    DWORD PropIdCount = 0;
    
    //Set PROPID_M_LABEL
    aMsgPropId[PropIdCount] = PROPID_M_LABEL;            //PropId
    aMsgPropVar[PropIdCount].vt = VT_LPWSTR;             //Type
    aMsgPropVar[PropIdCount].pwszVal = L"Hash hash";     //Value
    PropIdCount++
    
    //Set PROPID_M_BODY
    #define MESSAGE_BODY L"Secret matters"
    aMsgPropId[PropIdCount] = PROPID_M_BODY;             //PropId
    aMsgPropVar[PropIdCount].vt = VT_VECTOR | VT_UI1;
    aMsgPropVar[PropIdCount].caub.pElems = (LPBYTE)MESSAGE_BODY;
    aMsgPropVar[PropIdCount].caub.cElems = sizeof(MESSAGE_BODY);
    PropIdCount++
    
    //Set PROPID_M_PRIV_LEVEL
    aMsgPropId[PropIdCount] = PROPID_M_PRIV_LEVEL;       //PropId
    aMsgPropVar[PropIdCount].vt = VT_UI4;                //Type
    aMsgPropVar[PropIdCount].ulVal = MQMSG_PRIV_LEVEL_BODY;
     PropIdCount++
    
    //Optional. Set PROPID_M_ENCRYPTION_ALG. 
    aMsgPropId[PropIdCount] = PROPID_M_ENCRYPTION_ALG;   //PropId
    aMsgPropVar[PropIdCount].vt = VT_UI4;                //Type
    aMsgPropVar[PropIdCount].ulVal = CALG_RC4;    //Default is RC2.
     PropIdCount++
    
    //Set the MQMSGPROPS structure.
    MsgProps.cProp = PropIdCount;       //Number of properties.
    MsgProps.aPropID = aMsgPropId;      //Id of properties.
    MsgProps.aPropVar = aMsgVariant;    //Value of properties.
    MsgProps.aStatus  = aMsgStatus;     //Error report.

    ///////////////////
    // Send the message
    ///////////////////
    
    hr = MQSendMessage(hQueue, &MsgProps, NULL);
    if (FAILED(hr))
    {
     fprint(stderr, "Failed in MQSendMessage, error = 0x%x\n", hr);
     return -1;
    }
    
    ////////////////////
    // Close the queue.
    ///////////////////
    MQCloseQueue(hQueue);
    
    if (FAILED(hr))
    {
      fprintf(stderr, "Failed in MQCloseQueue, error = 0x%x\n", hr);
      return -1;
    }
    printf("The private message was sent successfully\n");
    
    return 0;
}
 

© 1997 by Microsoft Corporation. All rights reserved.