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.

529 lines
10 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996
  5. //
  6. // File: printhlp.cxx
  7. //
  8. // Contents: Helper functions for printer object.
  9. //
  10. // History: 08-May-96 t-ptam (PatrickT) migrated
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "nwcompat.hxx"
  14. #pragma hdrstop
  15. //----------------------------------------------------------------------------
  16. //
  17. // Function: WinNTEnumJobs
  18. //
  19. // Synopsis:
  20. //
  21. //----------------------------------------------------------------------------
  22. BOOL
  23. WinNTEnumJobs(
  24. HANDLE hPrinter,
  25. DWORD dwFirstJob,
  26. DWORD dwNoJobs,
  27. DWORD dwLevel,
  28. LPBYTE *lplpbJobs,
  29. DWORD *pcbBuf,
  30. LPDWORD lpdwReturned
  31. )
  32. {
  33. BOOL fStatus = FALSE;
  34. DWORD dwNeeded = 0;
  35. DWORD dwError = 0;
  36. //
  37. // Enumerate Jobs using Win32 API.
  38. //
  39. fStatus = EnumJobs(
  40. hPrinter,
  41. dwFirstJob,
  42. dwNoJobs,
  43. dwLevel,
  44. *lplpbJobs,
  45. *pcbBuf,
  46. &dwNeeded,
  47. lpdwReturned
  48. );
  49. //
  50. // Enumerate for Jobs again with a bigger buffer if a bigger one is needed
  51. // for the result.
  52. //
  53. if (!fStatus) {
  54. if ((dwError = GetLastError()) == ERROR_INSUFFICIENT_BUFFER) {
  55. if (*lplpbJobs) {
  56. FreeADsMem( *lplpbJobs );
  57. }
  58. *lplpbJobs = (LPBYTE)AllocADsMem(dwNeeded);
  59. if (!*lplpbJobs) {
  60. *pcbBuf = 0;
  61. return(FALSE);
  62. }
  63. *pcbBuf = dwNeeded;
  64. fStatus = EnumJobs(
  65. hPrinter,
  66. dwFirstJob,
  67. dwNoJobs,
  68. dwLevel,
  69. *lplpbJobs,
  70. *pcbBuf,
  71. &dwNeeded,
  72. lpdwReturned
  73. );
  74. if (!fStatus) {
  75. return(FALSE);
  76. }
  77. else {
  78. return(TRUE);
  79. }
  80. }
  81. else {
  82. return(FALSE);
  83. }
  84. }
  85. else {
  86. return(TRUE);
  87. }
  88. }
  89. //----------------------------------------------------------------------------
  90. //
  91. // Function: WinNTEnumPrinters
  92. //
  93. // Synopsis:
  94. //
  95. //----------------------------------------------------------------------------
  96. BOOL
  97. WinNTEnumPrinters(
  98. DWORD dwType,
  99. LPTSTR lpszName,
  100. DWORD dwLevel,
  101. LPBYTE *lplpbPrinters,
  102. LPDWORD lpdwReturned
  103. )
  104. {
  105. BOOL fStatus = FALSE;
  106. DWORD dwPassed = 1024;
  107. DWORD dwNeeded = 0;
  108. DWORD dwError = 0;
  109. LPBYTE pMem = NULL;
  110. //
  111. // Allocate memory for return buffer.
  112. //
  113. pMem = (LPBYTE)AllocADsMem(dwPassed);
  114. if (!pMem) {
  115. goto error;
  116. }
  117. //
  118. // Enumerate Printers using Win32 API.
  119. //
  120. fStatus = EnumPrinters(
  121. dwType,
  122. lpszName,
  123. dwLevel,
  124. pMem,
  125. dwPassed,
  126. &dwNeeded,
  127. lpdwReturned
  128. );
  129. //
  130. // Enumerate for Printers again with a bigger buffer if a bigger one is
  131. // needed for the result.
  132. //
  133. if (!fStatus) {
  134. if ((dwError = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) {
  135. goto error;
  136. }
  137. if (pMem) {
  138. FreeADsMem(pMem);
  139. }
  140. pMem = (LPBYTE)AllocADsMem(
  141. dwNeeded
  142. );
  143. if (!pMem) {
  144. goto error;
  145. }
  146. dwPassed = dwNeeded;
  147. fStatus = EnumPrinters(
  148. dwType,
  149. lpszName,
  150. dwLevel,
  151. pMem,
  152. dwPassed,
  153. &dwNeeded,
  154. lpdwReturned
  155. );
  156. if (!fStatus) {
  157. goto error;
  158. }
  159. }
  160. //
  161. // Return.
  162. //
  163. *lplpbPrinters = pMem;
  164. return(TRUE);
  165. error:
  166. if (pMem) {
  167. FreeADsMem(pMem);
  168. }
  169. return(FALSE);
  170. }
  171. //----------------------------------------------------------------------------
  172. //
  173. // Function: WinNTGetPrinter
  174. //
  175. // Synopsis:
  176. //
  177. //----------------------------------------------------------------------------
  178. BOOL
  179. WinNTGetPrinter(
  180. HANDLE hPrinter,
  181. DWORD dwLevel,
  182. LPBYTE *lplpbPrinters
  183. )
  184. {
  185. BOOL fStatus = FALSE;
  186. DWORD dwPassed = 1024;
  187. DWORD dwNeeded = 0;
  188. DWORD dwError = 0;
  189. LPBYTE pMem = NULL;
  190. //
  191. // Allocate memory for return buffer.
  192. //
  193. pMem = (LPBYTE)AllocADsMem(dwPassed);
  194. if (!pMem) {
  195. goto error;
  196. }
  197. //
  198. // Get printer's information.
  199. //
  200. fStatus = GetPrinter(
  201. hPrinter,
  202. dwLevel,
  203. pMem,
  204. dwPassed,
  205. &dwNeeded
  206. );
  207. //
  208. // Get printer's information again with a bigger buffer if a bigger buffer
  209. // is needed for the result.
  210. //
  211. if (!fStatus) {
  212. if ((dwError = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) {
  213. goto error;
  214. }
  215. if (pMem) {
  216. FreeADsMem(pMem);
  217. }
  218. pMem = (LPBYTE)AllocADsMem(
  219. dwNeeded
  220. );
  221. if (!pMem) {
  222. goto error;
  223. }
  224. dwPassed = dwNeeded;
  225. fStatus = GetPrinter(
  226. hPrinter,
  227. dwLevel,
  228. pMem,
  229. dwPassed,
  230. &dwNeeded
  231. );
  232. if (!fStatus) {
  233. goto error;
  234. }
  235. }
  236. //
  237. // Return.
  238. //
  239. *lplpbPrinters = pMem;
  240. return(TRUE);
  241. error:
  242. if (pMem) {
  243. FreeADsMem(pMem);
  244. }
  245. return(FALSE);
  246. }
  247. //----------------------------------------------------------------------------
  248. //
  249. // Function: GetPrinterInfo
  250. //
  251. // Synopsis:
  252. //
  253. //----------------------------------------------------------------------------
  254. HRESULT
  255. GetPrinterInfo(
  256. THIS_ LPPRINTER_INFO_2 *lplpPrinterInfo2,
  257. BSTR bstrPrinterName
  258. )
  259. {
  260. //
  261. // Do a GetPrinter call to bstrPrinterName
  262. //
  263. BOOL fStatus = FALSE;
  264. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_USE |
  265. READ_CONTROL};
  266. DWORD LastError = 0;
  267. HANDLE hPrinter = NULL;
  268. LPBYTE pMem = NULL;
  269. HRESULT hr = S_OK;
  270. ADsAssert(bstrPrinterName);
  271. //
  272. // Open a printer handle.
  273. //
  274. fStatus = OpenPrinter(
  275. (LPTSTR)bstrPrinterName,
  276. &hPrinter,
  277. &PrinterDefaults
  278. );
  279. //
  280. // If access is denied, do it again with a different access right.
  281. //
  282. if (!fStatus) {
  283. LastError = GetLastError();
  284. switch (LastError) {
  285. case ERROR_ACCESS_DENIED:
  286. {
  287. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_USE};
  288. fStatus = OpenPrinter(
  289. (LPTSTR)bstrPrinterName,
  290. &hPrinter,
  291. &PrinterDefaults
  292. );
  293. if (fStatus) {
  294. break;
  295. }
  296. }
  297. default:
  298. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  299. }
  300. }
  301. //
  302. // Get printer's info.
  303. //
  304. fStatus = WinNTGetPrinter(
  305. hPrinter,
  306. 2,
  307. (LPBYTE *)&pMem
  308. );
  309. if (!fStatus) {
  310. hr = HRESULT_FROM_WIN32(GetLastError());
  311. goto cleanup;
  312. }
  313. //
  314. // Return.
  315. //
  316. *lplpPrinterInfo2 = (LPPRINTER_INFO_2)pMem;
  317. cleanup:
  318. if(hPrinter)
  319. fStatus = ClosePrinter(hPrinter);
  320. RRETURN(hr);
  321. }
  322. //----------------------------------------------------------------------------
  323. //
  324. // Function: Set
  325. //
  326. // Synopsis:
  327. //
  328. //----------------------------------------------------------------------------
  329. HRESULT
  330. Set(
  331. LPPRINTER_INFO_2 lpPrinterInfo2,
  332. LPTSTR pszPrinterName
  333. )
  334. {
  335. BOOL fStatus = FALSE;
  336. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ALL_ACCESS};
  337. HANDLE hPrinter = NULL;
  338. HRESULT hr;
  339. ADsAssert(pszPrinterName);
  340. //
  341. // Open a printer handle.
  342. //
  343. fStatus = OpenPrinter(
  344. pszPrinterName,
  345. &hPrinter,
  346. &PrinterDefaults
  347. );
  348. if (!fStatus) {
  349. goto error;
  350. }
  351. //
  352. // Set printer's data.
  353. //
  354. fStatus = SetPrinter(
  355. hPrinter,
  356. 2,
  357. (LPBYTE)lpPrinterInfo2,
  358. 0
  359. );
  360. if (!fStatus) {
  361. goto error;
  362. }
  363. //
  364. // Return.
  365. //
  366. fStatus = ClosePrinter(hPrinter);
  367. RRETURN(S_OK);
  368. error:
  369. hr = HRESULT_FROM_WIN32(GetLastError());
  370. if(hPrinter)
  371. fStatus = ClosePrinter(hPrinter);
  372. RRETURN(hr);
  373. }
  374. //----------------------------------------------------------------------------
  375. //
  376. // Function: WinNTDeletePrinter
  377. //
  378. // Synopsis:
  379. //
  380. //----------------------------------------------------------------------------
  381. HRESULT
  382. WinNTDeletePrinter(
  383. POBJECTINFO pObjectInfo
  384. )
  385. {
  386. WCHAR szUncServerName[MAX_PATH];
  387. WCHAR szUncPrinterName[MAX_PATH];
  388. HANDLE hPrinter = NULL;
  389. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_ADMINISTER};
  390. BOOL fStatus = FALSE;
  391. HRESULT hr = S_OK;
  392. //
  393. // Make Unc Name for OpenPrinter.
  394. //
  395. MakeUncName(pObjectInfo->ComponentArray[1],
  396. szUncServerName
  397. );
  398. wcscpy(szUncPrinterName, szUncServerName);
  399. wcscat(szUncPrinterName, L"\\");
  400. wcscat(szUncPrinterName, (LPTSTR)pObjectInfo->ComponentArray[2]);
  401. //
  402. // Open a printer handle.
  403. //
  404. fStatus = OpenPrinter(
  405. (LPTSTR)szUncPrinterName,
  406. &hPrinter,
  407. &PrinterDefaults
  408. );
  409. if (!fStatus) {
  410. goto error;
  411. }
  412. //
  413. // Delete the given printer.
  414. //
  415. fStatus = DeletePrinter(hPrinter);
  416. //
  417. // Return.
  418. //
  419. if (!fStatus) {
  420. hr = GetLastError();
  421. fStatus = ClosePrinter(hPrinter);
  422. goto error;
  423. }
  424. error:
  425. RRETURN(hr);
  426. }