Source code of Windows XP (NT5)
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.

1842 lines
58 KiB

  1. /* ************************************************************************ */
  2. /* */
  3. /* Main file of the application MQ API test. */
  4. /* */
  5. /* ************************************************************************ */
  6. //
  7. // MainFrm.cpp : implementation of the CMainFrame class
  8. //
  9. //=--------------------------------------------------------------------------=
  10. // Copyright 1997-1999 Microsoft Corporation. All Rights Reserved.
  11. //
  12. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  13. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  14. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  15. // PARTICULAR PURPOSE.
  16. //=--------------------------------------------------------------------------=
  17. #include "stdafx.h"
  18. #include "MQApitst.h"
  19. #include <afxtempl.h>
  20. #include "MainFrm.h"
  21. #include "CrQDlg.h"
  22. #include "DelQDlg.h"
  23. #include "OpenQDlg.h"
  24. #include "ClosQDlg.h"
  25. #include "SendMDlg.h"
  26. #include "RecvMDlg.h"
  27. #include "RecWDlg.h"
  28. #include "LocatDlg.h"
  29. #ifdef _DEBUG
  30. #define new DEBUG_NEW
  31. #undef THIS_FILE
  32. static char THIS_FILE[] = __FILE__;
  33. #endif
  34. LPSTR UnicodeStringToAnsiString(LPCWSTR lpcsUnicode)
  35. {
  36. LPSTR lpAnsiString = NULL;
  37. if (lpcsUnicode)
  38. {
  39. DWORD dwSize = wcstombs(NULL, lpcsUnicode, 0);
  40. lpAnsiString = new char[dwSize+1];
  41. size_t rc = wcstombs(lpAnsiString, lpcsUnicode, dwSize);
  42. ASSERT(rc != (size_t)(-1));
  43. lpAnsiString[dwSize] = '\0';
  44. }
  45. return lpAnsiString;
  46. }
  47. void AnsiStringToUnicode(LPWSTR lpsUnicode, LPSTR lpsAnsi, DWORD nSize)
  48. {
  49. if (lpsUnicode == NULL)
  50. {
  51. return;
  52. }
  53. ASSERT(lpsAnsi != NULL);
  54. size_t rc = mbstowcs(lpsUnicode, lpsAnsi, nSize);
  55. ASSERT(rc != (size_t)(-1));
  56. if (lpsUnicode[nSize-1] != L'\0')
  57. lpsUnicode[nSize] = L'\0';
  58. }
  59. #ifdef UNICODE
  60. #define _mqscpy(dest, src) wcscpy(dest, src)
  61. #else
  62. #define _mqscpy(dest, src) AnsiStringToUnicode(dest, src, _tcslen(src)+1)
  63. #endif
  64. BOOL GetTextualSid(
  65. PSID pSid, // binary SID
  66. LPTSTR TextualSID, // buffer for textual representation of SID
  67. LPDWORD dwBufferLen // required/provided TextualSid buffersize
  68. )
  69. {
  70. PSID_IDENTIFIER_AUTHORITY psia;
  71. DWORD dwSubAuthorities;
  72. DWORD dwSidRev=SID_REVISION;
  73. DWORD dwCounter;
  74. DWORD dwSidSize;
  75. // obtain SidIdentifierAuthority
  76. psia=&((SID *)pSid)->IdentifierAuthority;
  77. // obtain sidsubauthority count
  78. dwSubAuthorities=(DWORD)((SID *)pSid)->SubAuthorityCount;
  79. //
  80. // compute buffer length
  81. // S-SID_REVISION- + identifierauthority- + subauthorities- + NULL
  82. //
  83. dwSidSize = (15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);
  84. //
  85. // check provided buffer length.
  86. // If not large enough, indicate proper size and setlasterror
  87. //
  88. if (*dwBufferLen < dwSidSize)
  89. {
  90. *dwBufferLen = dwSidSize;
  91. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  92. return FALSE;
  93. }
  94. //
  95. // prepare S-SID_REVISION-
  96. //
  97. TextualSID += _stprintf(TextualSID, TEXT("S-%lu-"), dwSidRev );
  98. //
  99. // prepare SidIdentifierAuthority
  100. //
  101. if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
  102. {
  103. TextualSID += _stprintf(TextualSID,
  104. TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
  105. (USHORT)psia->Value[0],
  106. (USHORT)psia->Value[1],
  107. (USHORT)psia->Value[2],
  108. (USHORT)psia->Value[3],
  109. (USHORT)psia->Value[4],
  110. (USHORT)psia->Value[5]);
  111. }
  112. else
  113. {
  114. TextualSID += _stprintf(TextualSID, TEXT("%lu"),
  115. (ULONG)(psia->Value[5] ) +
  116. (ULONG)(psia->Value[4] << 8) +
  117. (ULONG)(psia->Value[3] << 16) +
  118. (ULONG)(psia->Value[2] << 24) );
  119. }
  120. //
  121. // loop through SidSubAuthorities
  122. //
  123. for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++)
  124. {
  125. TextualSID += _stprintf(TextualSID, TEXT("-%lu"),
  126. ((SID *)pSid)->SubAuthority[ dwCounter] );
  127. }
  128. return TRUE;
  129. }
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CMainFrame
  132. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  133. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  134. //{{AFX_MSG_MAP(CMainFrame)
  135. ON_WM_CREATE()
  136. ON_COMMAND(ID_API_CREATE_QUEUE, OnApiCreateQueue)
  137. ON_COMMAND(ID_API_DELETE_QUEUE, OnApiDeleteQueue)
  138. ON_COMMAND(ID_API_OPEN_QUEUE, OnApiOpenQueue)
  139. ON_COMMAND(ID_API_CLOSE_QUEUE, OnApiCloseQueue)
  140. ON_COMMAND(ID_API_SEND_MESSAGE, OnApiSendMessage)
  141. ON_COMMAND(ID_API_RECEIVE_MESSAGE, OnApiReceiveMessage)
  142. ON_COMMAND(ID_API_LOCATE, OnApiLocate)
  143. //}}AFX_MSG_MAP
  144. END_MESSAGE_MAP()
  145. static UINT indicators[] =
  146. {
  147. ID_SEPARATOR, // status line indicator
  148. ID_INDICATOR_CAPS,
  149. ID_INDICATOR_NUM,
  150. ID_INDICATOR_SCRL,
  151. };
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CMainFrame construction/destruction
  154. CMainFrame::CMainFrame()
  155. {
  156. // TODO: add member initialization code here.
  157. }
  158. CMainFrame::~CMainFrame()
  159. {
  160. }
  161. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  162. {
  163. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  164. return -1;
  165. if (!m_wndToolBar.Create(this) ||
  166. !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  167. {
  168. TRACE0("Failed to create toolbar\n");
  169. return -1; // fail to create
  170. }
  171. if (!m_wndStatusBar.Create(this) ||
  172. !m_wndStatusBar.SetIndicators(indicators,
  173. sizeof(indicators)/sizeof(UINT)))
  174. {
  175. TRACE0("Failed to create status bar\n");
  176. return -1; // fail to create
  177. }
  178. // TODO: Remove this if you don't want tool tips or a resizeable toolbar
  179. m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  180. CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  181. // TODO: Delete these three lines if you don't want the toolbar to
  182. // be dockable
  183. m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  184. EnableDocking(CBRS_ALIGN_ANY);
  185. DockControlBar(&m_wndToolBar);
  186. return 0;
  187. }
  188. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  189. {
  190. // TODO: Modify the Window class or styles here by modifying
  191. // the CREATESTRUCT cs
  192. return CFrameWnd::PreCreateWindow(cs);
  193. }
  194. /////////////////////////////////////////////////////////////////////////////
  195. // CMainFrame diagnostics
  196. #ifdef _DEBUG
  197. void CMainFrame::AssertValid() const
  198. {
  199. CFrameWnd::AssertValid();
  200. }
  201. void CMainFrame::Dump(CDumpContext& dc) const
  202. {
  203. CFrameWnd::Dump(dc);
  204. }
  205. #endif //_DEBUG
  206. /////////////////////////////////////////////////////////////////////////////
  207. // CMainFrame message handlers
  208. #define MAXINDEX 31
  209. /* ************************************************************************ */
  210. /* RemoveFromPathNameArray */
  211. /* ************************************************************************ */
  212. /* This function goes through the PathName Array and compares the given */
  213. /* PathName to the PathName's of the items in the array. */
  214. /* If a match is found the item is removed from the array and the function */
  215. /* returns a pointer to the item, otherwise a NULL pointer is returned. */
  216. /* ************************************************************************ */
  217. ARRAYQ* CMainFrame::RemoveFromPathNameArray(TCHAR szPathName[MAX_Q_PATHNAME_LEN])
  218. {
  219. int Index;
  220. int MaxIndex = m_PathNameArray.GetSize();
  221. ARRAYQ* pQueue;
  222. //
  223. // Loop through the PathName array.
  224. //
  225. for (Index=0; Index<MaxIndex; Index++)
  226. {
  227. if (_tcscmp(szPathName, m_PathNameArray[Index]->szPathName) == 0)
  228. {
  229. //
  230. // Found a match.
  231. //
  232. pQueue = m_PathNameArray[Index];
  233. m_PathNameArray.RemoveAt(Index);
  234. return pQueue;
  235. }
  236. }
  237. return NULL; // ERROR - no match was found.
  238. }
  239. /* ************************************************************************ */
  240. /* CleanPathNameArray */
  241. /* ************************************************************************ */
  242. /* This function goes through the PathName array and deletes all the items */
  243. /* in it. the function frees the allocated memory. */
  244. /* ************************************************************************ */
  245. void CMainFrame::CleanPathNameArray()
  246. {
  247. ARRAYQ* pQueue;
  248. while (m_PathNameArray.GetSize() > 0)
  249. {
  250. pQueue = m_PathNameArray[0];
  251. m_PathNameArray.RemoveAt(0);
  252. delete pQueue;
  253. }
  254. }
  255. /* ************************************************************************ */
  256. /* RemoveFromOpenedQueuePathNameArray */
  257. /* ************************************************************************ */
  258. /* This function goes through the OpenedPathName Array and compares the */
  259. /* given PathName to the PathName's of the items in the array. */
  260. /* If a match is found the item is removed from the array and the function */
  261. /* returns a pointer to the item, otherwise a NULL pointer is returned. */
  262. /* ************************************************************************ */
  263. ARRAYQ* CMainFrame::RemoveFromOpenedQueuePathNameArray(TCHAR szPathName[MAX_Q_PATHNAME_LEN])
  264. {
  265. int Index;
  266. int MaxIndex = m_OpenedQueuePathNameArray.GetSize();
  267. ARRAYQ* pQueue;
  268. //
  269. // Loop through the OpenedPathName array.
  270. //
  271. for (Index=0; Index<MaxIndex; Index++)
  272. {
  273. if (_tcscmp(szPathName, m_OpenedQueuePathNameArray[Index]->szPathName) == 0)
  274. {
  275. //
  276. // Found a match.
  277. //
  278. pQueue = m_OpenedQueuePathNameArray[Index];
  279. m_OpenedQueuePathNameArray.RemoveAt(Index);
  280. return pQueue;
  281. }
  282. }
  283. return NULL; // ERROR - no match was found.
  284. }
  285. /* ************************************************************************ */
  286. /* IsOpenedQueueArrayEmpty */
  287. /* ************************************************************************ */
  288. /* This function checks if the size of the OpenedPathName array is zero or */
  289. /* less and if so it returns TRUE otherwise it returns FALSE. */
  290. /* ************************************************************************ */
  291. BOOL CMainFrame::IsOpenedQueueArrayEmpty()
  292. {
  293. if (m_OpenedQueuePathNameArray.GetSize() <= 0)
  294. {
  295. return TRUE;
  296. }
  297. else
  298. {
  299. return FALSE;
  300. }
  301. }
  302. /* ************************************************************************ */
  303. /* MoveToOpenedQueuePathNameArray */
  304. /* ************************************************************************ */
  305. /* This function moves an item from the PathName array to the */
  306. /* OpenedPathName array. also it updates the hadle and the access rights */
  307. /* to the moved queue. */
  308. /* ************************************************************************ */
  309. void CMainFrame::MoveToOpenedQueuePathNameArray(TCHAR szPathName[MAX_Q_PATHNAME_LEN],
  310. QUEUEHANDLE hQueue, DWORD dwAccess)
  311. {
  312. ARRAYQ* pQueue;
  313. pQueue = RemoveFromPathNameArray(szPathName);
  314. pQueue->hHandle = hQueue; // add Queue Handle.
  315. pQueue->dwAccess = dwAccess; // add Queue Access rights.
  316. Add2OpenedQueuePathNameArray(pQueue);
  317. }
  318. /* ************************************************************************ */
  319. /* MoveToPathNameArray */
  320. /* ************************************************************************ */
  321. /* This function moves an item from the OpenedPathName array to the */
  322. /* PathName array. */
  323. /* ************************************************************************ */
  324. void CMainFrame::MoveToPathNameArray(TCHAR szPathName[MAX_Q_PATHNAME_LEN])
  325. {
  326. ARRAYQ* pQueue;
  327. pQueue = RemoveFromOpenedQueuePathNameArray(szPathName);
  328. Add2PathNameArray(pQueue);
  329. }
  330. /* ************************************************************************ */
  331. /* UpdatePathNameArrays */
  332. /* ************************************************************************ */
  333. /* This function goes through the Opened Queue PathName array and for every */
  334. /* item in it, it checkes if the item is found in the PathName array as */
  335. /* well, if so the function removes the item from the PathName array. */
  336. /* ************************************************************************ */
  337. void CMainFrame::UpdatePathNameArrays()
  338. {
  339. int PathNameIndex;
  340. int OpenedPathNameIndex;
  341. int MaxPathNameIndex = m_PathNameArray.GetSize();
  342. int MaxOpenedPathNameIndex = m_OpenedQueuePathNameArray.GetSize();
  343. ARRAYQ* pQueue;
  344. //
  345. // Loop through the OpenedPathName array.
  346. //
  347. for (OpenedPathNameIndex=0; OpenedPathNameIndex<MaxOpenedPathNameIndex; OpenedPathNameIndex++)
  348. {
  349. for (PathNameIndex=0; PathNameIndex<MaxPathNameIndex; PathNameIndex++)
  350. {
  351. if (_tcscmp(m_OpenedQueuePathNameArray[OpenedPathNameIndex]->szPathName,
  352. m_PathNameArray[PathNameIndex]->szPathName) == 0)
  353. {
  354. //
  355. // Found a match, remove it from PathName Array.
  356. //
  357. pQueue = m_PathNameArray[PathNameIndex];
  358. m_PathNameArray.RemoveAt(PathNameIndex);
  359. delete pQueue;
  360. //
  361. // get out of inner for loop.
  362. //
  363. break;
  364. }
  365. }
  366. }
  367. }
  368. /* ************************************************************************ */
  369. /* GetQueueHandle */
  370. /* ************************************************************************ */
  371. /* This function goes through the OpenedPathName array and retrieve the */
  372. /* Handle to the queue which matches the given PathName. If no match was */
  373. /* found the function returns FALSE. */
  374. /* ************************************************************************ */
  375. BOOL CMainFrame::GetQueueHandle(TCHAR szPathName[MAX_Q_PATHNAME_LEN],
  376. QUEUEHANDLE* phClosedQueueHandle)
  377. {
  378. int Index;
  379. int MaxIndex = m_OpenedQueuePathNameArray.GetSize();
  380. ARRAYQ* pQueue;
  381. //
  382. // Loop through the OpenedPathName array.
  383. //
  384. for (Index=0; Index<MaxIndex; Index++)
  385. {
  386. if (_tcscmp(szPathName, m_OpenedQueuePathNameArray[Index]->szPathName) == 0)
  387. {
  388. //
  389. // Found a match.
  390. //
  391. pQueue = m_OpenedQueuePathNameArray[Index];
  392. *phClosedQueueHandle = pQueue->hHandle;
  393. return TRUE;
  394. }
  395. }
  396. return FALSE; // ERROR - no match was found.
  397. }
  398. /* ************************************************************************ */
  399. /* TranslatePathNameToFormatName */
  400. /* ************************************************************************ */
  401. /* This function goes through the PathName array and retrieve the */
  402. /* FormatName to the queue which matches the given PathName. If no match */
  403. /* was found the function returns FALSE. */
  404. /* ************************************************************************ */
  405. BOOL CMainFrame::TranslatePathNameToFormatName(TCHAR szPathName[MAX_Q_PATHNAME_LEN],
  406. TCHAR szFormatName[MAX_Q_FORMATNAME_LEN])
  407. {
  408. int Index;
  409. int MaxIndex = m_PathNameArray.GetSize();
  410. ARRAYQ* pQueue;
  411. //
  412. // Loop through the PathName array.
  413. //
  414. for (Index=0; Index<MaxIndex; Index++)
  415. {
  416. if (_tcscmp(szPathName, m_PathNameArray[Index]->szPathName) == 0)
  417. {
  418. //
  419. // Found a match.
  420. //
  421. pQueue = m_PathNameArray[Index];
  422. _tcsncpy (szFormatName, pQueue->szFormatName, MAX_Q_FORMATNAME_LEN);
  423. return TRUE;
  424. }
  425. }
  426. return FALSE; // ERROR - no match was found.
  427. }
  428. /* ************************************************************************ */
  429. /* TranslateOpenedQueuePathNameToFormatName */
  430. /* ************************************************************************ */
  431. /* This function goes through the OpenedPathName array and retrieve the */
  432. /* FormatName to the queue which matches the given PathName. If no match */
  433. /* was found the function returns FALSE. */
  434. /* ************************************************************************ */
  435. BOOL CMainFrame::TranslateOpenedQueuePathNameToFormatName(
  436. TCHAR szPathName[MAX_Q_PATHNAME_LEN],
  437. TCHAR szFormatName[MAX_Q_FORMATNAME_LEN]
  438. )
  439. {
  440. int Index;
  441. int MaxIndex = m_OpenedQueuePathNameArray.GetSize();
  442. ARRAYQ* pQueue;
  443. //
  444. // Loop through the OpenedPathName array.
  445. //
  446. for (Index=0; Index<MaxIndex; Index++)
  447. {
  448. if (_tcscmp(szPathName, m_OpenedQueuePathNameArray[Index]->szPathName) == 0)
  449. {
  450. //
  451. // Found a match.
  452. //
  453. pQueue = m_OpenedQueuePathNameArray[Index];
  454. _tcsncpy (szFormatName, pQueue->szFormatName, MAX_Q_FORMATNAME_LEN);
  455. return TRUE;
  456. }
  457. }
  458. return FALSE; // ERROR - no match was found.
  459. }
  460. /* ************************************************************************ */
  461. /* DisplayPathNameArray */
  462. /* ************************************************************************ */
  463. /* This function goes through the PathName Array and prints it to screen. */
  464. /* ************************************************************************ */
  465. void CMainFrame::DisplayPathNameArray()
  466. {
  467. int Index;
  468. int MaxIndex = m_PathNameArray.GetSize();
  469. TCHAR szMsgBuffer[BUFFERSIZE];
  470. _stprintf(szMsgBuffer, TEXT(" Located Queues Path Name :"));
  471. PrintToScreen(szMsgBuffer);
  472. //
  473. // Loop through the PathName array.
  474. //
  475. for (Index=0; Index<MaxIndex; Index++)
  476. {
  477. //
  478. // Print the PathNames.
  479. //
  480. _stprintf(szMsgBuffer, TEXT("\t%d. %s"),Index+1, m_PathNameArray[Index]->szPathName);
  481. PrintToScreen(szMsgBuffer);
  482. }
  483. }
  484. /* ************************************************************************ */
  485. /* DisplayOpenedQueuePathNameArray */
  486. /* ************************************************************************ */
  487. /* This function goes through the Opened Queues PathName Array and */
  488. /* prints it to screen. */
  489. /* ************************************************************************ */
  490. void CMainFrame::DisplayOpenedQueuePathNameArray()
  491. {
  492. int Index;
  493. int MaxIndex = m_OpenedQueuePathNameArray.GetSize();
  494. TCHAR szMsgBuffer[BUFFERSIZE];
  495. _stprintf(szMsgBuffer, TEXT(" Currently Opened Queues Path Names:"));
  496. PrintToScreen(szMsgBuffer);
  497. //
  498. // Loop through the OpenedQueuePathName array.
  499. //
  500. for (Index=0; Index<MaxIndex; Index++)
  501. {
  502. //
  503. // Print the PathNames.
  504. //
  505. _stprintf(szMsgBuffer, TEXT("\t%d. %s"),Index+1, m_OpenedQueuePathNameArray[Index]->szPathName);
  506. PrintToScreen(szMsgBuffer);
  507. }
  508. }
  509. /* ************************************************************************ */
  510. /* GetMsgClassStatus */
  511. /* ************************************************************************ */
  512. /* This function sets proper status string based on a given MQMSG class. */
  513. /* ************************************************************************ */
  514. struct
  515. {
  516. unsigned short mclass;
  517. LPTSTR pszDescription;
  518. } StringClass[] =
  519. {
  520. { MQMSG_CLASS_NORMAL, TEXT("The Message was received successfully.")},
  521. { MQMSG_CLASS_ACK_REACH_QUEUE, TEXT("The REACH QUEUE ACK Message was read successfully.")},
  522. { MQMSG_CLASS_ACK_RECEIVE, TEXT("The RECEIVE ACK Message was read successfully.")},
  523. { MQMSG_CLASS_NACK_BAD_DST_Q, TEXT("The DESTINATION QUEUE HANDLE INVALID Nack Message was read successfully.")},
  524. { MQMSG_CLASS_NACK_RECEIVE_TIMEOUT, TEXT("The TIME TO RECEIVE EXPIRED Nack Message was read successfully.")},
  525. { MQMSG_CLASS_NACK_REACH_QUEUE_TIMEOUT, TEXT("The TIME TO REACH QUEUE EXPIRED Nack Message was read successfully.")},
  526. { MQMSG_CLASS_NACK_Q_EXCEED_QUOTA, TEXT("The QUEUE IS FULL Nack Message was read successfully.")},
  527. { MQMSG_CLASS_NACK_ACCESS_DENIED, TEXT("The SENDER HAVE NO SEND ACCESS RIGHTS ON QUEUE Nack Message was read successfully.")},
  528. { MQMSG_CLASS_NACK_HOP_COUNT_EXCEEDED, TEXT("The HOP COUNT EXCEEDED Nack Message was read successfully.")},
  529. { MQMSG_CLASS_NACK_BAD_SIGNATURE, TEXT("The MESSAGE RECEIVED WITH BAD SIGNATURE Nack Message was read successfully.")},
  530. { MQMSG_CLASS_NACK_BAD_ENCRYPTION, TEXT("The MSG COULD NOT DECRYPTED Nack Message was read successfully.")},
  531. { MQMSG_CLASS_NACK_COULD_NOT_ENCRYPT, TEXT("The SOURCE QM COULD NOT ENCRYPT MSG FOR DEST QM Nack Message was read successfully.")},
  532. { 0, NULL}
  533. };
  534. void CMainFrame::ClassToString(unsigned short MsgClass,LPTSTR pszStatus)
  535. {
  536. //
  537. // loop the StringClass array to find MsgClass
  538. //
  539. DWORD dwIndex = 0;
  540. while (StringClass[dwIndex].pszDescription != NULL)
  541. {
  542. if (StringClass[dwIndex].mclass == MsgClass)
  543. {
  544. _stprintf(pszStatus,StringClass[dwIndex].pszDescription);
  545. return;
  546. }
  547. dwIndex++;
  548. }
  549. //
  550. // MsgClass not found - print default error
  551. //
  552. _stprintf(pszStatus,TEXT("The NACK (0x%X) Message was read successfully."),MsgClass);
  553. }
  554. /* ************************************************************************ */
  555. /* OnApiCreateQueue */
  556. /* ************************************************************************ */
  557. /* This function opens a dialog box and asks the user for the queue's */
  558. /* PathName and Label. Then it creates the specified queue. */
  559. /* */
  560. /* Uses: MQCreateQueue. */
  561. /* ************************************************************************ */
  562. void CMainFrame::OnApiCreateQueue()
  563. {
  564. // TODO: Add your command handler code here
  565. TCHAR szMsgBuffer[BUFFERSIZE];
  566. MQQUEUEPROPS QueueProps;
  567. MQPROPVARIANT aVariant[MAXINDEX];
  568. QUEUEPROPID aPropId[MAXINDEX];
  569. DWORD PropIdCount = 0;
  570. HRESULT hr;
  571. PSECURITY_DESCRIPTOR pSecurityDescriptor;
  572. TCHAR szPathNameBuffer[MAX_Q_PATHNAME_LEN];
  573. TCHAR szFormatNameBuffer[MAX_Q_FORMATNAME_LEN];
  574. TCHAR szLabelBuffer[MAX_Q_PATHNAME_LEN];
  575. DWORD dwFormatNameBufferLength = MAX_Q_FORMATNAME_LEN;
  576. //
  577. // Display CreateQueue Dialog.
  578. //
  579. CCreateQueueDialog CreateQueueDialog;
  580. if(CreateQueueDialog.DoModal() == IDCANCEL)
  581. {
  582. return;
  583. }
  584. CreateQueueDialog.GetPathName(szPathNameBuffer);
  585. CreateQueueDialog.GetLabel(szLabelBuffer);
  586. //
  587. // Get the input fields from the dialog box
  588. // and prepare the property array PROPVARIANT
  589. //
  590. //
  591. // Set the PROPID_Q_PATHNAME property
  592. //
  593. aPropId[PropIdCount] = PROPID_Q_PATHNAME; //PropId
  594. aVariant[PropIdCount].vt = VT_LPWSTR; //Type
  595. aVariant[PropIdCount].pwszVal = new WCHAR[MAX_Q_PATHNAME_LEN];
  596. _mqscpy(aVariant[PropIdCount].pwszVal, szPathNameBuffer); //Value
  597. PropIdCount++;
  598. //
  599. // Set the PROPID_Q_LABEL property
  600. //
  601. aPropId[PropIdCount] = PROPID_Q_LABEL; //PropId
  602. aVariant[PropIdCount].vt = VT_LPWSTR; //Type
  603. aVariant[PropIdCount].pwszVal = new WCHAR[MAX_Q_PATHNAME_LEN];
  604. _mqscpy(aVariant[PropIdCount].pwszVal, szLabelBuffer); //Value
  605. PropIdCount++;
  606. //
  607. // Set the MQEUEUPROPS structure
  608. //
  609. QueueProps.cProp = PropIdCount; //No of properties
  610. QueueProps.aPropID = aPropId; //Id of properties
  611. QueueProps.aPropVar = aVariant; //Value of properties
  612. QueueProps.aStatus = NULL; //No error reports
  613. //
  614. // No security (default)
  615. //
  616. pSecurityDescriptor = NULL;
  617. //
  618. // Create the queue
  619. //
  620. #ifdef UNICODE
  621. hr = MQCreateQueue(
  622. pSecurityDescriptor, //Security
  623. &QueueProps, //Queue properties
  624. szFormatNameBuffer, //Output: Format Name
  625. &dwFormatNameBufferLength //Output: Format Name len
  626. );
  627. #else
  628. WCHAR szwFormatNameBuffer[MAX_Q_FORMATNAME_LEN];
  629. hr = MQCreateQueue(
  630. pSecurityDescriptor, //Security
  631. &QueueProps, //Queue properties
  632. szwFormatNameBuffer, //Output: Format Name
  633. &dwFormatNameBufferLength //Output: Format Name len
  634. );
  635. if (SUCCEEDED(hr))
  636. {
  637. size_t rc =wcstombs(szFormatNameBuffer, szwFormatNameBuffer, dwFormatNameBufferLength);
  638. ASSERT(rc != (size_t)(-1));
  639. }
  640. #endif
  641. if (FAILED(hr))
  642. {
  643. //
  644. // Error - display message
  645. //
  646. _stprintf(szMsgBuffer, TEXT("MQCreateQueue failed, Error code = 0x%x."),hr);
  647. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  648. }
  649. else
  650. {
  651. //
  652. // Success - write in edit control.
  653. //
  654. _stprintf(szMsgBuffer, TEXT("The queue %s was created successfully. ( FormatName: %s )"),
  655. szPathNameBuffer, szFormatNameBuffer);
  656. PrintToScreen(szMsgBuffer);
  657. //
  658. // Add the new queue to the PathName Array.
  659. //
  660. ARRAYQ* pNewQueue = new ARRAYQ;
  661. //
  662. // Save PathName and FormatName in the ARRAYQ structure.
  663. //
  664. _tcsncpy (pNewQueue->szPathName, szPathNameBuffer, MAX_Q_PATHNAME_LEN);
  665. _tcsncpy (pNewQueue->szFormatName, szFormatNameBuffer, MAX_Q_FORMATNAME_LEN);
  666. Add2PathNameArray(pNewQueue);
  667. }
  668. //
  669. // Free allocated memory
  670. //
  671. delete aVariant[0].pwszVal;
  672. delete aVariant[1].pwszVal;
  673. }
  674. /* ************************************************************************ */
  675. /* OnApiDeleteQueue */
  676. /* ************************************************************************ */
  677. /* This function opens a dialog box and asks the user for the queue's */
  678. /* PathName. then it deletes the specified queue. */
  679. /* */
  680. /* Uses: MQDeleteQueue. */
  681. /* ************************************************************************ */
  682. void CMainFrame::OnApiDeleteQueue()
  683. {
  684. // TODO: Add your command handler code here
  685. TCHAR szPathNameBuffer[MAX_Q_PATHNAME_LEN];
  686. TCHAR szFormatNameBuffer[MAX_Q_PATHNAME_LEN];
  687. TCHAR szMsgBuffer[BUFFERSIZE];
  688. HRESULT hr;
  689. DWORD dwFormatNameBufferLength = MAX_Q_PATHNAME_LEN;
  690. CDeleteQueueDialog DeleteQueueDialog(&m_PathNameArray);
  691. //
  692. // Display DeleteQueue Dialog.
  693. //
  694. if (DeleteQueueDialog.DoModal() == IDCANCEL)
  695. {
  696. return;
  697. }
  698. DeleteQueueDialog.GetPathName(szPathNameBuffer);
  699. //
  700. // Translate the path name to format name using the ARRAYQ arrays.
  701. //
  702. if (TranslatePathNameToFormatName(szPathNameBuffer, szFormatNameBuffer) == FALSE)
  703. {
  704. //
  705. // Error - display message
  706. //
  707. _stprintf(szMsgBuffer, TEXT("Queue wasn't found"));
  708. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  709. return;
  710. }
  711. //
  712. // Delete the queue.
  713. //
  714. #ifdef UNICODE
  715. hr = MQDeleteQueue(szFormatNameBuffer); // FormatName of the Queue to be deleted.
  716. #else
  717. WCHAR szwFormatNameBuffer[MAX_Q_FORMATNAME_LEN];
  718. size_t rc = mbstowcs(szwFormatNameBuffer, szFormatNameBuffer, _tcslen(szFormatNameBuffer)+1);
  719. ASSERT(rc != (size_t)(-1));
  720. hr = MQDeleteQueue(szwFormatNameBuffer); // FormatName of the Queue to be deleted.
  721. #endif
  722. if (FAILED(hr))
  723. {
  724. //
  725. // Error - display message
  726. //
  727. _stprintf(szMsgBuffer, TEXT("MQDeleteQueue failed, Error code = 0x%x."),hr);
  728. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  729. }
  730. else
  731. {
  732. //
  733. // Success - write in edit control
  734. //
  735. _stprintf(szMsgBuffer, TEXT("The queue %s was deleted successfully."), szPathNameBuffer);
  736. PrintToScreen(szMsgBuffer);
  737. //
  738. // Delete the name from the Path Names array.
  739. //
  740. ARRAYQ* DeletedQueue = RemoveFromPathNameArray(szPathNameBuffer);
  741. if (DeletedQueue != NULL)
  742. {
  743. delete DeletedQueue;
  744. }
  745. }
  746. }
  747. /* ************************************************************************ */
  748. /* OnApiOpenQueue */
  749. /* ************************************************************************ */
  750. /* This function opens a dialog box and asks the user for the queue's */
  751. /* PathName. then it opens the specified queue. */
  752. /* */
  753. /* Uses: MQOpenQueue. */
  754. /* ************************************************************************ */
  755. void CMainFrame::OnApiOpenQueue()
  756. {
  757. // TODO: Add your command handler code here
  758. TCHAR szPathNameBuffer[MAX_Q_PATHNAME_LEN];
  759. TCHAR szFormatNameBuffer[MAX_Q_PATHNAME_LEN];
  760. TCHAR szAccessBuffer[ACCESSBUFFERSIZE];
  761. TCHAR szMsgBuffer[BUFFERSIZE];
  762. HRESULT hr;
  763. DWORD dwFormatNameBufferLength = MAX_Q_PATHNAME_LEN;
  764. DWORD dwAccess;
  765. QUEUEHANDLE hQueue;
  766. COpenQueueDialog OpenQueueDialog(&m_PathNameArray);
  767. //
  768. // Display the OpenQueue dialog.
  769. //
  770. if (OpenQueueDialog.DoModal() == IDCANCEL)
  771. {
  772. return;
  773. }
  774. OpenQueueDialog.GetPathName(szPathNameBuffer);
  775. dwAccess = OpenQueueDialog.GetAccess();
  776. //
  777. // Set the access buffer string.
  778. //
  779. switch (dwAccess)
  780. {
  781. case (MQ_RECEIVE_ACCESS | MQ_SEND_ACCESS):
  782. _tcscpy(szAccessBuffer, TEXT("MQ_RECEIVE_ACCESS, MQ_SEND_ACCESS."));
  783. break;
  784. case MQ_RECEIVE_ACCESS:
  785. _tcscpy(szAccessBuffer, TEXT("MQ_RECEIVE_ACCESS."));
  786. break;
  787. case MQ_SEND_ACCESS:
  788. _tcscpy(szAccessBuffer, TEXT("MQ_SEND_ACCESS."));
  789. break;
  790. default:
  791. _tcscpy(szAccessBuffer, TEXT("NONE."));
  792. break;
  793. }
  794. //
  795. // Translate the path name to format name using the ARRAYQ arrays.
  796. //
  797. if (TranslatePathNameToFormatName(szPathNameBuffer, szFormatNameBuffer) == FALSE)
  798. {
  799. //
  800. // Error - display message
  801. //
  802. _stprintf(szMsgBuffer, TEXT("Queue wasn't found"));
  803. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  804. return;
  805. }
  806. //
  807. // Open the queue. (no sharing)
  808. //
  809. #ifdef UNICODE
  810. hr = MQOpenQueue(
  811. szFormatNameBuffer, // Format Name of the queue to be opened.
  812. dwAccess, // Access rights to the Queue.
  813. 0, // No receive Exclusive.
  814. &hQueue // OUT: handle to the opened Queue.
  815. );
  816. #else
  817. WCHAR szwFormatNameBuffer[MAX_Q_FORMATNAME_LEN];
  818. size_t rc = mbstowcs(szwFormatNameBuffer, szFormatNameBuffer, _tcslen(szFormatNameBuffer)+1);
  819. ASSERT(rc != (size_t)(-1));
  820. hr = MQOpenQueue(
  821. szwFormatNameBuffer, // Format Name of the queue to be opened.
  822. dwAccess, // Access rights to the Queue.
  823. 0, // No receive Exclusive.
  824. &hQueue // OUT: handle to the opened Queue.
  825. );
  826. #endif
  827. if (FAILED(hr))
  828. {
  829. //
  830. // Error - display message
  831. //
  832. _stprintf(szMsgBuffer, TEXT("MQOpenQueue failed, Error code = 0x%x."),hr);
  833. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  834. }
  835. else
  836. {
  837. //
  838. // Success - write in edit control
  839. //
  840. _stprintf(szMsgBuffer,
  841. TEXT("The queue %s was opened successfully.\r\n\tQueueHandle: 0x%x\r\n\tQueue Access : %s"),
  842. szPathNameBuffer,
  843. hQueue,
  844. szAccessBuffer);
  845. PrintToScreen(szMsgBuffer);
  846. //
  847. // move the queue to the opened queues array.
  848. //
  849. MoveToOpenedQueuePathNameArray(szPathNameBuffer, hQueue, dwAccess);
  850. }
  851. }
  852. /* ************************************************************************ */
  853. /* OnApiCloseQueue */
  854. /* ************************************************************************ */
  855. /* This function opens a dialog box and asks the user for the queue's */
  856. /* PathName. then it closes the specified queue. */
  857. /* */
  858. /* Uses: MQCloseQueue. */
  859. /* ************************************************************************ */
  860. void CMainFrame::OnApiCloseQueue()
  861. {
  862. // TODO: Add your command handler code here
  863. TCHAR szPathNameBuffer[MAX_Q_PATHNAME_LEN];
  864. TCHAR szMsgBuffer[BUFFERSIZE];
  865. HRESULT hr;
  866. DWORD dwFormatNameBufferLength = MAX_Q_PATHNAME_LEN;
  867. QUEUEHANDLE hClosedQueueHandle;
  868. //
  869. // Display CloseQueue Dialog.
  870. //
  871. CCloseQueueDialog CloseQueueDialog(&m_OpenedQueuePathNameArray);
  872. if (CloseQueueDialog.DoModal() == IDCANCEL)
  873. {
  874. return;
  875. }
  876. CloseQueueDialog.GetPathName(szPathNameBuffer);
  877. //
  878. // Get the closed queue handle.
  879. //
  880. if (GetQueueHandle(szPathNameBuffer, &hClosedQueueHandle) == FALSE)
  881. {
  882. //
  883. // Error - display message
  884. //
  885. _stprintf(szMsgBuffer, TEXT("The Queue couldn't be closed since it was not opened before."));
  886. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  887. return;
  888. }
  889. //
  890. // Close the queue.
  891. //
  892. hr = MQCloseQueue(hClosedQueueHandle); // the handle of the Queue to be closed.
  893. if (FAILED(hr))
  894. {
  895. //
  896. // Error - display message
  897. //
  898. _stprintf(szMsgBuffer, TEXT("MQCloseQueue failed, Error code = 0x%x."),hr);
  899. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  900. }
  901. else
  902. {
  903. //
  904. // Success - write in edit control
  905. //
  906. _stprintf(szMsgBuffer, TEXT("The queue %s was closed successfully."), szPathNameBuffer);
  907. PrintToScreen(szMsgBuffer);
  908. //
  909. // Move the queue form the opened queues array to the path name array.
  910. //
  911. MoveToPathNameArray(szPathNameBuffer);
  912. }
  913. }
  914. /* ************************************************************************ */
  915. /* OnApiSendMessage */
  916. /* ************************************************************************ */
  917. /* This function opens a dialog box and asks the user for the queue's */
  918. /* PathName and some message properties. Then it sends the message to the */
  919. /* specified queue. */
  920. /* */
  921. /* Uses: MQSendMessage. */
  922. /* ************************************************************************ */
  923. //
  924. // two static buffers to hold the last message body and label for the next time.
  925. //
  926. TCHAR szLastMessageBody[BUFFERSIZE];
  927. TCHAR szLastMessageLabel[BUFFERSIZE];
  928. void CMainFrame::OnApiSendMessage()
  929. {
  930. // TODO: Add your command handler code here
  931. TCHAR szPathNameBuffer[MAX_Q_PATHNAME_LEN];
  932. TCHAR szAdminPathNameBuffer[MAX_Q_PATHNAME_LEN];
  933. TCHAR szAdminFormatNameBuffer[MAX_Q_FORMATNAME_LEN];
  934. TCHAR szMsgBuffer[BUFFERSIZE];
  935. MQMSGPROPS MsgProps;
  936. MQPROPVARIANT aVariant[MAXINDEX];
  937. MSGPROPID aPropId[MAXINDEX];
  938. DWORD PropIdCount = 0;
  939. HRESULT hr;
  940. unsigned char bPriority;
  941. unsigned char bDelivery;
  942. unsigned char bJournal;
  943. unsigned char bDeadLetter;
  944. unsigned char bAuthenticated;
  945. unsigned char bEncrypted;
  946. unsigned char bAcknowledge;
  947. WCHAR szMessageBodyBuffer [BUFFERSIZE];
  948. WCHAR szMessageLabelBuffer[BUFFERSIZE];
  949. DWORD dwTimeToReachQueue;
  950. DWORD dwTimeToBeReceived;
  951. QUEUEHANDLE hQueue;
  952. CSendMessageDialog SendMessageDialog(&m_OpenedQueuePathNameArray);
  953. //
  954. // Display the SendMessage dialog.
  955. //
  956. if (SendMessageDialog.DoModal() == IDCANCEL)
  957. {
  958. return;
  959. }
  960. //
  961. // Retrieve the properties from the dialog box.
  962. //
  963. SendMessageDialog.GetPathName(szPathNameBuffer);
  964. SendMessageDialog.GetAdminPathName(szAdminPathNameBuffer);
  965. bPriority = SendMessageDialog.GetPriority();
  966. bDelivery = SendMessageDialog.GetDelivery();
  967. bJournal = SendMessageDialog.GetJournal();
  968. bDeadLetter = SendMessageDialog.GetDeadLetter();
  969. bAuthenticated = SendMessageDialog.GetAuthenticated();
  970. bEncrypted = SendMessageDialog.GetEncrypted();
  971. bAcknowledge = SendMessageDialog.GetAcknowledge();
  972. SendMessageDialog.GetMessageBody(szLastMessageBody);
  973. SendMessageDialog.GetMessageLabel(szLastMessageLabel);
  974. dwTimeToReachQueue = SendMessageDialog.GetTimeToReachQueue();
  975. dwTimeToBeReceived = SendMessageDialog.GetTimeToBeReceived();
  976. //
  977. // Update the Last message properties.
  978. //
  979. _mqscpy(szMessageBodyBuffer, szLastMessageBody);
  980. _mqscpy(szMessageLabelBuffer, szLastMessageLabel);
  981. //
  982. // Get the target queue handle.
  983. //
  984. if (GetQueueHandle(szPathNameBuffer, &hQueue) == FALSE)
  985. {
  986. //
  987. // Error - display message
  988. //
  989. _stprintf(szMsgBuffer, TEXT("GetQueueHandle failed. Queue not opened yet."));
  990. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  991. return;
  992. }
  993. //
  994. // Get the admin queue FormatName.
  995. //
  996. if (TranslateOpenedQueuePathNameToFormatName(szAdminPathNameBuffer, szAdminFormatNameBuffer) == FALSE)
  997. {
  998. //
  999. // Error - display message
  1000. //
  1001. _stprintf(szMsgBuffer, TEXT("TranslatePathNameToFormatName failed, Queue has not been opened yet."));
  1002. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1003. return;
  1004. }
  1005. //
  1006. // prepare the property array PROPVARIANT.
  1007. //
  1008. //
  1009. // Set the PROPID_M_PRIORITY property.
  1010. //
  1011. aPropId[PropIdCount] = PROPID_M_PRIORITY; //PropId
  1012. aVariant[PropIdCount].vt = VT_UI1; //Type
  1013. aVariant[PropIdCount].bVal = bPriority; //Value
  1014. PropIdCount++;
  1015. //
  1016. // Set the PROPID_M_DELIVERY property.
  1017. //
  1018. aPropId[PropIdCount] = PROPID_M_DELIVERY; //PropId
  1019. aVariant[PropIdCount].vt = VT_UI1; //Type
  1020. aVariant[PropIdCount].bVal = bDelivery; //Value
  1021. PropIdCount++;
  1022. //
  1023. // Set the PROPID_M_ACKNOWLEDGE property.
  1024. //
  1025. aPropId[PropIdCount] = PROPID_M_ACKNOWLEDGE; //PropId
  1026. aVariant[PropIdCount].vt = VT_UI1; //Type
  1027. aVariant[PropIdCount].bVal = bAcknowledge; //Value
  1028. PropIdCount++;
  1029. //
  1030. // Set the PROPID_M_BODY property.
  1031. //
  1032. aPropId[PropIdCount] = PROPID_M_BODY; //PropId
  1033. aVariant[PropIdCount].vt = VT_VECTOR|VT_UI1; //Type
  1034. aVariant[PropIdCount].caub.cElems =
  1035. (wcslen(szMessageBodyBuffer) + 1) * sizeof(WCHAR); //Value
  1036. aVariant[PropIdCount].caub.pElems = (unsigned char *)szMessageBodyBuffer;
  1037. PropIdCount++;
  1038. //
  1039. // Set the PROPID_M_LABEL property.
  1040. //
  1041. aPropId[PropIdCount] = PROPID_M_LABEL; //PropId
  1042. aVariant[PropIdCount].vt = VT_LPWSTR; //Type
  1043. aVariant[PropIdCount].pwszVal = szMessageLabelBuffer; //Value
  1044. PropIdCount++;
  1045. //
  1046. // Set the PROPID_M_TIME_TO_REACH_QUEUE property.
  1047. //
  1048. aPropId[PropIdCount] = PROPID_M_TIME_TO_REACH_QUEUE; //PropId
  1049. aVariant[PropIdCount].vt = VT_UI4; //Type
  1050. aVariant[PropIdCount].ulVal = dwTimeToReachQueue; //Value
  1051. PropIdCount++;
  1052. //
  1053. // Set the PROPID_M_TIME_TO_BE_RECEIVED property.
  1054. //
  1055. aPropId[PropIdCount] = PROPID_M_TIME_TO_BE_RECEIVED; //PropId
  1056. aVariant[PropIdCount].vt = VT_UI4; //Type
  1057. aVariant[PropIdCount].ulVal = dwTimeToBeReceived; //Value
  1058. PropIdCount++;
  1059. if (bJournal || bDeadLetter)
  1060. {
  1061. //
  1062. // Set the PROPID_M_JOURNAL property.
  1063. //
  1064. aPropId[PropIdCount] = PROPID_M_JOURNAL; //PropId
  1065. aVariant[PropIdCount].vt = VT_UI1; //Type
  1066. if (bJournal)
  1067. aVariant[PropIdCount].bVal = MQMSG_JOURNAL;
  1068. else
  1069. aVariant[PropIdCount].bVal = 0;
  1070. if (bDeadLetter)
  1071. aVariant[PropIdCount].bVal |= MQMSG_DEADLETTER;
  1072. PropIdCount++;
  1073. }
  1074. if (bAuthenticated)
  1075. {
  1076. //
  1077. // Set the PROPID_M_AUTH_LEVEL property.
  1078. //
  1079. aPropId[PropIdCount] = PROPID_M_AUTH_LEVEL; //PropId
  1080. aVariant[PropIdCount].vt = VT_UI4; //Type
  1081. aVariant[PropIdCount].ulVal = MQMSG_AUTH_LEVEL_ALWAYS; //Value
  1082. PropIdCount++;
  1083. }
  1084. if (bEncrypted)
  1085. {
  1086. //
  1087. // Set the PROPID_M_ENCRYPTION_ALG property.
  1088. //
  1089. aPropId[PropIdCount] = PROPID_M_PRIV_LEVEL; //PropId
  1090. aVariant[PropIdCount].vt = VT_UI4; //Type
  1091. aVariant[PropIdCount].ulVal = MQMSG_PRIV_LEVEL_BODY; //Value
  1092. PropIdCount++;
  1093. }
  1094. //
  1095. // Set the PROPID_M_ADMIN_QUEUE property.
  1096. //
  1097. aPropId[PropIdCount] = PROPID_M_ADMIN_QUEUE; //PropId
  1098. aVariant[PropIdCount].vt = VT_LPWSTR; //Type
  1099. #ifdef UNICODE
  1100. aVariant[PropIdCount].pwszVal = szAdminFormatNameBuffer; //Value
  1101. #else
  1102. WCHAR szwAdminFormatNameBuffer[MAX_Q_FORMATNAME_LEN];
  1103. size_t rc = mbstowcs(szwAdminFormatNameBuffer,
  1104. szAdminFormatNameBuffer,
  1105. _tcslen(szAdminFormatNameBuffer)+1);
  1106. ASSERT(rc != (size_t)(-1));
  1107. aVariant[PropIdCount].pwszVal = szwAdminFormatNameBuffer; //Value
  1108. #endif
  1109. PropIdCount++;
  1110. //
  1111. // Set the MQMSGPROPS structure
  1112. //
  1113. MsgProps.cProp = PropIdCount; //Number of properties.
  1114. MsgProps.aPropID = aPropId; //Id of properties.
  1115. MsgProps.aPropVar = aVariant; //Value of properties.
  1116. MsgProps.aStatus = NULL; //No Error report.
  1117. //
  1118. // Send the message.
  1119. //
  1120. hr = MQSendMessage(
  1121. hQueue, // handle to the Queue.
  1122. &MsgProps, // Message properties to be sent.
  1123. NULL // No transaction
  1124. );
  1125. if (FAILED(hr))
  1126. {
  1127. //
  1128. // Error - display message
  1129. //
  1130. _stprintf(szMsgBuffer, TEXT("MQSendMessage failed, Error code = 0x%x."),hr);
  1131. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1132. }
  1133. else
  1134. {
  1135. //
  1136. // Success - write in edit control
  1137. //
  1138. _stprintf(szMsgBuffer, TEXT("The Message \"%s\" was sent successfully."), szLastMessageLabel);
  1139. PrintToScreen(szMsgBuffer);
  1140. }
  1141. }
  1142. /* ************************************************************************ */
  1143. /* OnApiReceiveMessage */
  1144. /* ************************************************************************ */
  1145. /* This function opens a dialog box and asks the user for the queue's */
  1146. /* PathName and the Time to wait for the message. Then it tries to get a */
  1147. /* message from the specified queue at the given time. */
  1148. /* */
  1149. /* Uses: MQReceiveMessage, MQFreeMemory. */
  1150. /* ************************************************************************ */
  1151. void CMainFrame::OnApiReceiveMessage()
  1152. {
  1153. TCHAR szPathNameBuffer[MAX_Q_PATHNAME_LEN];
  1154. TCHAR szMsgBuffer[2*BUFFERSIZE];
  1155. TCHAR szDomainName[BUFFERSIZE];
  1156. TCHAR szAccountName[BUFFERSIZE];
  1157. DWORD dwActNameSize = sizeof(szAccountName);
  1158. DWORD dwDomNameSize = sizeof(szDomainName);
  1159. TCHAR szTextSid[BUFFERSIZE];
  1160. DWORD dwTextSidSize = sizeof(szTextSid);
  1161. BYTE blobBuffer[BUFFERSIZE];
  1162. MQMSGPROPS MsgProps;
  1163. MQPROPVARIANT aVariant[MAXINDEX];
  1164. MSGPROPID aPropId[MAXINDEX];
  1165. DWORD PropIdCount = 0;
  1166. HRESULT hr;
  1167. WCHAR szMessageLabelBuffer[BUFFERSIZE];
  1168. DWORD dwTimeout;
  1169. QUEUEHANDLE hQueue;
  1170. CReceiveWaitDialog WaitDialog;
  1171. CReceiveMessageDialog ReceiveMessageDialog(&m_OpenedQueuePathNameArray);
  1172. //
  1173. // Display the ReceiveMessage dialog.
  1174. //
  1175. if (ReceiveMessageDialog.DoModal() == IDCANCEL)
  1176. {
  1177. return;
  1178. }
  1179. ReceiveMessageDialog.DestroyWindow();
  1180. ReceiveMessageDialog.GetPathName(szPathNameBuffer);
  1181. //
  1182. // Get the queue handle.
  1183. //
  1184. if (GetQueueHandle(szPathNameBuffer, &hQueue) == FALSE)
  1185. {
  1186. //
  1187. // Error - display message
  1188. //
  1189. _stprintf(szMsgBuffer, TEXT("GetQueueHandle failed. Queue was not found in Opened Queue Array"));
  1190. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1191. return;
  1192. }
  1193. //
  1194. // Retrieve the properties form the dialog box.
  1195. //
  1196. dwTimeout = ReceiveMessageDialog.GetTimeout();
  1197. //
  1198. // prepare the property array PROPVARIANT of
  1199. // message properties that we want to receive
  1200. //
  1201. //
  1202. // Set the PROPID_M_BODY property.
  1203. //
  1204. aPropId[PropIdCount] = PROPID_M_BODY; //PropId
  1205. aVariant[PropIdCount].vt = VT_VECTOR|VT_UI1; //Type
  1206. aVariant[PropIdCount].caub.cElems = ReceiveMessageDialog.GetBodySize() ;
  1207. aVariant[PropIdCount].caub.pElems = (unsigned char *) new
  1208. char [ aVariant[PropIdCount].caub.cElems ] ;
  1209. int iBodyIndex = PropIdCount ;
  1210. PropIdCount++;
  1211. //
  1212. // Set the PROPID_M_LABEL property.
  1213. //
  1214. aPropId[PropIdCount] = PROPID_M_LABEL; //PropId
  1215. aVariant[PropIdCount].vt = VT_LPWSTR; //Type
  1216. aVariant[PropIdCount].pwszVal = szMessageLabelBuffer;
  1217. PropIdCount++;
  1218. //
  1219. // Set the PROPID_M_PRIORITY property.
  1220. //
  1221. aPropId[PropIdCount] = PROPID_M_PRIORITY; //PropId
  1222. aVariant[PropIdCount].vt = VT_UI1; //Type
  1223. PropIdCount++;
  1224. //
  1225. // Set the PROPID_M_CLASS property.
  1226. //
  1227. aPropId[PropIdCount] = PROPID_M_CLASS; //PropId
  1228. aVariant[PropIdCount].vt = VT_UI2; //Type
  1229. PropIdCount++;
  1230. //
  1231. // Set the PROPID_M_AUTHENTICATED property.
  1232. //
  1233. aPropId[PropIdCount] = PROPID_M_AUTHENTICATED; //PropId
  1234. aVariant[PropIdCount].vt = VT_UI1; //Type
  1235. PropIdCount++;
  1236. //
  1237. // Set the PROPID_M_SENDERID property
  1238. //
  1239. aPropId[PropIdCount] = PROPID_M_SENDERID; //PropId
  1240. aVariant[PropIdCount].vt = VT_UI1|VT_VECTOR; //Type
  1241. aVariant[PropIdCount].blob.pBlobData = blobBuffer;
  1242. aVariant[PropIdCount].blob.cbSize = sizeof(blobBuffer);
  1243. PropIdCount++;
  1244. //
  1245. // Set the PROPID_M_PRIV_LEVEL property
  1246. //
  1247. aPropId[PropIdCount] = PROPID_M_PRIV_LEVEL; //PropId
  1248. aVariant[PropIdCount].vt = VT_UI4 ; //Type
  1249. PropIdCount++;
  1250. //
  1251. // Set the PROPID_M_LABEL_LEN property.
  1252. //
  1253. aPropId[PropIdCount] = PROPID_M_LABEL_LEN; //PropId
  1254. aVariant[PropIdCount].vt = VT_UI4; //Type
  1255. aVariant[PropIdCount].ulVal = BUFFERSIZE; //Value
  1256. PropIdCount++;
  1257. //
  1258. // Set the MQMSGPROPS structure
  1259. //
  1260. MsgProps.cProp = PropIdCount; //Number of properties.
  1261. MsgProps.aPropID = aPropId; //Id of properties.
  1262. MsgProps.aPropVar = aVariant; //Value of properties.
  1263. MsgProps.aStatus = NULL; //No Error report.
  1264. //
  1265. // Display a message window until the message from the queue will be received.
  1266. //
  1267. WaitDialog.Create(IDD_WAIT_DIALOG,pMainView);
  1268. WaitDialog.ShowWindow(SW_SHOWNORMAL);
  1269. WaitDialog.UpdateWindow();
  1270. WaitDialog.CenterWindow();
  1271. pMainView->RedrawWindow();
  1272. //
  1273. // Receive the message.
  1274. //
  1275. hr = MQReceiveMessage(
  1276. hQueue, // handle to the Queue.
  1277. dwTimeout, // Max time (msec) to wait for the message.
  1278. MQ_ACTION_RECEIVE, // Action.
  1279. &MsgProps, // properties to retrieve.
  1280. NULL, // No overlaped structure.
  1281. NULL, // No callback function.
  1282. NULL, // No Cursor.
  1283. NULL // No transaction
  1284. );
  1285. WaitDialog.ShowWindow(SW_HIDE);
  1286. if(hr == MQ_ERROR_IO_TIMEOUT)
  1287. {
  1288. _stprintf(szMsgBuffer, TEXT("MQReceiveMessage failed, Timeout expired."),hr);
  1289. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1290. }
  1291. else if(hr != MQ_OK)
  1292. {
  1293. //
  1294. // Error - display message
  1295. //
  1296. _stprintf(szMsgBuffer, TEXT("MQReceiveMessage failed, Error code = 0x%x."),hr);
  1297. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1298. }
  1299. else
  1300. {
  1301. //
  1302. // Success - write in edit control
  1303. //
  1304. ClassToString(aVariant[3].uiVal,szMsgBuffer);
  1305. PrintToScreen(szMsgBuffer);
  1306. //
  1307. // Print some of the Message properties.
  1308. //
  1309. #ifdef UNICODE
  1310. _stprintf(szMsgBuffer, TEXT("\tLabel: %s"), (WCHAR *)(aVariant[1].pwszVal));
  1311. #else
  1312. {
  1313. PCHAR lpLable = UnicodeStringToAnsiString((WCHAR *)(aVariant[1].pwszVal));
  1314. _stprintf(szMsgBuffer, TEXT("\tLabel: %s"), lpLable);
  1315. delete [] lpLable;
  1316. }
  1317. #endif
  1318. PrintToScreen(szMsgBuffer);
  1319. //
  1320. // Only if the message is not a falcon message print the body.
  1321. // (this is done since in ACK messages there is no message body).
  1322. //
  1323. if (aVariant[3].bVal == MQMSG_CLASS_NORMAL)
  1324. {
  1325. #ifdef UNICODE
  1326. _stprintf(szMsgBuffer, TEXT("\tBody : %s"), (WCHAR *)(aVariant[0].caub.pElems));
  1327. #else
  1328. {
  1329. PCHAR pBody = UnicodeStringToAnsiString((WCHAR *)(aVariant[0].caub.pElems));
  1330. _stprintf(szMsgBuffer, TEXT("\tBody : %s"), pBody);
  1331. delete [] pBody;
  1332. }
  1333. #endif
  1334. PrintToScreen(szMsgBuffer);
  1335. }
  1336. _stprintf(szMsgBuffer, TEXT("\tPriority : %d"), aVariant[2].bVal);
  1337. PrintToScreen(szMsgBuffer);
  1338. //
  1339. // Print Sender ID
  1340. //
  1341. //
  1342. // See if we're running on NT or Win95.
  1343. //
  1344. OSVERSIONINFO OsVerInfo;
  1345. OsVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  1346. GetVersionEx(&OsVerInfo);
  1347. if (OsVerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
  1348. {
  1349. //
  1350. // On NT
  1351. //
  1352. SID_NAME_USE peUse;
  1353. if (LookupAccountSid(NULL,
  1354. blobBuffer,
  1355. szAccountName,
  1356. &dwActNameSize,
  1357. szDomainName,
  1358. &dwDomNameSize,
  1359. &peUse) )
  1360. {
  1361. _stprintf(szMsgBuffer, TEXT("\tUser: %s\\%s"),
  1362. (WCHAR *)(szDomainName),(WCHAR *)(szAccountName));
  1363. PrintToScreen(szMsgBuffer);
  1364. }
  1365. }
  1366. else
  1367. {
  1368. //
  1369. // LookupAccountSid is not implemented on Win95,
  1370. // instead print textual sid
  1371. //
  1372. if ( GetTextualSid((PSID)blobBuffer, szTextSid, &dwTextSidSize))
  1373. {
  1374. _stprintf(szMsgBuffer, TEXT("\tUser SID : %s"), szTextSid);
  1375. PrintToScreen(szMsgBuffer);
  1376. }
  1377. }
  1378. //
  1379. // Print "Authenticated" or "Non Authenticated"
  1380. //
  1381. if (aVariant[4].bVal)
  1382. PrintToScreen(TEXT("\tMessage is Authenticated."));
  1383. else
  1384. PrintToScreen(TEXT("\tMessage is Not Authenticated."));
  1385. //
  1386. // Print "Encrypted" or "Non Encrypted"
  1387. //
  1388. if (aVariant[6].ulVal)
  1389. PrintToScreen(TEXT("\tMessage is Encrypted."));
  1390. else
  1391. PrintToScreen(TEXT("\tMessage is Not Encrypted."));
  1392. }
  1393. delete aVariant[ iBodyIndex ].caub.pElems ;
  1394. }
  1395. /* ************************************************************************ */
  1396. /* OnApiLocate */
  1397. /* ************************************************************************ */
  1398. /* This function opens a dialog box and ask the user to give a Label. Then */
  1399. /* it locates all the Queues in the DS with a matching label. */
  1400. /* The function updates the PathName Array with those queues. */
  1401. /* */
  1402. /* Uses: MQLocateBegin, MQLocateNext, MQLocateEnd, */
  1403. /* MQInstanceToFormatName, MQFreeMemory. */
  1404. /* ************************************************************************ */
  1405. void CMainFrame::OnApiLocate()
  1406. {
  1407. // TODO: Add your command handler code here
  1408. TCHAR szMsgBuffer[BUFFERSIZE];
  1409. TCHAR szLabelBuffer[BUFFERSIZE];
  1410. HRESULT hr;
  1411. MQPROPERTYRESTRICTION PropertyRestriction;
  1412. MQRESTRICTION Restriction;
  1413. MQCOLUMNSET Column;
  1414. QUEUEPROPID aPropId[2]; // only two properties to retrieve.
  1415. HANDLE hEnum;
  1416. DWORD cQueue;
  1417. MQPROPVARIANT aPropVar[MAX_VAR] = {0};
  1418. ARRAYQ* pArrayQ;
  1419. DWORD i;
  1420. DWORD dwColumnCount = 0;
  1421. DWORD dwFormatNameLength = MAX_Q_FORMATNAME_LEN;
  1422. CLocateDialog LocateDialog;
  1423. //
  1424. // Display the ReceiveMessage dialog.
  1425. //
  1426. if (LocateDialog.DoModal() == IDCANCEL)
  1427. {
  1428. return;
  1429. }
  1430. //
  1431. // Retrieve the label from the dialog box.
  1432. //
  1433. LocateDialog.GetLabel(szLabelBuffer);
  1434. //
  1435. // Clean the PathNameArray before locate.
  1436. //
  1437. CleanPathNameArray();
  1438. //
  1439. // Prepare Parameters to locate a queue.
  1440. //
  1441. //
  1442. // Prepare property restriction.
  1443. // Restriction = All queue with PROPID_Q_LABEL equal to "MQ API test".
  1444. //
  1445. PropertyRestriction.rel = PREQ;
  1446. PropertyRestriction.prop = PROPID_Q_LABEL;
  1447. PropertyRestriction.prval.vt = VT_LPWSTR;
  1448. #ifdef UNICODE
  1449. PropertyRestriction.prval.pwszVal = szLabelBuffer;
  1450. #else
  1451. DWORD size = _tcslen(szLabelBuffer) +1;
  1452. PropertyRestriction.prval.pwszVal = new WCHAR[size];
  1453. AnsiStringToUnicode(PropertyRestriction.prval.pwszVal, szLabelBuffer,size);
  1454. #endif
  1455. //
  1456. // prepare a restriction with one property restriction.
  1457. //
  1458. Restriction.cRes = 1;
  1459. Restriction.paPropRes = &PropertyRestriction;
  1460. //
  1461. // Columset (In other words what property I want to retrieve).
  1462. // Only the PathName is important.
  1463. //
  1464. aPropId[dwColumnCount] = PROPID_Q_PATHNAME;
  1465. dwColumnCount++;
  1466. aPropId[dwColumnCount] = PROPID_Q_INSTANCE;
  1467. dwColumnCount++;
  1468. Column.cCol = dwColumnCount;
  1469. Column.aCol = aPropId;
  1470. //
  1471. // Locate the queues. Issue the query
  1472. //
  1473. hr = MQLocateBegin(
  1474. NULL, //start search at the top.
  1475. &Restriction, //Restriction
  1476. &Column, //ColumnSet
  1477. NULL, //No sort order
  1478. &hEnum //Enumeration Handle
  1479. );
  1480. if(FAILED(hr))
  1481. {
  1482. //
  1483. // Error - display message
  1484. //
  1485. _stprintf(szMsgBuffer,
  1486. TEXT("MQLocateBegin failed, Error code = 0x%x."),hr);
  1487. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1488. return;
  1489. }
  1490. //
  1491. // Get the results.
  1492. //
  1493. cQueue = MAX_VAR;
  1494. //
  1495. // If cQueue == 0 it means that no Variants were retrieved in the last MQLocateNext.
  1496. //
  1497. while (cQueue != 0)
  1498. {
  1499. hr = MQLocateNext(
  1500. hEnum, // handle returned by MQLocateBegin.
  1501. &cQueue, // size of aPropVar array.
  1502. aPropVar // OUT: an array of MQPROPVARIANT to get the results in.
  1503. );
  1504. if(FAILED(hr))
  1505. {
  1506. //
  1507. // Error - display message
  1508. //
  1509. _stprintf(szMsgBuffer,
  1510. TEXT("MQLocateNext failed, Error code = 0x%x."),hr);
  1511. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1512. return;
  1513. }
  1514. for (i=0; i<cQueue; i++)
  1515. {
  1516. //
  1517. // add the new path names to the path name array.
  1518. //
  1519. pArrayQ = new ARRAYQ;
  1520. #ifdef UNICODE
  1521. wcsncpy (pArrayQ->szPathName, aPropVar[i].pwszVal, MAX_Q_PATHNAME_LEN);
  1522. #else
  1523. size_t rc = wcstombs(pArrayQ->szPathName, aPropVar[i].pwszVal, MAX_Q_PATHNAME_LEN);
  1524. ASSERT(rc != (size_t)(-1));
  1525. #endif
  1526. //
  1527. // move to the next property.
  1528. //
  1529. i = i + 1;
  1530. //
  1531. // Get the FormatName of the queue and set it in the PathName array.
  1532. //
  1533. #ifdef UNICODE
  1534. hr = MQInstanceToFormatName(aPropVar[i].puuid, pArrayQ->szFormatName, &dwFormatNameLength);
  1535. #else
  1536. WCHAR szwFormatNameBuffer[MAX_Q_FORMATNAME_LEN];
  1537. hr = MQInstanceToFormatName(aPropVar[i].puuid, szwFormatNameBuffer, &dwFormatNameLength);
  1538. if (SUCCEEDED(hr))
  1539. {
  1540. size_t rwc =wcstombs(pArrayQ->szFormatName, szwFormatNameBuffer, dwFormatNameLength);
  1541. ASSERT(rwc != (size_t)(-1));
  1542. }
  1543. #endif
  1544. if(FAILED(hr))
  1545. {
  1546. //
  1547. // Error - display message
  1548. //
  1549. _stprintf (szMsgBuffer,
  1550. TEXT("MQInstanceToFormatName failed, Error code = 0x%x."),hr);
  1551. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1552. }
  1553. //
  1554. // Free the memory allocated by MSMQ
  1555. //
  1556. MQFreeMemory(aPropVar[i].pwszVal);
  1557. //
  1558. // Add the new Queue to the PathNameArray.
  1559. //
  1560. Add2PathNameArray(pArrayQ);
  1561. }
  1562. }
  1563. //
  1564. // End the locate operation.
  1565. //
  1566. hr = MQLocateEnd(hEnum); // handle returned by MQLocateBegin.
  1567. if(FAILED(hr))
  1568. {
  1569. //
  1570. // Error - display message
  1571. //
  1572. _stprintf (szMsgBuffer,
  1573. TEXT("MQLocateEnd failed, Error code = 0x%x."),hr);
  1574. MessageBox(szMsgBuffer, TEXT("ERROR"), MB_OK);
  1575. return;
  1576. }
  1577. //
  1578. // Display the Queues found on the locate.
  1579. //
  1580. _stprintf (szMsgBuffer, TEXT("Locate Operation completed successfully"));
  1581. PrintToScreen(szMsgBuffer);
  1582. UpdatePathNameArrays();
  1583. DisplayPathNameArray();
  1584. DisplayOpenedQueuePathNameArray();
  1585. }
  1586. /* ************************************************************************ */
  1587. /* OnUpdateFrameTitle */
  1588. /* ************************************************************************ */
  1589. void CMainFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
  1590. {
  1591. SetWindowText (TEXT("MQ API test"));
  1592. }