C++ COM Code Example: Purging a Queue Synchronously (Windows Embedded CE 6.0)
1/6/2010
This example synchronously retrieves all messages from a known queue.
#include <windows.h>
#include <stdio.h> // for printf
#import "mqoa.dll" no_namespace
int main()
{
///////////////////////////////////////////////////////////
// Initialize OLE
///////////////////////////////////////////////////////////
OleInitialize(NULL);
// Set value of ReceiveTimout parameter.
_variant_t vtReceiveTimeout;
vtReceiveTimeout = (long)1000;
try
{
IMSMQQueueInfoPtr qinfo("MSMQ.MSMQQueueInfo");
IMSMQQueuePtr qRec;
IMSMQMessagePtr msgRec("MSMQ.MSMQMessage");
qinfo->PathName = ".\\testqueue";
try
{
qinfo->Create();
}
catch (_com_error comerr)
{
HRESULT hr = comerr.Error();
if (hr == MQ_ERROR_QUEUE_EXISTS)
{
printf("Opening existing queue.\n");
}
else
{
throw comerr;
}
};
////////////////////////////////////////////////////////////
// Open queue to retrieve message.
////////////////////////////////////////////////////////////
qRec = qinfo->Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE);
////////////////////////////////////////////////////////////
// Retrieve messages from queue.
////////////////////////////////////////////////////////////
msgRec = qRec->Receive(&vtMissing, &vtMissing, &vtMissing, &vtReceiveTimeout);
if (msgRec == NULL)
{
printf("There are no messages in the queue.\n");
return 0;
}
while (msgRec != NULL)
{
printf("Message removed from queue.\n");
msgRec = qRec->Receive(&vtMissing, &vtMissing, &vtMissing, &vtReceiveTimeout);
}
printf("All messages are removed from the queue.\n");
////////////////////////////////////////////////////////////
// Close queue.
////////////////////////////////////////////////////////////
qRec->Close();
return 0;
}
catch (_com_error comerr)
{
HRESULT hr = comerr.Error();
printf("unexpected error: %x\n", hr);
exit(-1); // failure
}
}