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
Before calling the function, specify the number of message properties that will be set and obtain the data needed for input parameters.
Define the MQMSGPROPS structure.
Specify message properties. This example sets the following message properties.
PROPID_M_PRIV_LEVEL: Specifies the privacy level provided by the caller.
PROPID_M_LABEL: Identifies the message.
Initialize the MQMSGPROPS structure.
Call MQOpenQueue to open the destination queue with send access. In this example the format name is provided by the caller.
Call MQSendMessage to send the message to the destination queue.
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;
}