Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. WCHAR * wszQueueName,
  2. WCHAR * wszComputerName
  3. )
  4. {
  5.  
  6. // Define the required constants and variables.
  7. const int NUMBEROFPROPERTIES = 5;
  8. DWORD cPropId = 0;
  9. HRESULT hr = MQ_OK; // Return code
  10. HANDLE hQueue = NULL; // Queue handle
  11. ULONG ulBufferSize = 2;
  12.  
  13. // Define an MQMSGPROPS structure.
  14. MQMSGPROPS msgprops;
  15. MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
  16. MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
  17. HRESULT aMsgStatus[NUMBEROFPROPERTIES];
  18.  
  19. // Specify the message properties to be retrieved.
  20. aMsgPropId[cPropId] = PROPID_M_LABEL_LEN; // Property ID
  21. aMsgPropVar[cPropId].vt = VT_UI4; // Type indicator
  22. aMsgPropVar[cPropId].ulVal = MQ_MAX_MSG_LABEL_LEN; // Length of label
  23. cPropId++;
  24.  
  25. WCHAR wszLabelBuffer[MQ_MAX_MSG_LABEL_LEN]; // Label buffer
  26. aMsgPropId[cPropId] = PROPID_M_LABEL; // Property ID
  27. aMsgPropVar[cPropId].vt = VT_LPWSTR; // Type indicator
  28. aMsgPropVar[cPropId].pwszVal = wszLabelBuffer; // Label buffer
  29. cPropId++;
  30.  
  31. UCHAR * pucBodyBuffer = NULL;
  32. pucBodyBuffer = (UCHAR*)malloc(ulBufferSize);
  33. if (pucBodyBuffer == NULL)
  34. {
  35. return MQ_ERROR_INSUFFICIENT_RESOURCES;
  36. }
  37. memset(pucBodyBuffer, 0, ulBufferSize);
  38. aMsgPropId[cPropId] = PROPID_M_BODY_SIZE; // Property ID
  39. aMsgPropVar[cPropId].vt = VT_NULL; // Type indicator
  40. cPropId++;
  41.  
  42. aMsgPropId[cPropId] = PROPID_M_BODY; // Property ID
  43. aMsgPropVar[cPropId].vt = VT_VECTOR | VT_UI1; // Type indicator
  44. aMsgPropVar[cPropId].caub.pElems = (UCHAR*)pucBodyBuffer; // Body buffer
  45. aMsgPropVar[cPropId].caub.cElems = ulBufferSize; // Buffer size
  46. cPropId++;
  47.  
  48. aMsgPropId[cPropId] = PROPID_M_BODY_TYPE; // Property ID
  49. aMsgPropVar[cPropId].vt = VT_NULL; // Type indicator
  50. cPropId++;
  51.  
  52. // Initialize the MQMSGPROPS structure.
  53. msgprops.cProp = cPropId; // Number of message properties
  54. msgprops.aPropID = aMsgPropId; // IDs of the message properties
  55. msgprops.aPropVar = aMsgPropVar; // Values of the message properties
  56. msgprops.aStatus = aMsgStatus; // Error reports
  57.  
  58. // Validate the input strings.
  59. if (wszQueueName == NULL || wszComputerName == NULL)
  60. {
  61. return MQ_ERROR_INVALID_PARAMETER;
  62. }
  63.  
  64. // Create a direct format name.
  65. WCHAR * wszFormatName = NULL;
  66. DWORD dwFormatNameLength = 0;
  67. dwFormatNameLength = wcslen(wszQueueName) + wcslen(wszComputerName) + 12;
  68. wszFormatName = new WCHAR[dwFormatNameLength];
  69. if (wszFormatName == NULL)
  70. {
  71. return MQ_ERROR_INSUFFICIENT_RESOURCES;
  72. }
  73. memset(wszFormatName, 0, dwFormatNameLength);
  74. // ************************************
  75. // You must concatenate "DIRECT=OS:", wszComputerName, "",
  76. // and wszQueueName into the wszFormatName buffer.
  77. // wszFormatName = "DIRECT=OS:" + wszComputerName + "" +
  78. // wszQueueName
  79. // If the format name is too long for the buffer, return FALSE.
  80. // ************************************
  81.  
  82. // Open the queue with receive access.
  83. hr = MQOpenQueue(
  84. wszFormatName, // Format name of the queue
  85. MQ_RECEIVE_ACCESS, // Access mode
  86. MQ_DENY_NONE, // Share mode
  87. &hQueue // OUT: Queue handle
  88. );
  89. // Free the memory that was allocated for the format name string.
  90. if (wszFormatName)
  91. {
  92. delete [] wszFormatName;
  93. }
  94.  
  95. // Handle any error returned by MQOpenQueue.
  96. if (FAILED(hr))
  97. {
  98. return hr;
  99. }
  100.  
  101. for ( ; ; )
  102. {
  103. aMsgPropVar[0].ulVal = MQ_MAX_MSG_LABEL_LEN;
  104. hr = MQReceiveMessage(
  105. hQueue, // Queue handle
  106. 1000, // Max time to (msec) to receive the message
  107. MQ_ACTION_RECEIVE, // Receive action
  108. &msgprops, // Message property structure
  109. NULL, // No OVERLAPPED structure
  110. NULL, // No callback function
  111. NULL, // No cursor handle
  112. MQ_NO_TRANSACTION // Not in a transaction
  113. );
  114.  
  115. if (hr == MQ_ERROR_BUFFER_OVERFLOW)
  116. {
  117. ulBufferSize = aMsgPropVar[2].ulVal*sizeof(UCHAR);
  118. pucBodyBuffer = (UCHAR*)realloc(pucBodyBuffer, ulBufferSize);
  119. if (pucBodyBuffer == NULL)
  120. {
  121. return MQ_ERROR_INSUFFICIENT_RESOURCES;
  122. }
  123. memset(pucBodyBuffer, 0, ulBufferSize);
  124. aMsgPropVar[3].caub.pElems = (UCHAR*)pucBodyBuffer;
  125. aMsgPropVar[3].caub.cElems = ulBufferSize;
  126. continue;
  127. }
  128.  
  129. if (FAILED(hr))
  130. {
  131. wprintf(L"No messages. Closing queuen");
  132. break;
  133. }
  134.  
  135. // If the message contains a label, print it.
  136. if (msgprops.aPropVar[0].ulVal == 0)
  137. {
  138. wprintf(L"Removed message from queue.n");
  139. }
  140. else
  141. {
  142. wprintf(L"Removed message '%s' from queue.n", wszLabelBuffer);
  143. }
  144.  
  145. // If the message body is a string, display it.
  146. if (msgprops.aPropVar[4].ulVal == VT_BSTR)
  147. {
  148. wprintf(L"Body: %s", (WCHAR*)pucBodyBuffer);
  149. wprintf(L"n");
  150. }
  151. }
  152.  
  153. // Close the queue and free the memory allocated for the body buffer.
  154. hr = MQCloseQueue(hQueue);
  155. free(pucBodyBuffer);
  156.  
  157. return hr;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement