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

Message Queuing COM objects can be invoked using Microsoft eMbedded Visual C++. There are different ways to implement and use COM components of MSMQ.

The following code examples illustrate the standard method and one that uses smart pointers.

Note:
The desktop version of MSMQ uses #import to reference Mqoa.dll; however, this is not supported in Windows Embedded CE.

You need Mqoai.h for standard COM programming and you must include Mqoa.lib in the project directory.

The Mqoa.lib file is in the %_WINCEROOT%\Public\Servers\Oak\Lib\x86\Retail directory.

To use smart pointers, also include Atlbase.h. The smart pointer provides reference counting and IUnknown::QueryInterfacesupport, so you do not need explicit AddRefand QueryInterfacecalls.

Copy Code
//
// MSMQ COM sample using smart pointer
//
// following sample code sends L"sample text" to the Q1 in
abcd.microsoft.com
//

#include <atlbase.h>
#include <mqoai.h>
// should include mqoa.lib to the projet
// mqoa.lib is under Public\SERVERS\OAK\LIB\X86\RETAIL
//#import "mqoa.dll" no_namespace   // not supported


HRESULT SendMessage()
{
   CComQIPtr<IMSMQQueueInfo, &IID_IMSMQQueueInfo>
ipQueueInfo;
   HRESULT hr = S_OK;
// hr = ipQueueInfo.CoCreateInstance(CLSID_MSMQQueueInfo);  // this
form is not supported
   hr = CoCreateInstance(
		 CLSID_MSMQQueueInfo,
		 NULL,
		 CLSCTX_SERVER,
		 IID_IMSMQQueueInfo,
		 (void**)(&ipQueueInfo.p));
   if(hr != S_OK)
	return hr;

   hr =
ipQueueInfo->put_PathName(L"abcd.microsoft.com\\private$\\Q1");
   if(hr != S_OK)
	return hr;

   IMSMQQueue* ipSend;
   hr = ipQueueInfo->Open(MQ_SEND_ACCESS, MQ_DENY_NONE,
&ipSend);
   if(hr != S_OK)
	return hr;

   CComQIPtr<IMSMQMessage, &IID_IMSMQMessage> ipMessage;
// hr = ipMessage.CoCreateInstance(CLSID_MSMQMessage);  // this
form is not supported
   hr = CoCreateInstance(
		 CLSID_MSMQMessage,
		 NULL,
		 CLSCTX_SERVER,
		 IID_IMSMQMessage,
		 (void**)(&ipMessage.p));
   if(hr != S_OK)
	return hr;

   VARIANT vtFalse;
   VariantInit(&vtFalse);
   vtFalse.vt = VT_BOOL;
   vtFalse.boolVal = FALSE;

   BSTR bszTextBody = SysAllocString(L"sample text");
   if (!bszTextBody)
	return ERROR_OUTOFMEMORY;
   VARIANT vtTextBody;
   VariantInit(&vtTextBody);
   vtTextBody.bstrVal = bszTextBody;
   vtTextBody.vt = VT_BSTR;

   hr = ipMessage->put_Body(vtTextBody);
   if(hr != S_OK) {
	SysFreeString(bszTextBody);
	return hr;
   }

   hr = ipMessage->Send(ipSend, &vtFalse);
   SysFreeString(bszTextBody);

   return hr;
}


#include <stdio.h>

int main(int argc, WCHAR* argv[])
{
   HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
   if(hr != S_OK)
   {
	printf(L"MSMQ.COM CoInitializeEx() error 0x%0X\n", hr);
	return -1;
   }

   hr = SendMessage();
   if(S_OK != hr)
	printf("MSMQ.COM message send error 0x%0X\n", hr);

   CoUninitialize();
   printf("MSMQ.COM message sent\n");
   return 0;
}
----------------------------------------------------------------------
//
// MSMQ COM sample using standard COM interface
//
// following sample code sends L"sample text" to the Q1 in
abcd.microsoft.com
//

#include <atlbase.h>
#include <mqoai.h>
// should include mqoa.lib to the projet
// mqoa.lib is under Public\SERVERS\OAK\LIB\X86\RETAIL
//#import "mqoa.dll" no_namespace   // not supported


int mqComTest1(CEdit& mEdit)
{
   IMSMQQueueInfo *ipQueueInfo;
   HRESULT hr = S_OK;
   hr = CoCreateInstance(
		 CLSID_MSMQQueueInfo,
		 NULL,
		 CLSCTX_SERVER,
		 IID_IMSMQQueueInfo,
		 (void**)(&ipQueueInfo));
   if(hr != S_OK)
	return hr;

   hr =
ipQueueInfo->put_PathName(L"abcd.microsoft.com\\private$\\Q1");
   if(hr != S_OK)
   {
	ipQueueInfo->Release();
	return hr;
   }

   IMSMQQueue* ipSendQueue;
   hr = ipQueueInfo->Open(MQ_SEND_ACCESS, MQ_DENY_NONE,
&ipSendQueue);
   if(hr != S_OK)
   {
	ipQueueInfo->Release();
	return hr;
   }

   IMSMQMessage *ipSendMessage;
   hr = CoCreateInstance(
		 CLSID_MSMQMessage,
		 NULL,
		 CLSCTX_SERVER,
		 IID_IMSMQMessage,
		 (void**)(&ipSendMessage));
   if(hr != S_OK)
   {
	ipSendQueue->Close();
	ipSendQueue->Release();
	ipQueueInfo->Release();
	return hr;
   }

   VARIANT vtFalse;
   VariantInit(&vtFalse);
   vtFalse.vt = VT_BOOL;
   vtFalse.boolVal = FALSE;

   VARIANT vtSendText;
   VariantInit(&vtSendText);
   vtSendText.vt = VT_BSTR; 
   vtSendText.bstrVal = SysAllocString(L"sample text");
   if (vtSendText.bstrVal != NULL)
   {
	if(
		 ((hr=ipSendMessage->put_Body(vtSendText)) != S_OK) ||
		 ((hr=ipSendMessage->Send(ipSendQueue, &vtFalse)) !=
S_OK)
	)
		 ;
	SysFreeString(vtSendText.bstrVal);
   }

   ipSendMessage->Release();
   ipSendQueue->Close();
   ipSendQueue->Release();
   ipQueueInfo->Release();
   return hr;
}



#include <stdio.h>

int main(int argc, WCHAR* argv[])
{
   HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
   if(hr != S_OK)
   {
	printf(L"MSMQ.COM CoInitializeEx() error 0x%0X\n", hr);
	return -1;
   }

   hr = SendMessage();
   if(S_OK != hr)
	printf("MSMQ.COM message send error 0x%0X\n", hr);

   CoUninitialize();
   printf("MSMQ.COM message sent\n");
   return 0;
}

See Also