Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This example synchronously retrieves all messages from a known queue.

Copy Code
#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
  }
}

See Also