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.

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