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.

270 lines
6.2 KiB

  1. #include <windows.h>
  2. #include <wmimsg.h>
  3. #include <comutl.h>
  4. #include "stdio.h"
  5. LPWSTR g_wszQueue;
  6. DWORD g_dwNumQueues = 1;
  7. DWORD g_dwQuota = 0xffffffff;
  8. DWORD g_dwQos = WMIMSG_FLAG_QOS_EXPRESS;
  9. BOOL g_bAuth = FALSE;
  10. BOOL g_bDestroy = FALSE;
  11. BOOL g_bCreate = FALSE;
  12. BOOL g_bVerbose = FALSE;
  13. GUID g_guidType = { 0x5b6ef4a4, 0x931, 0x46a4,
  14. {0xa5,0xa9,0x8e,0x4,0xfc,0xcd,0xb0,0xd5} };
  15. BOOL ParseArg( LPWSTR wszArg )
  16. {
  17. WCHAR* pCurr = wszArg;
  18. if ( *pCurr != '/' && *pCurr != '-' )
  19. {
  20. g_wszQueue = pCurr;
  21. return TRUE;
  22. }
  23. pCurr++; // remove the / or -
  24. if ( wbem_wcsicmp( pCurr, L"auth" ) == 0 )
  25. {
  26. g_bAuth = TRUE;
  27. }
  28. else if ( wbem_wcsicmp( pCurr, L"create" ) == 0 )
  29. {
  30. g_bCreate = TRUE;
  31. }
  32. else if ( wbem_wcsicmp( pCurr, L"destroy" ) == 0 )
  33. {
  34. g_bDestroy = TRUE;
  35. }
  36. else if ( wbem_wcsicmp( pCurr, L"txn" ) == 0 )
  37. {
  38. g_dwQos = WMIMSG_FLAG_QOS_XACT;
  39. }
  40. else if ( wbem_wcsicmp( pCurr, L"express" ) == 0 )
  41. {
  42. g_dwQos = WMIMSG_FLAG_QOS_EXPRESS;
  43. }
  44. else if ( wbem_wcsicmp( pCurr, L"guaranteed" ) == 0 )
  45. {
  46. g_dwQos = WMIMSG_FLAG_QOS_GUARANTEED;
  47. }
  48. else if ( wbem_wcsicmp( pCurr, L"verbose") == 0 )
  49. {
  50. g_bVerbose = TRUE;
  51. }
  52. else if ( wbem_wcsnicmp( pCurr, L"quota", 5 ) == 0 )
  53. {
  54. pCurr += 5;
  55. if ( *pCurr++ != ':' )
  56. {
  57. return FALSE;
  58. }
  59. g_dwQuota = _wtol( pCurr );
  60. }
  61. else if ( wbem_wcsnicmp( pCurr, L"numqueues", 9 ) == 0 )
  62. {
  63. pCurr += 9;
  64. if ( *pCurr++ != ':' )
  65. {
  66. return FALSE;
  67. }
  68. g_dwNumQueues = _wtol( pCurr );
  69. }
  70. else if ( wbem_wcsnicmp( pCurr, L"type", 4 ) == 0 )
  71. {
  72. pCurr += 4;
  73. if ( *pCurr++ != ':' )
  74. {
  75. return FALSE;
  76. }
  77. if ( FAILED( CLSIDFromString( pCurr, &g_guidType ) ) )
  78. {
  79. return FALSE;
  80. }
  81. }
  82. else
  83. {
  84. return FALSE;
  85. }
  86. return TRUE;
  87. }
  88. BOOL ParseArgs( int nArgs, LPWSTR* awszArgs )
  89. {
  90. if ( nArgs < 1 )
  91. {
  92. return FALSE;
  93. }
  94. for( int i=1; i < nArgs; i++ )
  95. {
  96. if ( !ParseArg( awszArgs[i] ) )
  97. {
  98. return FALSE;
  99. }
  100. }
  101. if ( (g_bCreate || g_bDestroy) && g_wszQueue == NULL )
  102. {
  103. return FALSE;
  104. }
  105. return TRUE;
  106. }
  107. int TestMain()
  108. {
  109. HRESULT hr;
  110. CWbemPtr<IWmiMessageQueueManager> pQueueMgr;
  111. hr = CoCreateInstance( CLSID_WmiMessageQueueManager,
  112. NULL,
  113. CLSCTX_INPROC,
  114. IID_IWmiMessageQueueManager,
  115. (void**)&pQueueMgr );
  116. if ( FAILED(hr) )
  117. {
  118. printf( "Failed Obtaining Queue Object. HR = 0x%x\n", hr );
  119. return 1;
  120. }
  121. WCHAR awchQueue[256];
  122. SYSTEMTIME Start, End;
  123. GetSystemTime( &Start );
  124. if ( g_bCreate )
  125. {
  126. wcscpy( awchQueue, g_wszQueue );
  127. hr = pQueueMgr->Create( awchQueue,
  128. g_guidType,
  129. g_bAuth,
  130. g_dwQos,
  131. g_dwQuota,
  132. NULL );
  133. for( DWORD i=1; i < g_dwNumQueues && SUCCEEDED(hr); i ++ )
  134. {
  135. swprintf( awchQueue, L"%s%d", g_wszQueue, i );
  136. hr = pQueueMgr->Create( awchQueue,
  137. g_guidType,
  138. g_bAuth,
  139. g_dwQos,
  140. g_dwQuota,
  141. NULL );
  142. }
  143. if ( FAILED(hr) )
  144. {
  145. wprintf( L"Failed Creating Queue with name %s. HR = 0x%x\n",
  146. awchQueue, hr );
  147. return 1;
  148. }
  149. }
  150. if ( g_bDestroy )
  151. {
  152. wcscpy( awchQueue, g_wszQueue );
  153. hr = pQueueMgr->Destroy( awchQueue );
  154. if ( FAILED(hr) )
  155. {
  156. wprintf( L"Failed Destroying Queue with name %s. HR = 0x%x\n",
  157. awchQueue, hr );
  158. }
  159. for( ULONG i=1; i < g_dwNumQueues; i ++ )
  160. {
  161. swprintf( awchQueue, L"%s%d", g_wszQueue, i );
  162. hr = pQueueMgr->Destroy( awchQueue );
  163. if ( FAILED(hr) )
  164. {
  165. wprintf( L"Failed Destroying Queue with name %s. HR = 0x%x\n",
  166. awchQueue, hr );
  167. }
  168. }
  169. }
  170. if ( g_bVerbose )
  171. {
  172. WCHAR achType[256];
  173. StringFromGUID2( g_guidType, achType, 256 );
  174. wprintf( L"Getting all Names for Type : %s\n", achType );
  175. LPWSTR* pwszNames;
  176. ULONG cwszNames;
  177. hr = pQueueMgr->GetAllNames(g_guidType, TRUE, &pwszNames, &cwszNames );
  178. if ( FAILED(hr) )
  179. {
  180. wprintf( L"Failed Getting All Queue Names. HR = 0x%x\n" );
  181. return 1;
  182. }
  183. for( ULONG i=0; i < cwszNames; i++ )
  184. {
  185. wprintf( L" %s\n", pwszNames[i] );
  186. CoTaskMemFree( pwszNames[i] );
  187. }
  188. CoTaskMemFree( pwszNames );
  189. }
  190. GetSystemTime( &End );
  191. __int64 i64Start, i64End;
  192. DWORD dwElapsed;
  193. SystemTimeToFileTime( &Start, PFILETIME(&i64Start) );
  194. SystemTimeToFileTime( &End, PFILETIME(&i64End) );
  195. dwElapsed = DWORD(i64End - i64Start) / 10000;
  196. printf("Test Completed in %d msec!\n", dwElapsed );
  197. return 0;
  198. }
  199. extern "C" int __cdecl wmain( int argc, wchar_t** argv )
  200. {
  201. CoInitialize( NULL );
  202. if ( !ParseArgs( argc, argv ) )
  203. {
  204. wprintf( L"Usage : msgqueue [-express|-guaranteed|-txn] \n"
  205. L" [-auth] [-numqueues:#] [-quota:#]\n"
  206. L" [-create] [-destroy] [-verbose]\n"
  207. L" [-type:clsid] <queuename>\n");
  208. return 1;
  209. }
  210. int ret = TestMain();
  211. CoUninitialize();
  212. return ret;
  213. }