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.

1042 lines
24 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. printhlp.cxx
  5. Abstract:
  6. Helper functions for printer object
  7. Author:
  8. Ram Viswanathan ( ramv)
  9. Revision History:
  10. --*/
  11. #include "winnt.hxx"
  12. #pragma hdrstop
  13. /*+------------------------------------------------------------------------
  14. * Helper functions follow
  15. *-------------------------------------------------------------------------
  16. */
  17. //
  18. // mapping WinNT Status Codes to ADs Status Codes and vice versa
  19. //
  20. typedef struct _PrintStatusList {
  21. DWORD dwWinNTPrintStatus;
  22. DWORD dwADsPrintStatus;
  23. } PRINT_STATUS_LIST, *PPRINT_STATUS_LIST;
  24. PRINT_STATUS_LIST PrintStatusList[] =
  25. {
  26. {PRINTER_STATUS_PAUSED, ADS_PRINTER_PAUSED},
  27. {PRINTER_STATUS_PENDING_DELETION, ADS_PRINTER_PENDING_DELETION}
  28. };
  29. BOOL PrinterStatusWinNTToADs( DWORD dwWinNTStatus,
  30. DWORD *pdwADsStatus)
  31. {
  32. BOOL found = FALSE;
  33. int i;
  34. for (i=0;i<2;i++){
  35. if(dwWinNTStatus == PrintStatusList[i].dwWinNTPrintStatus){
  36. *pdwADsStatus = PrintStatusList[i].dwADsPrintStatus;
  37. found = TRUE;
  38. break;
  39. }
  40. }
  41. return (found);
  42. }
  43. BOOL PrinterStatusADsToWinNT( DWORD dwADsStatus,
  44. DWORD *pdwWinNTStatus)
  45. {
  46. BOOL found = FALSE;
  47. int i;
  48. for (i=0;i<2;i++){
  49. if(dwADsStatus == PrintStatusList[i].dwADsPrintStatus){
  50. *pdwWinNTStatus = PrintStatusList[i].dwWinNTPrintStatus;
  51. found = TRUE;
  52. break;
  53. }
  54. }
  55. return (found);
  56. }
  57. BOOL
  58. WinNTEnumPrinters(
  59. DWORD dwType,
  60. LPTSTR lpszName,
  61. DWORD dwLevel,
  62. LPBYTE *lplpbPrinters,
  63. LPDWORD lpdwReturned
  64. )
  65. {
  66. BOOL bStatus = FALSE;
  67. DWORD dwPassed = 1024;
  68. DWORD dwNeeded = 0;
  69. DWORD dwError = 0;
  70. LPBYTE pMem = NULL;
  71. pMem = (LPBYTE)AllocADsMem(dwPassed);
  72. if (!pMem) {
  73. goto error;
  74. }
  75. bStatus = EnumPrinters(dwType,
  76. lpszName,
  77. dwLevel,
  78. pMem,
  79. dwPassed,
  80. &dwNeeded,
  81. lpdwReturned);
  82. if (!bStatus) {
  83. if ((dwError = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) {
  84. goto error;
  85. }
  86. if (pMem) {
  87. FreeADsMem(pMem);
  88. }
  89. pMem = (LPBYTE)AllocADsMem(dwNeeded);
  90. if (!pMem) {
  91. goto error;
  92. }
  93. dwPassed = dwNeeded;
  94. bStatus = EnumPrinters(dwType,
  95. lpszName,
  96. dwLevel,
  97. pMem,
  98. dwPassed,
  99. &dwNeeded,
  100. lpdwReturned);
  101. if (!bStatus) {
  102. goto error;
  103. }
  104. }
  105. *lplpbPrinters = pMem;
  106. return(TRUE);
  107. error:
  108. if (pMem) {
  109. FreeADsMem(pMem);
  110. }
  111. return(FALSE);
  112. }
  113. BOOL
  114. WinNTGetPrinter(HANDLE hPrinter,
  115. DWORD dwLevel,
  116. LPBYTE *lplpbPrinters)
  117. {
  118. BOOL bStatus = FALSE;
  119. DWORD dwPassed = 1024;
  120. DWORD dwNeeded = 0;
  121. DWORD dwError = 0;
  122. LPBYTE pMem = NULL;
  123. pMem = (LPBYTE)AllocADsMem(dwPassed);
  124. if (!pMem) {
  125. goto error;
  126. }
  127. bStatus = GetPrinter(hPrinter,
  128. dwLevel,
  129. pMem,
  130. dwPassed,
  131. &dwNeeded);
  132. if (!bStatus) {
  133. if ((dwError = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) {
  134. goto error;
  135. }
  136. if (pMem) {
  137. FreeADsMem(pMem);
  138. }
  139. pMem = (LPBYTE)AllocADsMem(dwNeeded);
  140. if (!pMem) {
  141. goto error;
  142. }
  143. dwPassed = dwNeeded;
  144. bStatus = GetPrinter(hPrinter,
  145. dwLevel,
  146. pMem,
  147. dwPassed,
  148. &dwNeeded);
  149. if (!bStatus) {
  150. goto error;
  151. }
  152. }
  153. *lplpbPrinters = pMem;
  154. return(TRUE);
  155. error:
  156. if (pMem) {
  157. FreeADsMem(pMem);
  158. }
  159. return(FALSE);
  160. }
  161. HRESULT
  162. GetPrinterInfo(
  163. THIS_ LPPRINTER_INFO_2 *lplpPrinterInfo2,
  164. LPWSTR pszPrinterName
  165. )
  166. {
  167. //
  168. // Do a GetPrinter call to pszPrinterName
  169. //
  170. BOOL fStatus = FALSE;
  171. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_USE |
  172. READ_CONTROL};
  173. DWORD LastError = 0;
  174. HANDLE hPrinter = NULL;
  175. LPBYTE pMem = NULL;
  176. HRESULT hr = S_OK;
  177. ADsAssert(pszPrinterName);
  178. fStatus = OpenPrinter(pszPrinterName,
  179. &hPrinter,
  180. &PrinterDefaults
  181. );
  182. if (!fStatus) {
  183. LastError = GetLastError();
  184. switch (LastError) {
  185. case ERROR_ACCESS_DENIED:
  186. {
  187. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_USE};
  188. fStatus = OpenPrinter(pszPrinterName,
  189. &hPrinter,
  190. &PrinterDefaults
  191. );
  192. if (fStatus) {
  193. break;
  194. }
  195. }
  196. default:
  197. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  198. }
  199. }
  200. fStatus = WinNTGetPrinter(hPrinter,
  201. 2,
  202. (LPBYTE *)&pMem
  203. );
  204. if (!fStatus) {
  205. hr = HRESULT_FROM_WIN32(GetLastError());
  206. goto cleanup;
  207. }
  208. *lplpPrinterInfo2 = (LPPRINTER_INFO_2)pMem;
  209. cleanup:
  210. if(hPrinter)
  211. fStatus = ClosePrinter(hPrinter);
  212. RRETURN(hr);
  213. }
  214. //+---------------------------------------------------------------------------
  215. //
  216. // Function: Set
  217. //
  218. // Synopsis: Helper function called by CADsPrintQueue:SetInfo
  219. //
  220. // Arguments:
  221. //
  222. // Returns: HRESULT.
  223. //
  224. // Modifies:
  225. //
  226. // History: 11-08-95 RamV Created
  227. //
  228. //----------------------------------------------------------------------------
  229. HRESULT
  230. Set(
  231. LPPRINTER_INFO_2 lpPrinterInfo2,
  232. LPTSTR pszPrinterName
  233. )
  234. {
  235. BOOL fStatus = FALSE;
  236. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ALL_ACCESS};
  237. HANDLE hPrinter = NULL;
  238. HRESULT hr;
  239. ADsAssert(pszPrinterName);
  240. fStatus = OpenPrinter(pszPrinterName,
  241. &hPrinter,
  242. &PrinterDefaults
  243. );
  244. if (!fStatus) {
  245. goto error;
  246. }
  247. fStatus = SetPrinter(hPrinter,
  248. 2,
  249. (LPBYTE)lpPrinterInfo2,
  250. 0
  251. );
  252. if (!fStatus) {
  253. goto error;
  254. }
  255. fStatus = ClosePrinter(hPrinter);
  256. RRETURN(S_OK);
  257. error:
  258. hr = HRESULT_FROM_WIN32(GetLastError());
  259. if(hPrinter)
  260. fStatus = ClosePrinter(hPrinter);
  261. RRETURN(hr);
  262. }
  263. //+---------------------------------------------------------------------------
  264. //
  265. // Function: Unmarshall
  266. //
  267. // Synopsis: Unmarshalls information from a PRINTER_INFO_2 to a
  268. // WinNT Printer object.Frees PRINTER_INFO_2 object.
  269. //
  270. // Arguments: [lpPrinterInfo2] -- Pointer to a PRINTER_INFO_2 struct
  271. //
  272. // Returns: HRESULT
  273. //
  274. // Modifies: GeneralInfo and Operation Functional sets
  275. //
  276. // History: 11/08/95 RamV Created
  277. //
  278. //----------------------------------------------------------------------------
  279. HRESULT
  280. CWinNTPrintQueue::UnMarshall(
  281. LPPRINTER_INFO_2 lpPrinterInfo2,
  282. BOOL fExplicit
  283. )
  284. {
  285. HRESULT hr;
  286. VARIANT vPortNames;
  287. PNTOBJECT pNtObject;
  288. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  289. TEXT("PrinterName"),
  290. lpPrinterInfo2->pPrinterName,
  291. fExplicit
  292. );
  293. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  294. TEXT("Model"),
  295. lpPrinterInfo2->pDriverName,
  296. fExplicit
  297. );
  298. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  299. TEXT("PrintProcessor"),
  300. lpPrinterInfo2->pPrintProcessor,
  301. fExplicit
  302. );
  303. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  304. TEXT("Location"),
  305. lpPrinterInfo2->pLocation,
  306. fExplicit
  307. );
  308. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  309. TEXT("Datatype"),
  310. lpPrinterInfo2->pDatatype,
  311. fExplicit
  312. );
  313. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  314. TEXT("BannerPage"),
  315. lpPrinterInfo2->pSepFile,
  316. fExplicit
  317. );
  318. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  319. TEXT("Description"),
  320. lpPrinterInfo2->pComment,
  321. fExplicit
  322. );
  323. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  324. TEXT("PrinterPath"),
  325. lpPrinterInfo2->pPrinterName,
  326. fExplicit
  327. );
  328. hr = SetDWORDPropertyInCache(_pPropertyCache,
  329. TEXT("Priority"),
  330. lpPrinterInfo2->Priority,
  331. fExplicit
  332. );
  333. hr = SetDelimitedStringPropertyInCache(_pPropertyCache,
  334. TEXT("PrintDevices"),
  335. lpPrinterInfo2->pPortName,
  336. fExplicit
  337. );
  338. hr = SetDWORDPropertyInCache(_pPropertyCache,
  339. TEXT("DefaultJobPriority"),
  340. lpPrinterInfo2->DefaultPriority,
  341. fExplicit
  342. );
  343. hr = SetDWORDPropertyInCache(_pPropertyCache,
  344. TEXT("JobCount"),
  345. lpPrinterInfo2->cJobs,
  346. fExplicit
  347. );
  348. hr = SetDWORDPropertyInCache(_pPropertyCache,
  349. TEXT("Attributes"),
  350. lpPrinterInfo2->Attributes,
  351. fExplicit
  352. );
  353. hr = SetDATEPropertyInCache(_pPropertyCache,
  354. TEXT("StartTime"),
  355. lpPrinterInfo2->StartTime,
  356. fExplicit
  357. );
  358. hr = SetDATEPropertyInCache(_pPropertyCache,
  359. TEXT("UntilTime"),
  360. lpPrinterInfo2->UntilTime,
  361. fExplicit
  362. );
  363. hr = SetLPTSTRPropertyInCache(
  364. _pPropertyCache,
  365. TEXT("Name"),
  366. _Name,
  367. fExplicit
  368. );
  369. RRETURN(S_OK);
  370. }
  371. //+---------------------------------------------------------------------------
  372. //
  373. // Function: MarshallAndSet
  374. //
  375. // Synopsis: Marshalls information from a Printer object to a
  376. // PRINTER_INFO_2 structure
  377. //
  378. // Arguments: [lpPrinterInfo2] -- Pointer to a PRINTER_INFO_2 struct.
  379. //
  380. // Returns: HRESULT
  381. //
  382. // Modifies:
  383. //
  384. // History: 11/09/95 RamV Created
  385. //
  386. //----------------------------------------------------------------------------
  387. HRESULT
  388. CWinNTPrintQueue::MarshallAndSet(
  389. LPPRINTER_INFO_2 lpPrinterInfo2
  390. )
  391. {
  392. HRESULT hr =S_OK;
  393. DWORD dwPriority;
  394. LPTSTR pszPrinterName = NULL;
  395. LPTSTR pszDriverName = NULL;
  396. LPTSTR pszComment = NULL;
  397. LPTSTR pszLocation = NULL;
  398. LPTSTR pszDatatype = NULL;
  399. LPTSTR pszPrintProcessor = NULL;
  400. LPTSTR pszBannerPage = NULL;
  401. VARIANT vPortNames;
  402. PNTOBJECT pNtObject = NULL;
  403. LPTSTR pszPorts = NULL;
  404. DWORD dwSyntaxId;
  405. DWORD dwTimeValue;
  406. DWORD dwNumValues = 0;
  407. DWORD dwAttributes;
  408. DWORD dwUpdatedProps = 0;
  409. //
  410. // We can set the update variable based on the number
  411. // of properties in the cache.
  412. //
  413. hr = _pPropertyCache->get_PropertyCount(&dwUpdatedProps);
  414. if (dwUpdatedProps != 0) {
  415. //
  416. // We can do all this to get the props
  417. //
  418. hr = GetLPTSTRPropertyFromCache(
  419. _pPropertyCache,
  420. TEXT("PrinterName"),
  421. &pszPrinterName
  422. );
  423. if(SUCCEEDED(hr)){
  424. lpPrinterInfo2->pPrinterName= pszPrinterName;
  425. }
  426. hr = GetLPTSTRPropertyFromCache(
  427. _pPropertyCache,
  428. TEXT("Model"),
  429. &pszDriverName
  430. );
  431. if(SUCCEEDED(hr)){
  432. lpPrinterInfo2->pDriverName = pszDriverName;
  433. }
  434. hr = GetLPTSTRPropertyFromCache(
  435. _pPropertyCache,
  436. TEXT("PrintProcessor"),
  437. &pszPrintProcessor
  438. );
  439. if(SUCCEEDED(hr)){
  440. lpPrinterInfo2->pPrintProcessor = pszPrintProcessor;
  441. }
  442. hr = GetLPTSTRPropertyFromCache(
  443. _pPropertyCache,
  444. TEXT("Description"),
  445. &pszComment
  446. );
  447. if(SUCCEEDED(hr)){
  448. lpPrinterInfo2->pComment = pszComment;
  449. }
  450. hr = GetLPTSTRPropertyFromCache(
  451. _pPropertyCache,
  452. TEXT("Location"),
  453. &pszLocation
  454. );
  455. if(SUCCEEDED(hr)){
  456. lpPrinterInfo2->pLocation = pszLocation;
  457. }
  458. hr = GetLPTSTRPropertyFromCache(
  459. _pPropertyCache,
  460. TEXT("Datatype"),
  461. &pszDatatype
  462. );
  463. if(SUCCEEDED(hr)){
  464. lpPrinterInfo2->pDatatype = pszDatatype;
  465. }
  466. hr = GetLPTSTRPropertyFromCache(
  467. _pPropertyCache,
  468. TEXT("BannerPage"),
  469. &pszBannerPage
  470. );
  471. if(SUCCEEDED(hr)){
  472. lpPrinterInfo2->pSepFile = pszBannerPage;
  473. }
  474. hr = GetDWORDPropertyFromCache(
  475. _pPropertyCache,
  476. TEXT("Priority"),
  477. &dwPriority
  478. );
  479. if(SUCCEEDED(hr)){
  480. lpPrinterInfo2->Priority = dwPriority;
  481. }
  482. hr = GetDWORDPropertyFromCache(
  483. _pPropertyCache,
  484. TEXT("DefaultJobPriority"),
  485. &dwPriority
  486. );
  487. if(SUCCEEDED(hr)){
  488. lpPrinterInfo2->DefaultPriority = dwPriority;
  489. }
  490. //
  491. // will NOT marshall or set job count on the server
  492. //
  493. hr = GetDWORDPropertyFromCache(
  494. _pPropertyCache,
  495. TEXT("Attributes"),
  496. &dwAttributes
  497. );
  498. if(SUCCEEDED(hr)){
  499. lpPrinterInfo2->Attributes = dwAttributes;
  500. }
  501. hr = GetDelimitedStringPropertyFromCache(
  502. _pPropertyCache,
  503. TEXT("PrintDevices"),
  504. &pszPorts
  505. );
  506. if(SUCCEEDED(hr)){
  507. lpPrinterInfo2->pPortName = pszPorts;
  508. }
  509. hr = GetDATEPropertyFromCache(
  510. _pPropertyCache,
  511. TEXT("StartTime"),
  512. &dwTimeValue
  513. );
  514. if(SUCCEEDED(hr)){
  515. lpPrinterInfo2->StartTime = dwTimeValue;
  516. }
  517. hr = GetDATEPropertyFromCache(
  518. _pPropertyCache,
  519. TEXT("UntilTime"),
  520. &dwTimeValue
  521. );
  522. if(SUCCEEDED(hr)){
  523. lpPrinterInfo2->UntilTime = dwTimeValue;
  524. }
  525. //
  526. // Need to do the set
  527. //
  528. hr = Set(lpPrinterInfo2,
  529. _pszPrinterName);
  530. //
  531. // only this hr gets recorded
  532. //
  533. BAIL_IF_ERROR(hr);
  534. }
  535. cleanup:
  536. if(pszDriverName)
  537. FreeADsStr(pszDriverName);
  538. if(pszComment)
  539. FreeADsStr(pszComment);
  540. if(pszLocation)
  541. FreeADsStr(pszLocation);
  542. if(pszDatatype)
  543. FreeADsStr(pszDatatype);
  544. if(pszPrintProcessor)
  545. FreeADsStr(pszPrintProcessor);
  546. if(pszBannerPage)
  547. FreeADsStr(pszBannerPage);
  548. if(pszPorts){
  549. FreeADsStr(pszPorts);
  550. }
  551. RRETURN(hr);
  552. }
  553. HRESULT
  554. WinNTDeletePrinter( POBJECTINFO pObjectInfo)
  555. {
  556. WCHAR szUncServerName[MAX_PATH];
  557. WCHAR szUncPrinterName[MAX_PATH];
  558. HANDLE hPrinter = NULL;
  559. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ALL_ACCESS};
  560. BOOL fStatus = FALSE;
  561. HRESULT hr = S_OK;
  562. //
  563. // first open printer to get a handle to it
  564. //
  565. MakeUncName(pObjectInfo->ComponentArray[1],
  566. szUncServerName
  567. );
  568. wcscpy(szUncPrinterName, szUncServerName);
  569. wcscat(szUncPrinterName, L"\\");
  570. wcscat(szUncPrinterName, (LPTSTR)pObjectInfo->ComponentArray[2]);
  571. fStatus = OpenPrinter((LPTSTR)szUncPrinterName,
  572. &hPrinter,
  573. &PrinterDefaults
  574. );
  575. if (!fStatus) {
  576. hr = HRESULT_FROM_WIN32(GetLastError());
  577. goto error;
  578. }
  579. fStatus = DeletePrinter(hPrinter);
  580. if (!fStatus) {
  581. hr = HRESULT_FROM_WIN32(GetLastError());
  582. fStatus = ClosePrinter(hPrinter);
  583. goto error;
  584. }
  585. error:
  586. RRETURN(hr);
  587. }
  588. HRESULT
  589. PrinterNameFromObjectInfo(
  590. POBJECTINFO pObjectInfo,
  591. LPTSTR szUncPrinterName
  592. )
  593. {
  594. if(!pObjectInfo){
  595. RRETURN(S_OK);
  596. }
  597. if(!((pObjectInfo->NumComponents == 3) ||(pObjectInfo->NumComponents == 4)) ){
  598. RRETURN(E_ADS_BAD_PATHNAME);
  599. }
  600. if(pObjectInfo->NumComponents == 3) {
  601. wcscpy(szUncPrinterName, TEXT("\\\\"));
  602. wcscat(szUncPrinterName, pObjectInfo->ComponentArray[1]);
  603. wcscat(szUncPrinterName, TEXT("\\"));
  604. wcscat(szUncPrinterName, pObjectInfo->ComponentArray[2]);
  605. } else {
  606. wcscpy(szUncPrinterName, TEXT("\\\\"));
  607. wcscat(szUncPrinterName, pObjectInfo->ComponentArray[0]);
  608. wcscat(szUncPrinterName, TEXT("\\"));
  609. wcscat(szUncPrinterName, pObjectInfo->ComponentArray[1]);
  610. }
  611. RRETURN(S_OK);
  612. }
  613. #if (!defined(BUILD_FOR_NT40))
  614. //+---------------------------------------------------------------------------
  615. //
  616. // Function: MarshallAndSet
  617. //
  618. // Synopsis: Marshalls information from a Printer object to a
  619. // PRINTER_INFO_7 structure
  620. //
  621. // Arguments: [lpPrinterInfo7] -- Pointer to a PRINTER_INFO_2 struct.
  622. //
  623. // Returns: HRESULT
  624. //
  625. // Modifies:
  626. //
  627. // History: 11/09/95 RamV Created
  628. //
  629. //----------------------------------------------------------------------------
  630. HRESULT
  631. CWinNTPrintQueue::MarshallAndSet(
  632. LPPRINTER_INFO_7 lpPrinterInfo7
  633. )
  634. {
  635. HRESULT hr =S_OK;
  636. DWORD dwAction;
  637. LPTSTR pszObjectGUID = NULL;
  638. BOOL fSetInfoNeeded = FALSE;
  639. hr = GetLPTSTRPropertyFromCache(
  640. _pPropertyCache,
  641. TEXT("ObjectGUID"),
  642. &pszObjectGUID
  643. );
  644. if(SUCCEEDED(hr)){
  645. fSetInfoNeeded = TRUE;
  646. lpPrinterInfo7->pszObjectGUID = pszObjectGUID;
  647. }
  648. hr = GetDWORDPropertyFromCache(
  649. _pPropertyCache,
  650. TEXT("Action"),
  651. &dwAction
  652. );
  653. if(SUCCEEDED(hr)){
  654. fSetInfoNeeded = TRUE;
  655. lpPrinterInfo7->dwAction = dwAction;
  656. }
  657. //
  658. // Need to do the set if flag is set
  659. //
  660. if (fSetInfoNeeded) {
  661. hr = SetPrinter7(lpPrinterInfo7,
  662. _pszPrinterName);
  663. //
  664. // only this hr gets recorded
  665. //
  666. BAIL_IF_ERROR(hr);
  667. }
  668. else {
  669. //
  670. // Need to this - if we are here it means that the hr is
  671. // probably set as prop was not found in cache.
  672. hr = S_OK;
  673. }
  674. cleanup:
  675. if(pszObjectGUID)
  676. FreeADsStr(pszObjectGUID);
  677. RRETURN(hr);
  678. }
  679. HRESULT
  680. GetPrinterInfo7(
  681. THIS_ LPPRINTER_INFO_7 *lplpPrinterInfo7,
  682. LPWSTR pszPrinterName
  683. )
  684. {
  685. //
  686. // Do a GetPrinter call to pszPrinterName
  687. //
  688. BOOL fStatus = FALSE;
  689. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_USE |
  690. READ_CONTROL};
  691. DWORD LastError = 0;
  692. HANDLE hPrinter = NULL;
  693. LPBYTE pMem = NULL;
  694. HRESULT hr = S_OK;
  695. ADsAssert(pszPrinterName);
  696. fStatus = OpenPrinter(pszPrinterName,
  697. &hPrinter,
  698. &PrinterDefaults
  699. );
  700. if (!fStatus) {
  701. LastError = GetLastError();
  702. switch (LastError) {
  703. case ERROR_ACCESS_DENIED:
  704. {
  705. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_USE};
  706. fStatus = OpenPrinter(pszPrinterName,
  707. &hPrinter,
  708. &PrinterDefaults
  709. );
  710. if (fStatus) {
  711. break;
  712. }
  713. }
  714. default:
  715. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  716. }
  717. }
  718. fStatus = WinNTGetPrinter(hPrinter,
  719. 7,
  720. (LPBYTE *)&pMem
  721. );
  722. if (!fStatus) {
  723. hr = HRESULT_FROM_WIN32(GetLastError());
  724. goto cleanup;
  725. }
  726. *lplpPrinterInfo7 = (LPPRINTER_INFO_7)pMem;
  727. cleanup:
  728. if(hPrinter)
  729. fStatus = ClosePrinter(hPrinter);
  730. RRETURN(hr);
  731. }
  732. HRESULT
  733. SetPrinter7(
  734. LPPRINTER_INFO_7 lpPrinterInfo7,
  735. LPTSTR pszPrinterName
  736. )
  737. {
  738. BOOL fStatus = FALSE;
  739. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ALL_ACCESS};
  740. HANDLE hPrinter = NULL;
  741. HRESULT hr;
  742. ADsAssert(pszPrinterName);
  743. fStatus = OpenPrinter(pszPrinterName,
  744. &hPrinter,
  745. &PrinterDefaults
  746. );
  747. if (!fStatus) {
  748. goto error;
  749. }
  750. fStatus = SetPrinter(hPrinter,
  751. 7,
  752. (LPBYTE)lpPrinterInfo7,
  753. 0
  754. );
  755. if (!fStatus) {
  756. goto error;
  757. }
  758. fStatus = ClosePrinter(hPrinter);
  759. RRETURN(S_OK);
  760. error:
  761. hr = HRESULT_FROM_WIN32(GetLastError());
  762. if(hPrinter)
  763. fStatus = ClosePrinter(hPrinter);
  764. RRETURN(hr);
  765. }
  766. //+---------------------------------------------------------------------------
  767. //
  768. // Function: Unmarshall
  769. //
  770. // Synopsis: Unmarshalls information from a PRINTER_INFO_2 to a
  771. // WinNT Printer object.Frees PRINTER_INFO_2 object.
  772. //
  773. // Arguments: [lpPrinterInfo2] -- Pointer to a PRINTER_INFO_2 struct
  774. //
  775. // Returns: HRESULT
  776. //
  777. // Modifies: GeneralInfo and Operation Functional sets
  778. //
  779. // History: 11/08/95 RamV Created
  780. //
  781. //----------------------------------------------------------------------------
  782. HRESULT
  783. CWinNTPrintQueue::UnMarshall7(
  784. LPPRINTER_INFO_7 lpPrinterInfo7,
  785. BOOL fExplicit
  786. )
  787. {
  788. HRESULT hr;
  789. VARIANT vPortNames;
  790. PNTOBJECT pNtObject;
  791. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  792. TEXT("ObjectGUID"),
  793. lpPrinterInfo7->pszObjectGUID,
  794. fExplicit
  795. );
  796. hr = SetDWORDPropertyInCache(_pPropertyCache,
  797. TEXT("Action"),
  798. lpPrinterInfo7->dwAction,
  799. fExplicit
  800. );
  801. RRETURN(S_OK);
  802. }
  803. #endif