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.

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