Thursday, April 26, 2012

Open Message Queue in C++

Open MSMQ in C++ :


The MQOpenQueue function opens a queue for sending, peeking at, retrieving, or purging messages.

HRESULT APIENTRY MQOpenQueue(
  LPCWSTR lpwcsFormatName, 
  DWORD dwAccess,       
  DWORD dwShareMode,    
  QUEUEHANDLE * phQueue 
);

Parameters:
lpwcsFormatName :

[in] Pointer to the format name string of the queue you want to open.

dwAccess:

[in] Specifies how the application accesses the queue (peek, send, or receive). This setting cannot be changed while the queue is open.

Specify one of the following access modes:

MQ_PEEK_ACCESS

Messages can only be looked at. They cannot be removed from the queue.

MQ_SEND_ACCESS

Messages can only be sent to the queue. A subqueue cannot be opened using this access mode.

MQ_MOVE_ACCESS

Can be used only with a subqueue. Requires the user to have peek permission for the queue.

MQ_RECEIVE_ACCESS

Messages can be retrieved (read and removed) from the queue, peeked at, or purged. Whether a message is removed from the queue in a call to MQReceiveMessage depends on the dwAction parameter of this function.

See the description of the dwShareMode parameter for information on limiting who can receive messages from the queue.

MQ_PEEK_ACCESS | MQ_ADMIN_ACCESS

Messages in the local outgoing queue can only be peeked at (read without being removed from the queue).

MQ_RECEIVE_ACCESS | MQ_ADMIN_ACCESS

Messages in the local outgoing queue can be retrieved (read and removed from the queue), peeked at (read without being removed from the queue), or purged (deleted).

Note   MQ_ADMIN_ACCESS is used to access messages in a local outgoing queue, rather than the corresponding remote destination queue.

dwShareMode:

[in] How the queue will be shared. Specify one of the following:

MQ_DENY_NONE

Default. The queue is available to everyone. This setting must be used if dwAccess is set to MQ_SEND_ACCESS.

MQ_DENY_RECEIVE_SHARE

Limits those who can receive messages from the queue to this process. Once a process opens a queue with this share mode and with dwAccess set to MQ_RECEIVE_ACCESS, no one else, including the process that opened the queue, can open it again to peek or receive messages (this includes attempting to open the queue with multiple threads within the same process) until the original caller closes the queue. However, inside the process, the returned queue handle can be used by several threads.

Once the queue is opened with this share mode and with dwAccess set to MQ_RECEIVE_ACCESS, the MQ_ERROR_SHARING_VIOLATION error is returned when a second attempt is made to open the queue to peek or receive messages.

phQueue

[out] Pointer to a handle to the opened queue. If MQOpenQueue fails, a NULL pointer is returned.

Example Code:

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

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


  // 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;
    }
  }
  return hr;

 }


int main(array ^args)
{

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

No comments:

Post a Comment