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.

306 lines
7.4 KiB

  1. #include <windows.h>
  2. #include <wmimsg.h>
  3. #include <comutl.h>
  4. #include "stdio.h"
  5. ULONG g_cTargets = 0;
  6. LPWSTR g_awszTargets[256];
  7. LPWSTR g_wszAckTarget = NULL;
  8. LPWSTR g_wszTargetPrincipal = NULL;
  9. DWORD g_dwFlags = 0;
  10. ULONG g_ulNumMsgs = 1;
  11. ULONG g_ulSizeMsg = 256;
  12. BOOL g_bVerbose = FALSE;
  13. class CTestErrorSink : public IWmiMessageTraceSink
  14. {
  15. public:
  16. STDMETHOD_(ULONG,AddRef)() { return 1; }
  17. STDMETHOD_(ULONG,Release)() { return 1; }
  18. STDMETHOD(QueryInterface)( REFIID, void** ) { return E_NOINTERFACE; }
  19. STDMETHOD(Notify)( HRESULT hRes,
  20. GUID guidSource,
  21. LPCWSTR wszTrace,
  22. IUnknown* pCtx )
  23. {
  24. if ( FAILED(hRes) )
  25. {
  26. wprintf(L"Error: %s, HR: 0x%x\n", wszTrace, hRes );
  27. }
  28. else if ( g_bVerbose )
  29. {
  30. wprintf(L"Trace: %s, HR: 0x%x\n", wszTrace, hRes );
  31. }
  32. return S_OK;
  33. }
  34. };
  35. BOOL ParseArg( LPWSTR wszArg )
  36. {
  37. WCHAR* pCurr = wszArg;
  38. if ( *pCurr != '/' && *pCurr != '-' )
  39. {
  40. g_awszTargets[g_cTargets++] = pCurr;
  41. return TRUE;
  42. }
  43. pCurr++; // remove the / or -
  44. if ( wbem_wcsicmp( pCurr, L"auth" ) == 0 )
  45. {
  46. g_dwFlags |= WMIMSG_FLAG_SNDR_AUTHENTICATE;
  47. }
  48. else if ( wbem_wcsicmp( pCurr, L"encrypt" ) == 0 )
  49. {
  50. g_dwFlags |= WMIMSG_FLAG_SNDR_ENCRYPT;
  51. }
  52. else if ( wbem_wcsicmp( pCurr, L"txn" ) == 0 )
  53. {
  54. g_dwFlags &= ~WMIMSG_MASK_QOS;
  55. g_dwFlags |= WMIMSG_FLAG_QOS_XACT;
  56. }
  57. else if ( wbem_wcsicmp( pCurr, L"express" ) == 0 )
  58. {
  59. g_dwFlags &= ~WMIMSG_MASK_QOS;
  60. g_dwFlags |= WMIMSG_FLAG_QOS_EXPRESS;
  61. }
  62. else if ( wbem_wcsicmp( pCurr, L"guaranteed" ) == 0 )
  63. {
  64. g_dwFlags &= ~WMIMSG_MASK_QOS;
  65. g_dwFlags |= WMIMSG_FLAG_QOS_GUARANTEED;
  66. }
  67. else if ( wbem_wcsicmp( pCurr, L"sync" ) == 0 )
  68. {
  69. g_dwFlags &= ~WMIMSG_MASK_QOS;
  70. g_dwFlags |= WMIMSG_FLAG_QOS_SYNCHRONOUS;
  71. }
  72. else if ( wbem_wcsicmp( pCurr, L"verbose") == 0 )
  73. {
  74. g_bVerbose = TRUE;
  75. }
  76. else if ( wbem_wcsicmp( pCurr, L"sign") == 0 )
  77. {
  78. g_dwFlags |= WMIMSG_FLAG_SNDR_PRIV_SIGN;
  79. }
  80. else if ( wbem_wcsnicmp( pCurr, L"ack", 3 ) == 0 )
  81. {
  82. pCurr += 3;
  83. if ( *pCurr++ != ':' )
  84. {
  85. return FALSE;
  86. }
  87. g_wszAckTarget = pCurr;
  88. }
  89. else if ( wbem_wcsnicmp( pCurr, L"tgtprinc", 8 ) == 0 )
  90. {
  91. pCurr += 8;
  92. if ( *pCurr++ != ':' )
  93. {
  94. return FALSE;
  95. }
  96. g_wszTargetPrincipal = pCurr;
  97. }
  98. else if ( wbem_wcsnicmp( pCurr, L"nummsgs", 7 ) == 0 )
  99. {
  100. pCurr += 7;
  101. if ( *pCurr++ != ':' )
  102. {
  103. return FALSE;
  104. }
  105. g_ulNumMsgs = _wtol( pCurr );
  106. }
  107. else if ( wbem_wcsnicmp( pCurr, L"msgsize", 7 ) == 0 )
  108. {
  109. pCurr += 7;
  110. if ( *pCurr++ != ':' )
  111. {
  112. return FALSE;
  113. }
  114. g_ulSizeMsg = _wtol( pCurr );
  115. }
  116. else
  117. {
  118. return FALSE;
  119. }
  120. return TRUE;
  121. }
  122. BOOL ParseArgs( int nArgs, LPWSTR* awszArgs )
  123. {
  124. if ( nArgs < 2 )
  125. {
  126. return FALSE;
  127. }
  128. for( int i=1; i < nArgs; i++ )
  129. {
  130. if ( !ParseArg( awszArgs[i] ) )
  131. {
  132. return FALSE;
  133. }
  134. }
  135. return TRUE;
  136. }
  137. int TestMain( CLSID ClsidSender )
  138. {
  139. HRESULT hr;
  140. CWbemPtr<IWmiMessageSender> pSender;
  141. hr = CoCreateInstance( ClsidSender,
  142. NULL,
  143. CLSCTX_INPROC,
  144. IID_IWmiMessageSender,
  145. (void**)&pSender );
  146. if ( FAILED(hr) )
  147. {
  148. printf( "Failed Creating Sender. HR = 0x%x\n", hr );
  149. return 1;
  150. }
  151. CWbemPtr<IWmiMessageMultiSendReceive> pMultiSend;
  152. hr = CoCreateInstance( CLSID_WmiMessageMultiSendReceive,
  153. NULL,
  154. CLSCTX_INPROC,
  155. IID_IWmiMessageMultiSendReceive,
  156. (void**)&pMultiSend );
  157. if ( FAILED(hr) )
  158. {
  159. printf( "Failed Creating Multi Send. HR = 0x%x\n", hr );
  160. return 1;
  161. }
  162. CTestErrorSink ErrorSink;
  163. for( ULONG i=0; i < g_cTargets; i++ )
  164. {
  165. CWbemPtr<IWmiMessageSendReceive> pSend;
  166. WMIMSG_SNDR_AUTH_INFO AuthInfo;
  167. ZeroMemory( &AuthInfo, sizeof(WMIMSG_SNDR_AUTH_INFO) );
  168. AuthInfo.wszTargetPrincipal = g_wszTargetPrincipal;
  169. hr = pSender->Open( g_awszTargets[i],
  170. g_dwFlags,
  171. &AuthInfo,
  172. g_wszAckTarget,
  173. &ErrorSink,
  174. &pSend );
  175. if ( FAILED(hr) )
  176. {
  177. printf( "Failed Opening Sender. HR = 0x%x\n", hr );
  178. return 1;
  179. }
  180. hr = pMultiSend->Add( 0, pSend );
  181. if ( FAILED(hr) )
  182. {
  183. printf( "Failed Adding to Multi Send. HR = 0x%x\n", hr );
  184. return 1;
  185. }
  186. }
  187. BYTE* pMsg = new BYTE[g_ulSizeMsg];
  188. BYTE achAuxMsg[256];
  189. for( i=0; i < g_ulNumMsgs; i++ )
  190. {
  191. hr = pMultiSend->SendReceive( pMsg, g_ulSizeMsg, achAuxMsg, 256, 0, NULL );
  192. if ( FAILED(hr) )
  193. {
  194. printf( "Failed sending message. HR = 0x%x\n", hr );
  195. return 1;
  196. }
  197. }
  198. printf("Test Complete!\n");
  199. return 0;
  200. }
  201. extern "C" int __cdecl wmain( int argc, wchar_t** argv )
  202. {
  203. CoInitialize( NULL );
  204. if ( !ParseArgs( argc, argv ) )
  205. {
  206. wprintf( L"Usage : msgsend [-sync|-express|-guaranteed|-txn] \n"
  207. L" [-auth] [-encrypt] [-ack:target] \n"
  208. L" [-nummsgs:#] [-msgsize:#] [-sign]\n"
  209. L" [-verbose] [-tgtprinc:princname] \n"
  210. L" target1 target2..\n");
  211. return 1;
  212. }
  213. DWORD dwQos = g_dwFlags & WMIMSG_MASK_QOS;
  214. if ( g_bVerbose )
  215. {
  216. if (g_dwFlags & WMIMSG_FLAG_SNDR_AUTHENTICATE) printf( "-Authenticate\n" );
  217. if ( g_dwFlags & WMIMSG_FLAG_SNDR_ENCRYPT ) printf( "-Encryption\n" );
  218. if ( dwQos == WMIMSG_FLAG_QOS_SYNCHRONOUS ) printf( "-Sync QoS\n" );
  219. if ( dwQos == WMIMSG_FLAG_QOS_EXPRESS ) printf( "-Express QoS\n" );
  220. if ( dwQos == WMIMSG_FLAG_QOS_GUARANTEED ) printf("-Guaranteed QoS\n");
  221. if ( dwQos == WMIMSG_FLAG_QOS_XACT ) printf( "-Xact QoS\n" );
  222. if ( g_wszAckTarget != NULL )
  223. {
  224. wprintf( L"-Ack Target: %s \n", g_wszAckTarget );
  225. }
  226. if ( g_wszTargetPrincipal != NULL )
  227. {
  228. wprintf( L"-Target Principal: %s \n", g_wszTargetPrincipal );
  229. }
  230. for( ULONG i=0; i < g_cTargets; i++ )
  231. {
  232. wprintf( L"-Target: %s \n", g_awszTargets[i] );
  233. }
  234. printf( "---------------------------------\n\n" );
  235. }
  236. CLSID ClsidSender;
  237. if ( dwQos == WMIMSG_FLAG_QOS_SYNCHRONOUS )
  238. {
  239. ClsidSender = CLSID_WmiMessageRpcSender;
  240. }
  241. else
  242. {
  243. ClsidSender = CLSID_WmiMessageMsmqSender;
  244. }
  245. int ret = TestMain( ClsidSender );
  246. CoUninitialize();
  247. return ret;
  248. }