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.

393 lines
9.0 KiB

  1. //
  2. // Client.cpp - client implementation
  3. //
  4. #include "precomp.h"
  5. #include "tchar.h"
  6. #include "bidispl.h"
  7. #define IS_ARG(c) (((c) == '-') || ((c) == '/'))
  8. #ifndef MODULE
  9. #define MODULE "BIDIUTIL:"
  10. #endif
  11. #ifdef DEBUG
  12. MODULE_DEBUG_INIT( DBG_ERROR | DBG_WARNING | DBG_TRACE | DBG_INFO , DBG_ERROR );
  13. #else
  14. MODULE_DEBUG_INIT( DBG_ERROR | DBG_WARNING | DBG_TRACE, DBG_ERROR );
  15. #endif
  16. VOID DbgBreakPoint (VOID)
  17. {
  18. }
  19. void DumpData (DWORD dwType, PBYTE pData, DWORD dwSize)
  20. {
  21. DBGMSG (DBG_INFO, ("dwType = %d, pData = %x, dwSize = %d\n", dwType, pData, dwSize)) ;
  22. DWORD col = 0;
  23. CHAR szBuf[512];
  24. szBuf[0] = 0;
  25. for (DWORD i = 0; i < dwSize; i++) {
  26. sprintf (szBuf + strlen (szBuf), "%02x ", pData[i]);
  27. if ((col ++) % 16 == 15) {
  28. DBGMSG (DBG_INFO, ("%s\n", szBuf));
  29. szBuf[0] = 0;
  30. }
  31. }
  32. DBGMSG (DBG_INFO, ("%s\n", szBuf));
  33. }
  34. void DumpOutputData (DWORD dwType, PBYTE pData, DWORD dwSize)
  35. {
  36. DWORD i, col = 0;
  37. CHAR szBuf[512];
  38. szBuf[0] = 0;
  39. printf ("\tdwType = %d, pData = %x, dwSize = %d\n", dwType, pData, dwSize);
  40. switch (dwType) {
  41. case BIDI_NULL:
  42. printf("\tBIDI_NULL\n");
  43. break;
  44. case BIDI_INT:
  45. printf ("\tBIDI_INT = %d\n", *(INT*)pData);
  46. break;
  47. case BIDI_FLOAT:
  48. printf ("\tBIDI_FLOAT = %f\n", *(FLOAT*)pData);
  49. break;
  50. case BIDI_BOOL:
  51. printf ("\tBIDI_BOOL = %s\n", (*(BOOL*)pData)?"TRUE":"FALSE");
  52. break;
  53. case BIDI_ENUM:
  54. case BIDI_TEXT:
  55. case BIDI_STRING:
  56. printf ("\tBIDI_STRING = |%ws|\n", pData);
  57. break;
  58. case BIDI_BLOB:
  59. printf ("\tBIDI_BLOB\n", pData);
  60. for (i = 0; i < dwSize; i++) {
  61. sprintf (szBuf + strlen (szBuf), "%02x ", pData[i]);
  62. if ((col ++) % 16 == 15) {
  63. printf ("%s\n", szBuf);
  64. szBuf[0] = 0;
  65. }
  66. }
  67. printf ("%s\n", szBuf);
  68. default:
  69. printf ("\tunsupported\n", pData);
  70. }
  71. }
  72. void DumpRequest (IBidiRequest * pIRequest)
  73. {
  74. DWORD dwTotoal, i;
  75. HRESULT hr, hResult;
  76. LPWSTR pszSchema = NULL;
  77. BYTE *pData;
  78. DWORD uSize;
  79. DWORD dwType;
  80. DWORD dwTotal;
  81. hr = pIRequest->GetResult (&hResult);
  82. SPLASSERT (SUCCEEDED (hr));
  83. hr = pIRequest->GetEnumCount (&dwTotal);
  84. SPLASSERT (SUCCEEDED (hr));
  85. printf("hr = %x; total response = %d\n", hResult, dwTotal);
  86. for (i = 0; i < dwTotal; i++) {
  87. hr = pIRequest->GetOutputData (i, &pszSchema, &dwType, &pData, &uSize);
  88. printf("%d: %ws\n", i, (pszSchema)?pszSchema:L"");
  89. DumpOutputData (dwType, pData, uSize);
  90. }
  91. }
  92. void DumpContainer (IBidiRequestContainer * pIReqContainer)
  93. {
  94. IBidiRequest * pRequest = NULL;
  95. DWORD dwRef, i;
  96. DWORD dwTotal;
  97. HRESULT hr;
  98. hr = pIReqContainer->GetRequestCount (&dwTotal);
  99. SPLASSERT (SUCCEEDED (hr));
  100. IEnumUnknown *pEnumIunk;
  101. hr = pIReqContainer->GetEnumObject (&pEnumIunk) ;
  102. SPLASSERT (SUCCEEDED (hr));
  103. for (i = 0; i < dwTotal; i++){
  104. IUnknown *pIunk;
  105. DWORD dwFetched;
  106. hr = pEnumIunk->Next (1, &pIunk, &dwFetched);
  107. SPLASSERT (SUCCEEDED (hr));
  108. SPLASSERT (dwFetched == 1);
  109. IBidiRequest *pIRequest = NULL;
  110. hr = pIunk->QueryInterface (IID_IBidiRequest, (void **) & pIRequest);
  111. SPLASSERT (SUCCEEDED (hr));
  112. printf("Request No.%d\n", i);
  113. DumpRequest (pIRequest);
  114. dwRef = pIunk->Release();
  115. dwRef = pIRequest->Release();
  116. }
  117. pEnumIunk->Release ();
  118. }
  119. IBidiRequest * GenRequest (LPWSTR pSchema)
  120. {
  121. HRESULT hr;
  122. IUnknown * pIUnk = NULL ;
  123. IBidiRequest * pIReq = NULL ;
  124. hr = ::CoCreateInstance(CLSID_BidiRequest,
  125. NULL,
  126. CLSCTX_INPROC_SERVER,
  127. IID_IUnknown,
  128. (void**)&pIUnk) ;
  129. if (FAILED (hr)) {
  130. DBGMSG (DBG_WARNING, ("CoCreateInstance\n"));
  131. exit (0);
  132. }
  133. hr = pIUnk->QueryInterface (IID_IBidiRequest, (void **)&pIReq);
  134. pIUnk->Release();
  135. if (FAILED (hr)) {
  136. DBGMSG (DBG_WARNING, ("QueryInterface\n"));
  137. exit (0);
  138. }
  139. if (pSchema) {
  140. hr = pIReq->SetSchema (pSchema);
  141. if (FAILED (hr)) {
  142. DBGMSG (DBG_WARNING, ("SetSchema\n"));
  143. exit (0);
  144. }
  145. }
  146. return pIReq;
  147. }
  148. IBidiRequestContainer *CreateContainer ()
  149. {
  150. HRESULT hr;
  151. IBidiRequestContainer * pIReqContainer = NULL ;
  152. hr = ::CoCreateInstance(CLSID_BidiRequestContainer,
  153. NULL,
  154. CLSCTX_INPROC_SERVER,
  155. IID_IBidiRequestContainer,
  156. (void**)&pIReqContainer) ;
  157. if (FAILED (hr)) {
  158. DBGMSG (DBG_WARNING, ("CoCreateInstance\n"));
  159. exit (0);
  160. }
  161. return pIReqContainer;
  162. }
  163. void AddRequest (
  164. IBidiRequestContainer * pIReqContainer,
  165. LPWSTR pSchema)
  166. {
  167. IBidiRequest * pRequest = NULL;
  168. HRESULT hr;
  169. DWORD dwRef;
  170. pRequest = GenRequest (pSchema);
  171. if (!pRequest) {
  172. DBGMSG (DBG_WARNING, ("GenRequest\n"));
  173. exit (0);
  174. }
  175. hr = pIReqContainer->AddRequest (pRequest);
  176. if (FAILED (hr)) {
  177. DBGMSG (DBG_WARNING, ("AddRequest\n"));
  178. exit (0);
  179. }
  180. dwRef = pRequest->Release ();
  181. }
  182. void usage()
  183. {
  184. printf("\n"
  185. "usage: bidiutil [-p pname] [-t] [-d] [-a action] [-s schema1 schema2 schema3 ... ] \n"
  186. "\n"
  187. "where: -p printer name\n"
  188. " -t SendRecv (default: MultiSendRecv)\n"
  189. " -d admin (default: User) \n"
  190. " -a action e.g. Enum, Get, GetAll) \n"
  191. " -s schema e.g. /printer/installalableoption/duplexunit\n");
  192. exit (0);
  193. }
  194. extern "C"
  195. INT
  196. _cdecl
  197. _tmain(
  198. int argc,
  199. TCHAR **argv)
  200. {
  201. HRESULT hr;
  202. ULONG dwRef;
  203. IBidiSpl * pIBidiSpl = NULL ;
  204. IBidiRequestContainer *pIReqContainer;
  205. IBidiRequest * pRequest = NULL;
  206. LPTSTR pPrinter = NULL;
  207. LPTSTR pAction = NULL;;
  208. BOOL bLoop = TRUE;
  209. BOOL bSingle = FALSE;
  210. BOOL dwAccess = BIDI_ACCESS_USER;
  211. for (--argc, ++argv; argc && bLoop; --argc, ++argv) {
  212. if (IS_ARG(**argv)) {
  213. switch (tolower(*++*argv)) {
  214. case L'?':
  215. usage();
  216. break;
  217. case L'p':
  218. ++argv;
  219. --argc;
  220. pPrinter = *argv;
  221. break;
  222. case L's':
  223. bLoop = FALSE;
  224. break;
  225. case L'a':
  226. ++argv;
  227. --argc;
  228. pAction = *argv;
  229. break;
  230. case L't':
  231. bSingle = TRUE;
  232. break;
  233. case L'd':
  234. dwAccess = BIDI_ACCESS_ADMINISTRATOR;
  235. break;
  236. default:
  237. usage();
  238. break;
  239. }
  240. }
  241. else {
  242. printf("Invalid Argument %s\n", *argv);
  243. exit (1);
  244. }
  245. }
  246. if (!pAction || !pPrinter) {
  247. usage ();
  248. }
  249. hr = CoInitializeEx (NULL, COINIT_MULTITHREADED) ;
  250. if (FAILED (hr)) {
  251. DBGMSG (DBG_WARNING, ("CoInitilaizeEx\n"));
  252. exit (0);
  253. }
  254. hr = CoCreateInstance(CLSID_BidiSpl,
  255. NULL,
  256. CLSCTX_INPROC_SERVER,
  257. IID_IBidiSpl,
  258. (void**)&pIBidiSpl) ;
  259. if (FAILED (hr)) {
  260. DBGMSG (DBG_WARNING, ("CoCreateInstance\n"));
  261. exit (0);
  262. }
  263. hr = pIBidiSpl->BindDevice (pPrinter, dwAccess);
  264. if (FAILED (hr)) {
  265. DBGMSG (DBG_WARNING, ("BindDevice\n"));
  266. exit (0);
  267. }
  268. if (bSingle) {
  269. if (argc > 0)
  270. pRequest = GenRequest (*argv);
  271. else
  272. pRequest = GenRequest (NULL);
  273. hr = pIBidiSpl->SendRecv (pAction, pRequest);
  274. if (FAILED (hr)) {
  275. printf ("SendRecv failed (0x%x)\n", hr);
  276. exit (0);
  277. }
  278. DumpRequest (pRequest);
  279. dwRef = pRequest->Release ();
  280. }
  281. else {
  282. pIReqContainer = CreateContainer ();
  283. if (argc > 0) {
  284. while (argc-- > 0)
  285. AddRequest (pIReqContainer, *argv++);
  286. }
  287. else
  288. AddRequest (pIReqContainer, NULL);
  289. hr = pIBidiSpl->MultiSendRecv (pAction, pIReqContainer);
  290. if (FAILED (hr)) {
  291. printf ("MultiSendRecv failed (0x%x)\n", hr);
  292. exit (0);
  293. }
  294. DumpContainer (pIReqContainer);
  295. dwRef = pIReqContainer->Release ();
  296. }
  297. dwRef = pIBidiSpl->Release ();
  298. DBGMSG (DBG_TRACE, ("IBidiSpl released.\n")) ;
  299. return 0 ;
  300. }