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.

836 lines
19 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: cprinter.cxx
  7. //
  8. // Contents: CNWCOMPATPrinter
  9. //
  10. //
  11. // History: 30-Apr-96 t-ptam (Patrick Tam) Created.
  12. //
  13. //----------------------------------------------------------------------------
  14. #include "NWCOMPAT.hxx"
  15. #pragma hdrstop
  16. //
  17. // Printer Class
  18. //
  19. //
  20. // Macro-ized implementation.
  21. //
  22. DEFINE_IDispatch_ExtMgr_Implementation(CNWCOMPATPrintQueue)
  23. DEFINE_IADs_TempImplementation(CNWCOMPATPrintQueue)
  24. DEFINE_IADs_PutGetImplementation(CNWCOMPATPrintQueue, PrintQueueClass, gdwPrinterTableSize)
  25. DEFINE_IADsPropertyList_Implementation(CNWCOMPATPrintQueue, PrintQueueClass, gdwPrinterTableSize)
  26. //----------------------------------------------------------------------------
  27. //
  28. // Function: CNWCOMPATPrintQueue::CNWCOMPATPrintQueue
  29. //
  30. // Synopsis:
  31. //
  32. //----------------------------------------------------------------------------
  33. CNWCOMPATPrintQueue::CNWCOMPATPrintQueue():
  34. _pDispMgr(NULL),
  35. _pExtMgr(NULL)
  36. {
  37. ENLIST_TRACKING(CNWCOMPATPrintQueue);
  38. _pPropertyCache = NULL;
  39. }
  40. //----------------------------------------------------------------------------
  41. //
  42. // Function: CNWCOMPATPrintQueue::~CNWCOMPATPrintQueue
  43. //
  44. // Synopsis:
  45. //
  46. //----------------------------------------------------------------------------
  47. CNWCOMPATPrintQueue::~CNWCOMPATPrintQueue()
  48. {
  49. delete _pExtMgr; // created last, destroyed first
  50. delete _pDispMgr;
  51. delete _pPropertyCache;
  52. }
  53. //----------------------------------------------------------------------------
  54. //
  55. // Function: CNWCOMPATPrintQueue:: CreatePrintQueue
  56. //
  57. // Synopsis:
  58. //
  59. //----------------------------------------------------------------------------
  60. HRESULT
  61. CNWCOMPATPrintQueue:: CreatePrintQueue(
  62. LPTSTR pszADsParent,
  63. LPTSTR pszPrinterName,
  64. DWORD dwObjectState,
  65. REFIID riid,
  66. LPVOID * ppvoid
  67. )
  68. {
  69. CNWCOMPATPrintQueue *pPrintQueue = NULL;
  70. HRESULT hr = S_OK;
  71. LPWSTR lpszTempName = NULL;
  72. //
  73. // Create the printer object
  74. //
  75. hr = AllocatePrintQueueObject(
  76. &pPrintQueue
  77. );
  78. BAIL_ON_FAILURE(hr);
  79. //
  80. // Initialize the core object
  81. //
  82. hr = pPrintQueue->InitializeCoreObject(
  83. pszADsParent,
  84. pszPrinterName,
  85. PRINTER_CLASS_NAME,
  86. PRINTER_SCHEMA_NAME,
  87. CLSID_NWCOMPATPrintQueue,
  88. dwObjectState
  89. );
  90. BAIL_ON_FAILURE(hr);
  91. //
  92. // Query for the specified interface.
  93. //
  94. hr = pPrintQueue->QueryInterface(
  95. riid,
  96. (void **)ppvoid
  97. );
  98. BAIL_ON_FAILURE(hr);
  99. //
  100. // Make Unc Printer name for Win32 API.
  101. //
  102. hr = BuildPrinterNameFromADsPath(
  103. pszADsParent,
  104. pszPrinterName,
  105. pPrintQueue->_szUncPrinterName
  106. );
  107. BAIL_ON_FAILURE(hr);
  108. //
  109. // Return.
  110. //
  111. pPrintQueue->Release();
  112. hr = pPrintQueue->_pExtMgr->FinalInitializeExtensions();
  113. BAIL_ON_FAILURE(hr);
  114. RRETURN(hr);
  115. error:
  116. delete pPrintQueue;
  117. RRETURN_EXP_IF_ERR(hr);
  118. }
  119. //----------------------------------------------------------------------------
  120. //
  121. // Function: CNWCOMPATPrintQueue::QueryInterface
  122. //
  123. // Synopsis:
  124. //
  125. //----------------------------------------------------------------------------
  126. STDMETHODIMP
  127. CNWCOMPATPrintQueue::QueryInterface(
  128. REFIID riid,
  129. LPVOID FAR* ppvObj
  130. )
  131. {
  132. if(!ppvObj)
  133. {
  134. RRETURN(E_POINTER);
  135. }
  136. if (IsEqualIID(riid, IID_IUnknown))
  137. {
  138. *ppvObj = (IADsPrintQueue FAR *)this;
  139. }
  140. else if (IsEqualIID(riid, IID_IDispatch))
  141. {
  142. *ppvObj = (IADsPrintQueue FAR *)this;
  143. }
  144. else if (IsEqualIID(riid, IID_ISupportErrorInfo))
  145. {
  146. *ppvObj = (ISupportErrorInfo FAR *) this;
  147. }
  148. else if (IsEqualIID(riid, IID_IADs))
  149. {
  150. *ppvObj = (IADsPrintQueue FAR *) this;
  151. }
  152. else if (IsEqualIID(riid, IID_IADsPrintQueue))
  153. {
  154. *ppvObj = (IADsPrintQueue FAR *) this;
  155. }
  156. else if (IsEqualIID(riid, IID_IADsPrintQueueOperations))
  157. {
  158. *ppvObj = (IADsPrintQueueOperations FAR *) this;
  159. }
  160. else if (IsEqualIID(riid, IID_IADsPropertyList))
  161. {
  162. *ppvObj = (IADsPropertyList FAR *) this;
  163. }
  164. else if (_pExtMgr)
  165. {
  166. RRETURN( _pExtMgr->QueryInterface(riid, ppvObj));
  167. }
  168. else
  169. {
  170. *ppvObj = NULL;
  171. RRETURN(E_NOINTERFACE);
  172. }
  173. AddRef();
  174. return NOERROR;
  175. }
  176. //----------------------------------------------------------------------------
  177. //
  178. // Function: CNWCOMPATPrintQueue::InterfaceSupportsErrorInfo
  179. //
  180. // Synopsis:
  181. //
  182. //----------------------------------------------------------------------------
  183. STDMETHODIMP
  184. CNWCOMPATPrintQueue::InterfaceSupportsErrorInfo(
  185. THIS_ REFIID riid
  186. )
  187. {
  188. if (IsEqualIID(riid, IID_IADs) ||
  189. IsEqualIID(riid, IID_IADsPrintQueue) ||
  190. IsEqualIID(riid, IID_IADsPrintQueueOperations) ||
  191. IsEqualIID(riid, IID_IADsPropertyList)) {
  192. RRETURN(S_OK);
  193. } else {
  194. RRETURN(S_FALSE);
  195. }
  196. }
  197. //----------------------------------------------------------------------------
  198. //
  199. // Function: CNWCOMPATPrintQueue::SetInfo
  200. //
  201. // Synopsis:
  202. //
  203. //----------------------------------------------------------------------------
  204. STDMETHODIMP
  205. CNWCOMPATPrintQueue::SetInfo(THIS)
  206. {
  207. HANDLE hPrinter = NULL;
  208. HRESULT hr = S_OK;
  209. LPBYTE lpbBuffer = NULL;
  210. //
  211. // Make sure object is bound to a tangible resource.
  212. //
  213. if (GetObjectState() == ADS_OBJECT_UNBOUND) {
  214. /*
  215. hr = NWApiAddPrinter();
  216. BAIL_ON_FAILURE(hr);
  217. */
  218. SetObjectState(ADS_OBJECT_BOUND);
  219. }
  220. //
  221. // Open a handle to a printer.
  222. //
  223. hr = NWApiOpenPrinter(
  224. _szUncPrinterName,
  225. &hPrinter,
  226. PRINTER_ALL_ACCESS
  227. );
  228. BAIL_ON_FAILURE(hr);
  229. //
  230. // Get Printer Info structure.
  231. //
  232. hr = NWApiGetPrinter(
  233. hPrinter,
  234. WIN32_API_LEVEL_2,
  235. &lpbBuffer
  236. );
  237. BAIL_ON_FAILURE(hr);
  238. //
  239. // Set info.
  240. //
  241. hr = MarshallAndSet(
  242. hPrinter,
  243. (LPPRINTER_INFO_2) lpbBuffer
  244. );
  245. error:
  246. //
  247. // Close Printer handle.
  248. //
  249. if (hPrinter) {
  250. NWApiClosePrinter(hPrinter);
  251. }
  252. if (lpbBuffer) {
  253. FreeADsMem((void*)lpbBuffer);
  254. }
  255. RRETURN_EXP_IF_ERR(hr);
  256. }
  257. //----------------------------------------------------------------------------
  258. //
  259. // Function: CNWCOMPATPrintQueue::GetInfo
  260. //
  261. // Synopsis:
  262. //
  263. //----------------------------------------------------------------------------
  264. STDMETHODIMP
  265. CNWCOMPATPrintQueue::GetInfo(THIS)
  266. {
  267. _pPropertyCache->flushpropcache();
  268. RRETURN (GetInfo(
  269. TRUE,
  270. PRINTER_API_LEVEL
  271. ));
  272. }
  273. //----------------------------------------------------------------------------
  274. //
  275. // Function: CNWCOMPATPrintQueue::AllocatePrintQueueObject
  276. //
  277. // Synopsis:
  278. //
  279. //----------------------------------------------------------------------------
  280. HRESULT
  281. CNWCOMPATPrintQueue::AllocatePrintQueueObject(
  282. CNWCOMPATPrintQueue FAR * FAR * ppPrintQueue
  283. )
  284. {
  285. CNWCOMPATPrintQueue FAR * pPrintQueue = NULL;
  286. CAggregatorDispMgr FAR * pDispMgr = NULL;
  287. CADsExtMgr FAR * pExtensionMgr = NULL;
  288. HRESULT hr = S_OK;
  289. //
  290. // Allocate memory for a PrintQueue object.
  291. //
  292. pPrintQueue = new CNWCOMPATPrintQueue();
  293. if (pPrintQueue == NULL) {
  294. hr = E_OUTOFMEMORY;
  295. }
  296. BAIL_ON_FAILURE(hr);
  297. //
  298. // Create a Dispatch Manager object.
  299. //
  300. pDispMgr = new CAggregatorDispMgr;
  301. if (pDispMgr == NULL) {
  302. hr = E_OUTOFMEMORY;
  303. }
  304. BAIL_ON_FAILURE(hr);
  305. //
  306. // Load type info.
  307. //
  308. hr = LoadTypeInfoEntry(
  309. pDispMgr,
  310. LIBID_ADs,
  311. IID_IADsPrintQueue,
  312. (IADsPrintQueue *) pPrintQueue,
  313. DISPID_REGULAR
  314. );
  315. BAIL_ON_FAILURE(hr);
  316. hr = LoadTypeInfoEntry(
  317. pDispMgr,
  318. LIBID_ADs,
  319. IID_IADsPrintQueueOperations,
  320. (IADsPrintQueueOperations *) pPrintQueue,
  321. DISPID_REGULAR
  322. );
  323. BAIL_ON_FAILURE(hr);
  324. hr = LoadTypeInfoEntry(
  325. pDispMgr,
  326. LIBID_ADs,
  327. IID_IADsPropertyList,
  328. (IADsPropertyList *) pPrintQueue,
  329. DISPID_VALUE
  330. );
  331. BAIL_ON_FAILURE(hr);
  332. hr = CPropertyCache::createpropertycache(
  333. PrintQueueClass,
  334. gdwPrinterTableSize,
  335. (CCoreADsObject *)pPrintQueue,
  336. &(pPrintQueue->_pPropertyCache)
  337. );
  338. BAIL_ON_FAILURE(hr);
  339. pDispMgr->RegisterPropertyCache(
  340. pPrintQueue->_pPropertyCache
  341. );
  342. hr = ADSILoadExtensionManager(
  343. PRINTER_CLASS_NAME,
  344. (IADsPrintQueue *) pPrintQueue,
  345. pDispMgr,
  346. &pExtensionMgr
  347. );
  348. BAIL_ON_FAILURE(hr);
  349. //
  350. // Return.
  351. //
  352. pPrintQueue->_pDispMgr = pDispMgr;
  353. pPrintQueue->_pExtMgr = pExtensionMgr;
  354. *ppPrintQueue = pPrintQueue;
  355. RRETURN(hr);
  356. error:
  357. delete pDispMgr;
  358. delete pPrintQueue;
  359. delete pExtensionMgr;
  360. RRETURN(hr);
  361. }
  362. //----------------------------------------------------------------------------
  363. //
  364. // Function: CNWCOMPATPrintQueue::GetInfo
  365. //
  366. // Synopsis:
  367. //
  368. //----------------------------------------------------------------------------
  369. STDMETHODIMP
  370. CNWCOMPATPrintQueue::GetInfo(
  371. THIS_ BOOL fExplicit,
  372. DWORD dwPropertyID
  373. )
  374. {
  375. HANDLE hPrinter = NULL;
  376. HRESULT hr = S_OK;
  377. LPBYTE lpbPrinterInfo = NULL;
  378. //
  379. // Open a handle to a printer.
  380. //
  381. hr = NWApiOpenPrinter(
  382. _szUncPrinterName,
  383. &hPrinter,
  384. PRINTER_ACCESS_USE
  385. );
  386. BAIL_ON_FAILURE(hr);
  387. //
  388. // Get printer's info.
  389. //
  390. hr = NWApiGetPrinter(
  391. hPrinter,
  392. WIN32_API_LEVEL_2,
  393. &lpbPrinterInfo
  394. );
  395. BAIL_ON_FAILURE(hr);
  396. //
  397. // Unmarshall.
  398. //
  399. hr = UnMarshall_GeneralInfo(
  400. (LPPRINTER_INFO_2) lpbPrinterInfo,
  401. fExplicit
  402. );
  403. BAIL_ON_FAILURE(hr);
  404. hr = UnMarshall_Operation(
  405. (LPPRINTER_INFO_2) lpbPrinterInfo,
  406. fExplicit
  407. );
  408. BAIL_ON_FAILURE(hr);
  409. error:
  410. if(lpbPrinterInfo){
  411. FreeADsMem(lpbPrinterInfo);
  412. }
  413. if (hPrinter) {
  414. NWApiClosePrinter(hPrinter);
  415. }
  416. RRETURN_EXP_IF_ERR(hr);
  417. }
  418. //----------------------------------------------------------------------------
  419. //
  420. // Function: CNWCOMPATPrintQueue::UnMarshall_GeneralInfo
  421. //
  422. // Synopsis:
  423. //
  424. //----------------------------------------------------------------------------
  425. HRESULT
  426. CNWCOMPATPrintQueue::UnMarshall_GeneralInfo(
  427. LPPRINTER_INFO_2 lpPrinterInfo2,
  428. BOOL fExplicit
  429. )
  430. {
  431. HRESULT hr = S_OK;
  432. DATE daStartTime = 0;
  433. DATE daUntilTime = 0;
  434. VARIANT vPortNames;
  435. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  436. TEXT("PrinterPath"),
  437. _szUncPrinterName,
  438. fExplicit
  439. );
  440. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  441. TEXT("Model"),
  442. lpPrinterInfo2->pDriverName,
  443. fExplicit
  444. );
  445. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  446. TEXT("Datatype"),
  447. lpPrinterInfo2->pDatatype,
  448. fExplicit
  449. );
  450. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  451. TEXT("PrintProcessor"),
  452. lpPrinterInfo2->pPrintProcessor,
  453. fExplicit
  454. );
  455. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  456. TEXT("Description"),
  457. lpPrinterInfo2->pComment,
  458. fExplicit
  459. );
  460. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  461. TEXT("Location"),
  462. lpPrinterInfo2->pLocation,
  463. fExplicit
  464. );
  465. hr = SetDATEPropertyInCache(_pPropertyCache,
  466. TEXT("StartTime"),
  467. lpPrinterInfo2->StartTime,
  468. fExplicit
  469. );
  470. hr = SetDATEPropertyInCache(_pPropertyCache,
  471. TEXT("UntilTime"),
  472. lpPrinterInfo2->UntilTime,
  473. fExplicit
  474. );
  475. hr = SetDWORDPropertyInCache(_pPropertyCache,
  476. TEXT("DefaultJobPriority"),
  477. lpPrinterInfo2->DefaultPriority,
  478. fExplicit
  479. );
  480. hr = SetDWORDPropertyInCache(_pPropertyCache,
  481. TEXT("Priority"),
  482. lpPrinterInfo2->Priority,
  483. fExplicit
  484. );
  485. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  486. TEXT("BannerPage"),
  487. lpPrinterInfo2->pSepFile,
  488. fExplicit
  489. );
  490. hr = SetDelimitedStringPropertyInCache(_pPropertyCache,
  491. TEXT("PrintDevices"),
  492. lpPrinterInfo2->pPortName,
  493. fExplicit
  494. );
  495. hr = S_OK;
  496. RRETURN(hr);
  497. }
  498. //----------------------------------------------------------------------------
  499. //
  500. // Function: CNWCOMPATPrintQueue::UnMarshall_Operation
  501. //
  502. // Synopsis:
  503. //
  504. //----------------------------------------------------------------------------
  505. HRESULT
  506. CNWCOMPATPrintQueue::UnMarshall_Operation(
  507. LPPRINTER_INFO_2 lpPrinterInfo2,
  508. BOOL fExplicit
  509. )
  510. {
  511. HRESULT hr = S_OK;
  512. hr = SetDWORDPropertyInCache(_pPropertyCache,
  513. TEXT("Status"),
  514. lpPrinterInfo2->Status,
  515. fExplicit
  516. );
  517. RRETURN(hr);
  518. }
  519. //----------------------------------------------------------------------------
  520. //
  521. // Function: CNWCOMPATPrintQueue:: MarshallAndSet
  522. //
  523. // Synopsis:
  524. //
  525. //----------------------------------------------------------------------------
  526. HRESULT
  527. CNWCOMPATPrintQueue::MarshallAndSet(
  528. HANDLE hPrinter,
  529. LPPRINTER_INFO_2 lpPrinterInfo2
  530. )
  531. {
  532. LPTSTR pszDriverName = NULL;
  533. LPTSTR pszDatatype = NULL;
  534. LPTSTR pszDescription = NULL;
  535. LPTSTR pszLocation = NULL;
  536. LPTSTR pszBannerPage = NULL;
  537. LPTSTR pszHostComputer = NULL;
  538. LPTSTR pszPrintProcessor = NULL;
  539. HRESULT hr = S_OK;
  540. LPTSTR pszPorts = NULL;
  541. VARIANT vTemp;
  542. DWORD dwTimeValue;
  543. DWORD dwPriority;
  544. //
  545. // Set Model.
  546. //
  547. hr = GetLPTSTRPropertyFromCache(
  548. _pPropertyCache,
  549. TEXT("Model"),
  550. &pszDriverName
  551. );
  552. if (SUCCEEDED(hr)){
  553. lpPrinterInfo2->pDriverName = (LPTSTR)pszDriverName;
  554. }
  555. //
  556. // Set Datatype.
  557. //
  558. hr = GetLPTSTRPropertyFromCache(
  559. _pPropertyCache,
  560. TEXT("Datatype"),
  561. &pszDatatype
  562. );
  563. if (SUCCEEDED(hr)){
  564. lpPrinterInfo2->pDatatype = (LPTSTR)pszDatatype;
  565. }
  566. //
  567. // Set Description.
  568. //
  569. hr = GetLPTSTRPropertyFromCache(
  570. _pPropertyCache,
  571. TEXT("Description"),
  572. &pszDescription
  573. );
  574. if (SUCCEEDED(hr)){
  575. lpPrinterInfo2->pComment = (LPTSTR)pszDescription;
  576. }
  577. //
  578. // Set Location.
  579. //
  580. hr = GetLPTSTRPropertyFromCache(
  581. _pPropertyCache,
  582. TEXT("Location"),
  583. &pszLocation
  584. );
  585. if (SUCCEEDED(hr)){
  586. lpPrinterInfo2->pLocation = (LPTSTR)pszLocation;
  587. }
  588. //
  589. // Set Priority.
  590. //
  591. hr = GetDWORDPropertyFromCache(
  592. _pPropertyCache,
  593. TEXT("Priority"),
  594. &dwPriority
  595. );
  596. if(SUCCEEDED(hr)){
  597. lpPrinterInfo2->Priority = dwPriority;
  598. }
  599. //
  600. // Set StartTime.
  601. //
  602. hr = GetDATEPropertyFromCache(
  603. _pPropertyCache,
  604. TEXT("StartTime"),
  605. &dwTimeValue
  606. );
  607. if(SUCCEEDED(hr)){
  608. lpPrinterInfo2->StartTime = dwTimeValue;
  609. }
  610. //
  611. // Set UntilTime.
  612. //
  613. hr = GetDATEPropertyFromCache(
  614. _pPropertyCache,
  615. TEXT("UntilTime"),
  616. &dwTimeValue
  617. );
  618. if(SUCCEEDED(hr)){
  619. lpPrinterInfo2->UntilTime = dwTimeValue;
  620. }
  621. //
  622. // Set DefaultJobPriority.
  623. //
  624. hr = GetDWORDPropertyFromCache(
  625. _pPropertyCache,
  626. TEXT("DefaultJobPriority"),
  627. &dwPriority
  628. );
  629. if(SUCCEEDED(hr)){
  630. lpPrinterInfo2->DefaultPriority = dwPriority;
  631. }
  632. //
  633. // Set BannerPage.
  634. //
  635. hr = GetLPTSTRPropertyFromCache(
  636. _pPropertyCache,
  637. TEXT("BannerPage"),
  638. &pszBannerPage
  639. );
  640. if(SUCCEEDED(hr)){
  641. lpPrinterInfo2->pSepFile = pszBannerPage;
  642. }
  643. //
  644. // Set PrintProcessor.
  645. //
  646. hr = GetLPTSTRPropertyFromCache(
  647. _pPropertyCache,
  648. TEXT("PrintProcessor"),
  649. &pszPrintProcessor
  650. );
  651. if(SUCCEEDED(hr)){
  652. lpPrinterInfo2->pPrintProcessor = pszPrintProcessor;
  653. }
  654. //
  655. // Set Ports.
  656. //
  657. hr = GetDelimitedStringPropertyFromCache(
  658. _pPropertyCache,
  659. TEXT("PrintDevices"),
  660. &pszPorts
  661. );
  662. if(SUCCEEDED(hr)){
  663. lpPrinterInfo2->pPortName = pszPorts;
  664. }
  665. //
  666. // Commit changes.
  667. //
  668. hr = NWApiSetPrinter(
  669. hPrinter,
  670. WIN32_API_LEVEL_2,
  671. (LPBYTE) lpPrinterInfo2,
  672. 0
  673. );
  674. BAIL_ON_FAILURE(hr);
  675. error:
  676. if(pszDriverName)
  677. FreeADsStr(pszDriverName);
  678. if(pszDatatype)
  679. FreeADsStr(pszDatatype);
  680. if(pszDescription)
  681. FreeADsStr(pszDescription);
  682. if(pszLocation)
  683. FreeADsStr(pszLocation);
  684. if(pszBannerPage)
  685. FreeADsStr(pszBannerPage);
  686. if(pszPrintProcessor)
  687. FreeADsStr(pszPrintProcessor);
  688. if(pszPorts)
  689. FreeADsStr(pszPorts);
  690. RRETURN(hr);
  691. }