Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSMQ help.

Status
Not open for further replies.

jcisco2

Programmer
Apr 13, 2004
102
US
ok i'm going crazy here. i'm praying that someone can help me. please... i sooo new at c++ and i have a deadline looming. i bagged the socket idea. I thought i would try my next thought on application communication. which is MSMQ cause i know how to use the MSMQ through vb.net very well. I found on MS site example code in c++ but i'm having some problems with it. All i want to do is send one message to a private queue called test on the local box with a message title, and body. that's it. nothing else.
but i have no clue on what most of this example is tell me. I see that it is asking for a queue name on the input method (which mine is called test) and it's also asking for a computer name. (my computer is called apple or since i just want it to ref my local box do i call it local?)

I also have no clue on what a WChar is no idea what so ever. how do i put in my queue name into the wszQueueName? same with the computername

i tried
WChar qName = "Test";
WChar cName = "local"; //or is it "apple"? i don't want to have to hard code the computer name.


if anyone has any thoughts on this it will be a great help. Or if i need to clear up anything i would love to.

thanks.
Code:
HRESULT SendMessage(
                    WCHAR * wszQueueName,
                    WCHAR * wszComputerName
                    )
{

  // Validate the input strings.
  if (wszQueueName == NULL || wszComputerName == NULL)
  {
    return MQ_ERROR_INVALID_PARAMETER;
  }
  
  
  // Define the required constants and variables.
  const int NUMBEROFPROPERTIES = 5;                   // Number of properties
  DWORD cPropId = 0;                                  // Property counter
  HRESULT hr = MQ_OK;                                 // Return code
  HANDLE hQueue = NULL;                               // Queue handle

  // 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_LABEL;               // Property ID
  aMsgPropVar[cPropId].vt = VT_LPWSTR;                // Type indicator
  aMsgPropVar[cPropId].pwszVal = L"Test Message";     // The message label
  cPropId++;
  
  
  // Initialize the MQMSGPROPS structure.
  msgProps.cProp = cPropId;    
  msgProps.aPropID = aMsgPropId;
  msgProps.aPropVar = aMsgPropVar;
  msgProps.aStatus = aMsgStatus;
  
  
  // Create a direct format name for the queue.
  WCHAR * wszFormatName = NULL;
  DWORD dwBufferLength = 0;
  dwBufferLength = wcslen(wszQueueName) + wcslen(wszComputerName) + 12;
  wszFormatName = new WCHAR[dwBufferLength];
  if (wszFormatName == NULL)
  {
    return MQ_ERROR_INSUFFICIENT_RESOURCES;
  }
  memset(wszFormatName, 0, dwBufferLength*sizeof(WCHAR));
  
  if (_snwprintf(
                 wszFormatName,
                 dwBufferLength - 1,
                 L"DIRECT=OS:%s\\%s",
                 wszComputerName,
                 wszQueueName
                ) < 0)
  {
    wprintf(L"The format name is too long for the buffer specified.\n");
    return FALSE;
  }
  else
  {
    wszFormatName[dwBufferLength - 1] = L'\0';
  }
  
  
  // Call MQOpenQueue to open the queue with send access.
  hr = MQOpenQueue(
                   wszFormatName,                     // Format name of the queue
                   MQ_SEND_ACCESS,                    // Access mode
                   MQ_DENY_NONE,                      // Share mode
                   &hQueue                            // OUT: Queue handle
                   );
  // Free the memory that was allocated for the format name string.
  delete [] wszFormatName;


  // Handle any error returned by MQOpenQueue.
  if (FAILED(hr))
  {
    return hr;
  }
  
  
  // Call MQSendMessage to send the message to the queue.
  hr = MQSendMessage(
                     hQueue,                          // Queue handle
                     &msgProps,                       // Message property structure
                     MQ_NO_TRANSACTION               // Not in a transaction
                     );
  if (FAILED(hr))
  {
    MQCloseQueue(hQueue);
    return hr;
  }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top