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.

336 lines
7.7 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996
  5. //
  6. // File: cenumjob.cxx
  7. //
  8. // Contents: NetWare 3.12 JobCollection Enumeration Code
  9. //
  10. // CNWCOMPATJobCollectionEnum::Create
  11. // CNWCOMPATJobCollectionEnum::GetJobObject
  12. // CNWCOMPATJobCollectionEnum::EnumJobMembers
  13. // CNWCOMPATJobCollectionEnum::Next
  14. //
  15. // History: 08-May-96 t-ptam (Patrick Tam) Created.
  16. //
  17. //----------------------------------------------------------------------------
  18. #include "NWCOMPAT.hxx"
  19. #pragma hdrstop
  20. //+---------------------------------------------------------------------------
  21. //
  22. // Function: CNWCOMPATEnumVariant::Create
  23. //
  24. // Synopsis:
  25. //
  26. // Arguments: [pCollection]
  27. // [ppEnumVariant]
  28. //
  29. // Returns: HRESULT
  30. //
  31. // Modifies:
  32. //
  33. // History: 08-Mag-96 t-ptam (Patrick Tam) Created.
  34. //
  35. //----------------------------------------------------------------------------
  36. HRESULT
  37. CNWCOMPATJobCollectionEnum::Create(
  38. CNWCOMPATJobCollectionEnum FAR* FAR* ppEnumVariant,
  39. BSTR PrinterName
  40. )
  41. {
  42. HRESULT hr = S_OK;
  43. CNWCOMPATJobCollectionEnum FAR* pEnumVariant = NULL;
  44. POBJECTINFO pPrinterObjectInfo = NULL;
  45. WCHAR szUncPrinterName[MAX_PATH];
  46. //
  47. // Validate input parameters.
  48. //
  49. if (!(ppEnumVariant) || !(PrinterName)) {
  50. RRETURN(E_ADS_BAD_PARAMETER);
  51. }
  52. *ppEnumVariant = NULL;
  53. //
  54. // Allocate a Collection Enumerator object.
  55. //
  56. pEnumVariant = new CNWCOMPATJobCollectionEnum();
  57. if (!pEnumVariant) {
  58. hr = E_OUTOFMEMORY;
  59. BAIL_ON_FAILURE(hr);
  60. }
  61. hr = ADsAllocString(PrinterName, &pEnumVariant->_PrinterName);
  62. BAIL_ON_FAILURE(hr);
  63. //
  64. // Make Unc Name to open a printer.
  65. //
  66. hr = BuildObjectInfo(
  67. PrinterName,
  68. &pPrinterObjectInfo
  69. );
  70. BAIL_ON_FAILURE(hr);
  71. ADsAssert(pPrinterObjectInfo->NumComponents == 2);
  72. wcscpy(PrinterName,
  73. pPrinterObjectInfo->ComponentArray[0]);
  74. MakeUncName (PrinterName,
  75. szUncPrinterName);
  76. wcscat(szUncPrinterName,TEXT("\\"));
  77. wcscat(szUncPrinterName, pPrinterObjectInfo->ComponentArray[1]);
  78. hr = NWApiOpenPrinter(
  79. szUncPrinterName,
  80. &pEnumVariant->_hPrinter,
  81. PRINTER_ACCESS_USE
  82. );
  83. BAIL_ON_FAILURE(hr);
  84. //
  85. // Return.
  86. //
  87. *ppEnumVariant = pEnumVariant;
  88. if(pPrinterObjectInfo){
  89. FreeObjectInfo(pPrinterObjectInfo);
  90. }
  91. RRETURN(hr);
  92. error:
  93. if(pPrinterObjectInfo){
  94. FreeObjectInfo(pPrinterObjectInfo);
  95. }
  96. delete pEnumVariant;
  97. RRETURN_EXP_IF_ERR(hr);
  98. }
  99. //----------------------------------------------------------------------------
  100. //
  101. // Function: CNWCOMPATJobCollectionEnum::CNWCOMPATJobCollectionEnum
  102. //
  103. // Synopsis:
  104. //
  105. //----------------------------------------------------------------------------
  106. CNWCOMPATJobCollectionEnum::CNWCOMPATJobCollectionEnum():
  107. _PrinterName(NULL),
  108. _hPrinter(NULL),
  109. _pBuffer(NULL),
  110. _dwReturned(0),
  111. _dwCurrentObject(0)
  112. {
  113. }
  114. //----------------------------------------------------------------------------
  115. //
  116. // Function: CNWCOMPATJobCollectionEnum::~CNWCOMPATJobCollectionEnum
  117. //
  118. // Synopsis:
  119. //
  120. //----------------------------------------------------------------------------
  121. CNWCOMPATJobCollectionEnum::~CNWCOMPATJobCollectionEnum()
  122. {
  123. if (_PrinterName) {
  124. SysFreeString(_PrinterName);
  125. }
  126. if (_hPrinter) {
  127. NWApiClosePrinter(_hPrinter);
  128. }
  129. if (_pBuffer) {
  130. FreeADsMem(_pBuffer);
  131. }
  132. }
  133. //----------------------------------------------------------------------------
  134. //
  135. // Function: CNWCOMPATJobCollectionEnum::EnumJobMembers
  136. //
  137. // Synopsis:
  138. //
  139. //----------------------------------------------------------------------------
  140. HRESULT
  141. CNWCOMPATJobCollectionEnum::EnumJobMembers(
  142. ULONG cElements,
  143. VARIANT FAR* pvar,
  144. ULONG FAR* pcElementFetched
  145. )
  146. {
  147. HRESULT hr = S_OK;
  148. IDispatch *pDispatch = NULL;
  149. DWORD i = 0;
  150. while (i < cElements) {
  151. hr = GetJobObject(&pDispatch);
  152. if (hr == S_FALSE) {
  153. break;
  154. }
  155. VariantInit(&pvar[i]);
  156. pvar[i].vt = VT_DISPATCH;
  157. pvar[i].pdispVal = pDispatch;
  158. (*pcElementFetched)++;
  159. i++;
  160. }
  161. RRETURN(hr);
  162. }
  163. //----------------------------------------------------------------------------
  164. //
  165. // Function: CNWCOMPATJobCollectionEnum::GetJobObject
  166. //
  167. // Synopsis:
  168. //
  169. //----------------------------------------------------------------------------
  170. HRESULT
  171. CNWCOMPATJobCollectionEnum::GetJobObject(
  172. IDispatch ** ppDispatch
  173. )
  174. {
  175. DWORD dwBuf = 0;
  176. DWORD dwJobInQueue = 0;
  177. HRESULT hr = S_OK;
  178. LPBYTE lpbPrinterInfo = NULL;
  179. LPJOB_INFO_1 lpJobInfo = NULL;
  180. //
  181. // Fill _pBuffer with JobID. Win32 API returns all jobs in one shot.
  182. //
  183. if (!_pBuffer) {
  184. //
  185. // Get the number of print jobs that have been queued for the printer.
  186. //
  187. hr = NWApiGetPrinter(
  188. _hPrinter,
  189. WIN32_API_LEVEL_2,
  190. &lpbPrinterInfo
  191. );
  192. BAIL_ON_FAILURE(hr);
  193. dwJobInQueue = ((LPPRINTER_INFO_2)lpbPrinterInfo)->cJobs;
  194. //
  195. // Enumerate for all the jobs.
  196. //
  197. hr = NWApiEnumJobs(
  198. _hPrinter,
  199. FIRST_PRINTJOB,
  200. dwJobInQueue,
  201. WIN32_API_LEVEL_1,
  202. &_pBuffer,
  203. &dwBuf,
  204. &_dwReturned
  205. );
  206. BAIL_ON_FAILURE(hr);
  207. }
  208. //
  209. // Traverse the buffer and return a PrintJob object.
  210. //
  211. if (_dwCurrentObject < _dwReturned) {
  212. //
  213. // Go to the next structure in the buffer.
  214. //
  215. lpJobInfo = (LPJOB_INFO_1)_pBuffer + _dwCurrentObject;
  216. //
  217. // Create a print job object.
  218. //
  219. hr = CNWCOMPATPrintJob::CreatePrintJob(
  220. _PrinterName,
  221. lpJobInfo->JobId,
  222. ADS_OBJECT_BOUND,
  223. IID_IDispatch,
  224. (void **)ppDispatch
  225. );
  226. BAIL_ON_FAILURE(hr);
  227. //
  228. // Return.
  229. //
  230. _dwCurrentObject++;
  231. if(lpbPrinterInfo){
  232. FreeADsMem(lpbPrinterInfo);
  233. }
  234. RRETURN(S_OK);
  235. }
  236. error:
  237. if(lpbPrinterInfo){
  238. FreeADsMem(lpbPrinterInfo);
  239. }
  240. *ppDispatch = NULL;
  241. RRETURN(S_FALSE);
  242. }
  243. //+---------------------------------------------------------------------------
  244. //
  245. // Function: CNWCOMPATJobCollectionEnum::Next
  246. //
  247. // Synopsis: Returns cElements number of requested NetOle objects in the
  248. // array supplied in pvar.
  249. //
  250. // Arguments: [cElements] -- The number of elements requested by client
  251. // [pvar] -- ptr to array of VARIANTs to for return objects
  252. // [pcElementFetched] -- if non-NULL, then number of elements
  253. // -- actually returned is placed here
  254. //
  255. // Returns: HRESULT -- S_OK if number of elements requested are returned
  256. // -- S_FALSE if number of elements is < requested
  257. //
  258. // Modifies:
  259. //
  260. // History: 11-3-95 krishnag Created.
  261. //
  262. //----------------------------------------------------------------------------
  263. STDMETHODIMP
  264. CNWCOMPATJobCollectionEnum::Next(
  265. ULONG cElements,
  266. VARIANT FAR* pvar,
  267. ULONG FAR* pcElementFetched
  268. )
  269. {
  270. ULONG cElementFetched = 0;
  271. HRESULT hr = S_OK;
  272. hr = EnumJobMembers(
  273. cElements,
  274. pvar,
  275. &cElementFetched
  276. );
  277. if (pcElementFetched) {
  278. *pcElementFetched = cElementFetched;
  279. }
  280. RRETURN_EXP_IF_ERR(hr);
  281. }