Friday, April 27, 2012

Send Message to MSMQ in C++

Send Message to MSMQ in C++ :


The MQSendMessage function sends a message to the corresponding queue.


HRESULT APIENTRY MQSendMessage(
  QUEUEHANDLE hDestinationQueue, 
  MQMSGPROPS * pMessageProps,     
  ITransaction * pTransaction     
);

Parameters:

hDestinationQueue

[in] Handle to the queue where you want to send the message.

pMessageProps

[in] Pointer to an MQMSGPROPS structure describing the message to send.

pTransaction

[in] Must be a pointer to an ITransaction interface, a constant, or NULL.


Constants include:

MQ_NO_TRANSACTION

Specifies that the call is not part of a transaction. This constant cannot be used to send a message to a transactional queue.

MQ_MTS_TRANSACTION

Instructs Message Queuing to verify that a COM+ object is running and that the current COM+ object is participating in a transaction. If Message Queuing finds that the application is running in the context of a COM+ (Component Services) transaction, the message is sent within the current COM+ transaction. Otherwise, the message is sent outside of a transaction. For more information, see COM+ Transactions.

MQ_SINGLE_MESSAGE

Specifies that the message is sent in a single-message transaction. Messages sent in a single-message transaction must be sent to a transactional queue.

MQ_XA_TRANSACTION

Specifies that the call is part of an externally coordinated, XA-compliant transaction.


To Send a message:

    1.Define the required constants and variables.

    2.Define the MQMSGPROPS structure.

    3.Specify the properties of the message. This example defines the PROPID_M_LABEL property.

    4.Initialize the MQMSGPROPS structure.

    5.Call MQOpenQueue to open the queue with send access.

    6.Call MQSendMessage to send the message.

    7.Call MQCloseQueue to close the opened queue and free resources.





Example Code:

#include "stdafx.h"
#include "windows.h"
#include "mq.h"
#include "tchar.h"
#include




 
HRESULT SendMessage(LPWSTR wszPathName)
{
  QUEUEHANDLE phQueue = new QUEUEHANDLE ;
  HRESULT hr = S_FALSE;
  DWORD dwFormatNameLength = 0;
  WCHAR *wszFormatName = NULL;

  const int NUMBEROFPROPERTIES = 5; 
  DWORD cPropId = 0;
   // Define an MQMSGPROPS structure.
  MQMSGPROPS msgProps;
  MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
  MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
  HRESULT aMsgStatus[NUMBEROFPROPERTIES];
 
 
  // Specify the message properties to be sent.
  aMsgPropId[cPropId] = PROPID_M_BODY;            
  aMsgPropVar[cPropId].vt =VT_VECTOR | VT_UI1;
  aMsgPropVar[cPropId].caub.pElems = (LPBYTE)"Test Message";
  aMsgPropVar[cPropId].caub.cElems = sizeof("Test Message");

 
  cPropId++;
  aMsgPropId[cPropId] = PROPID_M_LABEL;             
  aMsgPropVar[cPropId].vt = VT_LPWSTR;              
  aMsgPropVar[cPropId].pwszVal = L"Test Message";
  cPropId++;
 
  // Initialize the MQMSGPROPS structure.
  msgProps.cProp = cPropId;
  msgProps.aPropID = aMsgPropId;
  msgProps.aPropVar = aMsgPropVar;
  msgProps.aStatus = aMsgStatus;


  // Validate the input parameters.
  if ((wszPathName == NULL) || (phQueue == NULL))
  {
    return MQ_ERROR_INVALID_PARAMETER;
  }


  // Create a buffer for the direct format name.
  dwFormatNameLength = wcslen(wszPathName) + 11;
  wszFormatName = new WCHAR[dwFormatNameLength];
 
  if (wszFormatName)
  {


    // Create the direct format name of the queue.
    memset(wszFormatName, 0, dwFormatNameLength*sizeof(WCHAR));
    if (_snwprintf(
                   wszFormatName,
                   dwFormatNameLength - 1,
                   L"DIRECT=OS:%s",
                   wszPathName
                   ) < 0)
    {
      wprintf(L"The format name is too long for the buffer specified.\n");
      return FALSE;
    }
    else
    {
      wszFormatName[dwFormatNameLength - 1] = L'\0';
    }
 
 
    // Call MQOpenQueue to open the queue with the access and
    // share mode provided by the caller.
    hr = MQOpenQueue(
                     wszFormatName,           // Direct format name of the queue
                     MQ_SEND_ACCESS,                    // Access mode
                     MQ_DENY_NONE,             // Share mode
                     &phQueue                  // OUT: Queue handle
                     );
    delete [] wszFormatName;
    if (FAILED(hr))
    {
      fprintf(stderr, "An error occurred in MQOpenQueue \n",hr);
      return hr;
    }
  }

 
  // Call MQSendMessage to send the message to the queue.
  hr = MQSendMessage(
                     phQueue,                          // Queue handle
                     &msgProps,                       // Message property structure
                     MQ_NO_TRANSACTION               // Not in a transaction
                     );
  if (FAILED(hr))
  {
    MQCloseQueue(phQueue);
    return hr;
  }
 
 
  // Call MQCloseQueue to close the queue.
  hr = MQCloseQueue(phQueue);

  return hr;

 }


int main(array ^args)
{

    LPWSTR wszPathName =L".\\PRIVATE$\\SampleQueue";
    HRESULT hr = SendMessage(wszPathName);
    return 0;
}






No comments:

Post a Comment