C-C++ Code Example: Time Sent Filters
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 retrieves all the messages in a queue that were sent after a specific date and time. Filtering is done by peeking at the PROPID_M_SENTTIME property of each message in the queue and removing only those messages sent after the date and time provided by the caller.
To filter messages by sent time
Define the maximum number of queue properties to be specified and the queue property counter.
Define the MQMSGPROPS structure.
Specify PROPID_M_SENTTIME.
Initialize the MQMSGPROPS structure.
Call MQOpenQueue to open the queue with receive access. Receive access allows the application to peek at or remove the messages in the queue.
Call MQCreateCursor to create the cursor for navigating the queue. The returned cursor handle is used in the calls to read messages.
Call MQReceiveMessage with peek access to peek at the first message in the queue. This call also initializes the cursor so that the cursor points at the first message in the queue.
In a loop structure, continue to call MQReceiveMessage to peek at and remove the messages in the queue.
Note
The receive action of each call varies depending on if the previous message was or was not removed from the queue.
If the current message was sent later than the provided date and time, set dwRecAction to MQ_ACTION_RECEIVE, call MQReceiveMessage to remove the message, and then reset dwRecAction to MQ_ACTION_PEEK_CURRENT to process the next message. This will remove the current message and Message Queuing moves the cursor to the next message.
If the message was sent prior to or at the provided date and time, set dwRecAction to MQ_ACTION_PEEK_NEXT to move the cursor and process the next message. In this case, the application must move the cursor to the next message.
- Call MQCloseQueue and MQCloseCursor to free the resources.
Code Example
The following code example can be run on all versions of Message Queuing.
HRESULT FilterSentTime(
LPCWSTR wszQueueFormatName,
DWORD dwSentTime
)
{
// Validate the input string.
if (wszQueueFormatName == NULL)
{
return MQ_ERROR_INVALID_PARAMETER;
}
// Define the required structures for the filter.
const int NUMBEROFPROPERTIES = 5; // Number of properties
DWORD cPropId = 0; // Property counter
//Define MQMSGPROPS structure.
MQMSGPROPS msgprops;
MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
PROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
HRESULT aMsgStatus[NUMBEROFPROPERTIES];
HANDLE hQueue = NULL; // Queue handle
HANDLE hCursor = NULL; // Cursor handle
HRESULT hr = MQ_OK; // Return code
DWORD dwRecAction; // Receive action mode
// Specify the message properties to be retrieved
aMsgPropId[cPropId] = PROPID_M_SENTTIME; // Property ID
aMsgPropVar[cPropId].vt = VT_UI4; // Type indicator
cPropId++;
// Initialize the MQMSGPROPS structure.
msgprops.cProp = cPropId; // Number of message properties
msgprops.aPropID = aMsgPropId; // IDs of message properties
msgprops.aPropVar = aMsgPropVar; // Values of message properties
msgprops.aStatus = aMsgStatus; // Error reports
// Open the queue to filter messages.
hr = MQOpenQueue(
wszQueueFormatName, // Format name of the queue
MQ_RECEIVE_ACCESS, // Access mode
MQ_DENY_RECEIVE_SHARE, // Share mode
&hQueue // OUT: Queue handle
);
if (FAILED(hr))
{
return hr;
}
// Create the cursor used to navigate through the queue.
hr = MQCreateCursor(
hQueue, // Queue handle
&hCursor // OUT: Cursor handle
);
if (FAILED(hr))
{
MQCloseQueue(hQueue);
return hr;
}
// Peek at the first message in the queue.
hr = MQReceiveMessage(
hQueue, // Queue handle
0, // Max time (msec)
MQ_ACTION_PEEK_CURRENT, // Receive action
&msgprops, // Message property structure
NULL, // No OVERLAPPED structure
NULL, // No callback function
hCursor, // Cursor handle
MQ_NO_TRANSACTION // Not in a transaction
);
if (FAILED(hr))
{
MQCloseCursor(hCursor);
MQCloseQueue(hQueue);
return hr;
}
// Filter messages from queue.
do
{
if (msgprops.aPropVar[0].ulVal > dwSentTime)
{
dwRecAction = MQ_ACTION_RECEIVE; // Receive action
hr = MQReceiveMessage(
hQueue, // Queue handle
0, // Max time (msec)
dwRecAction, // Receive action
&msgprops, // Message property structure
NULL, // No OVERLAPPED structure
NULL, // No callback function
hCursor, // Cursor handle
MQ_NO_TRANSACTION // Not in a transaction
);
if (FAILED(hr))
{
break;
}
//
// Process the message.
//
dwRecAction = MQ_ACTION_PEEK_CURRENT; // Reset to peek at current cursor position
}
else
{
dwRecAction = MQ_ACTION_PEEK_NEXT; // Reset receive action mode
}
hr = MQReceiveMessage(
hQueue, // Queue handle
0, // Max time (msec)
dwRecAction, // Receive action
&msgprops, // Message property structure
NULL, // No OVERLAPPED structure
NULL, // No callback function
hCursor, // Cursor handle
MQ_NO_TRANSACTION // Not in a transaction
);
if (FAILED(hr))
{
break;
}
} while (SUCCEEDED(hr));
// Close cursor and queue.
hr = MQCloseCursor(hCursor);
if (FAILED(hr))
{
MQCloseQueue(hQueue);
return hr;
}
hr = MQCloseQueue(hQueue);
return hr;
}