Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
4.9 KiB

  1. #include "msmqmon.h"
  2. const int NUMBEROFPROPERTIES = 5;
  3. MSMQMon::MSMQMon()
  4. {
  5. ZeroMemory( szQueueName, sizeof szQueueName );
  6. hOpenQueue = NULL;
  7. dwQueueAccessType = MQ_PEEK_ACCESS; //default queue access type to peeking
  8. dwMsgWaitTime = 1;
  9. }
  10. MSMQMon::MSMQMon( TCHAR *szQueueToMonitor)
  11. {
  12. ZeroMemory( szQueueName, sizeof szQueueName );
  13. hOpenQueue = NULL;
  14. dwMsgWaitTime = 1;
  15. dwQueueAccessType = MQ_PEEK_ACCESS; //default queue access type to peeking
  16. StringCbCopy( szQueueName, sizeof szQueueName, szQueueToMonitor );
  17. }
  18. MSMQMon::~MSMQMon(void)
  19. {
  20. }
  21. void MSMQMon::DisplayCurrentQueue( TCHAR *szUserVar )
  22. {
  23. _tprintf( _T("Current queue: %s\n"), szQueueName );
  24. }
  25. void MSMQMon::SetMessageWaitTime( DWORD dwNewWaitTime )
  26. {
  27. if ( 0 >= dwNewWaitTime )
  28. dwMsgWaitTime = dwNewWaitTime;
  29. }
  30. HRESULT MSMQMon::ConnectToQueue( DWORD constAccessType )
  31. {
  32. dwQueueAccessType = constAccessType;
  33. return ( ConnectToQueue() );
  34. }
  35. HRESULT MSMQMon::ConnectToQueue( void )
  36. {
  37. TCHAR szConnectString[256];
  38. HRESULT hResult = MQ_OK;
  39. hResult = StringCbCopy( szConnectString, sizeof szConnectString, _T("DIRECT=OS:") );
  40. if ( SUCCEEDED( hResult ) )
  41. {
  42. hResult = StringCbCat ( szConnectString, sizeof szConnectString, szQueueName );
  43. if SUCCEEDED( hResult )
  44. {
  45. hResult = MQOpenQueue( (LPCWSTR)szConnectString, dwQueueAccessType, MQ_DENY_NONE, &hOpenQueue );
  46. }
  47. }
  48. return ( hResult );
  49. }
  50. HRESULT MSMQMon::CloseOpenQueue( void )
  51. {
  52. return ( MQCloseQueue( hOpenQueue ) );
  53. }
  54. DWORD MSMQMon::CountMessagesInQueue( int *count )
  55. {
  56. HRESULT hResult; //MSMQ function return results
  57. MQMSGPROPS mqProperties;
  58. HANDLE hQueueCursor;
  59. //initialize the structure with junk, we aren't reading messages, so it doesn't matter
  60. mqProperties.cProp = 0;
  61. mqProperties.aPropID = NULL;
  62. mqProperties.aStatus = NULL;
  63. mqProperties.aPropVar = NULL;
  64. *count = 0;
  65. hResult = MQCreateCursor( hOpenQueue, &hQueueCursor );
  66. if( MQ_OK != hResult )
  67. return hResult;
  68. hResult = MQReceiveMessage ( hOpenQueue,
  69. dwMsgWaitTime, //amount of time to wait for a message (MS)
  70. MQ_ACTION_PEEK_CURRENT,
  71. &mqProperties,
  72. NULL, //overlapped structure
  73. NULL, //callback
  74. hQueueCursor, //cursor
  75. MQ_NO_TRANSACTION
  76. );
  77. if ( MQ_OK == hResult )
  78. {
  79. (*count)++;
  80. do
  81. {
  82. hResult = MQReceiveMessage(hOpenQueue,
  83. dwMsgWaitTime,
  84. MQ_ACTION_PEEK_NEXT,
  85. &mqProperties,
  86. NULL,
  87. NULL,
  88. hQueueCursor,
  89. MQ_NO_TRANSACTION
  90. );
  91. if (FAILED(hResult))
  92. {
  93. break;
  94. }
  95. (*count)++;
  96. } while (SUCCEEDED(hResult));
  97. MQCloseCursor( hQueueCursor );
  98. return MQ_OK;
  99. }
  100. else
  101. {
  102. MQCloseCursor( hQueueCursor );
  103. return hResult;
  104. }
  105. }
  106. //This function was borrwed from the ISAPI dll, and modified slighly to fit here in this app.
  107. //if you have a problem with this, then, go buy a bridge.
  108. BOOL MSMQMon::SendQueueMessage( void )
  109. {
  110. MQMSGPROPS msgProps;
  111. MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
  112. MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
  113. HRESULT aMsgStatus[NUMBEROFPROPERTIES];
  114. DWORD cPropId = 0;
  115. BOOL Status = TRUE;
  116. HRESULT hResult = S_OK;
  117. char szGuid[512];
  118. char szPath[512];
  119. TCHAR szMessageTitle[] = _T("This is a test message title");
  120. TCHAR szMessageBody[] = _T("This is a test message body");
  121. aMsgPropId [cPropId] = PROPID_M_LABEL; // Property ID.
  122. aMsgPropVar[cPropId].vt = VT_LPWSTR; // Type indicator.
  123. aMsgPropVar[cPropId].pwszVal = szMessageTitle; // The message label.
  124. cPropId++;
  125. aMsgPropId [cPropId] = PROPID_M_BODY;
  126. aMsgPropVar [cPropId].vt = VT_VECTOR|VT_UI1;
  127. aMsgPropVar [cPropId].caub.pElems = (LPBYTE) szMessageBody;
  128. aMsgPropVar [cPropId].caub.cElems = (DWORD) sizeof szMessageBody;
  129. cPropId++;
  130. aMsgPropId [cPropId] = PROPID_M_BODY_TYPE;
  131. aMsgPropVar[cPropId].vt = VT_UI4;
  132. aMsgPropVar[cPropId].ulVal = (DWORD) VT_BSTR;
  133. cPropId++;
  134. // Initialize the MQMSGPROPS structure.
  135. msgProps.cProp = cPropId;
  136. msgProps.aPropID = aMsgPropId;
  137. msgProps.aPropVar = aMsgPropVar;
  138. msgProps.aStatus = aMsgStatus;
  139. //
  140. // Send it
  141. //
  142. hResult = MQSendMessage(
  143. hOpenQueue, // Queue handle.
  144. &msgProps, // Message property structure.
  145. MQ_NO_TRANSACTION // No transaction.
  146. );
  147. if (FAILED(hResult))
  148. {
  149. Status = FALSE;
  150. }
  151. return Status;
  152. }