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.

854 lines
18 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: cjob.cxx
  7. //
  8. // Contents: CNWCOMPATPrintJob
  9. //
  10. //
  11. // History: 1-May-96 t-ptam (Patrick Tam) Created.
  12. // 1-Jul-96 ramv (Ram Viswanathan) Modified to use propcache
  13. //----------------------------------------------------------------------------
  14. #include "NWCOMPAT.hxx"
  15. #pragma hdrstop
  16. //
  17. // Macro-ized implementation.
  18. //
  19. DEFINE_IDispatch_Implementation(CNWCOMPATPrintJob);
  20. DEFINE_IADs_TempImplementation(CNWCOMPATPrintJob);
  21. DEFINE_IADs_PutGetImplementation(CNWCOMPATPrintJob, PrintJobClass, gdwJobTableSize);
  22. DEFINE_IADsPropertyList_Implementation(CNWCOMPATPrintJob, PrintJobClass, gdwJobTableSize);
  23. //----------------------------------------------------------------------------
  24. //
  25. // Function: CNWCOMPATPrintJob::CNWCOMPATPrintJob
  26. //
  27. // Synopsis:
  28. //
  29. //----------------------------------------------------------------------------
  30. CNWCOMPATPrintJob::CNWCOMPATPrintJob():
  31. _pDispMgr(NULL),
  32. _lJobId(0),
  33. _lStatus(0),
  34. _pPropertyCache(NULL),
  35. _pszPrinterPath(NULL),
  36. _hConn(NULL)
  37. {
  38. ENLIST_TRACKING(CNWCOMPATPrintJob);
  39. }
  40. //----------------------------------------------------------------------------
  41. //
  42. // Function: CNWCOMPATPrintJob::~CNWCOMPATPrintJob
  43. //
  44. // Synopsis:
  45. //
  46. //----------------------------------------------------------------------------
  47. CNWCOMPATPrintJob::~CNWCOMPATPrintJob()
  48. {
  49. delete _pDispMgr;
  50. delete _pPropertyCache;
  51. if(_pszPrinterPath){
  52. FreeADsStr(_pszPrinterPath);
  53. }
  54. if (_hConn)
  55. NWApiReleaseBinderyHandle(_hConn);
  56. }
  57. //----------------------------------------------------------------------------
  58. //
  59. // Function: CNWCOMPATPrintJob::CreatePrintJob
  60. //
  61. // Synopsis:
  62. //
  63. //----------------------------------------------------------------------------
  64. HRESULT
  65. CNWCOMPATPrintJob::CreatePrintJob(
  66. LPTSTR pszPrinterPath,
  67. LONG lJobId,
  68. CCredentials &Credentials,
  69. DWORD dwObjectState,
  70. REFIID riid,
  71. LPVOID *ppvoid
  72. )
  73. {
  74. CNWCOMPATPrintJob *pPrintJob = NULL;
  75. HRESULT hr = S_OK;
  76. TCHAR szJobName[MAX_LONG_LENGTH];
  77. POBJECTINFO pObjectInfo = NULL;
  78. TCHAR szUncPrinterName[MAX_PATH];
  79. //
  80. // Allocate memory for a print job object.
  81. //
  82. hr = AllocatePrintJobObject(
  83. &pPrintJob
  84. );
  85. BAIL_ON_FAILURE(hr);
  86. //
  87. // Convert the JobId that we have into a string that we move
  88. // into the Name field
  89. //
  90. _ltow(
  91. lJobId,
  92. szJobName,
  93. 10
  94. );
  95. //
  96. // Initialize its core object.
  97. //
  98. hr = pPrintJob->InitializeCoreObject(
  99. pszPrinterPath,
  100. szJobName,
  101. PRINTJOB_CLASS_NAME,
  102. PRINTJOB_SCHEMA_NAME,
  103. CLSID_NWCOMPATPrintJob,
  104. dwObjectState
  105. );
  106. BAIL_ON_FAILURE(hr);
  107. //
  108. // Job ID.
  109. //
  110. pPrintJob->_lJobId = lJobId;
  111. pPrintJob->_pszPrinterPath = AllocADsStr(pszPrinterPath);
  112. if(! pPrintJob->_pszPrinterPath) {
  113. hr = E_OUTOFMEMORY;
  114. goto error;
  115. }
  116. // Credentials
  117. pPrintJob->_Credentials = Credentials;
  118. //
  119. // Componentize ADs name.
  120. //
  121. hr = BuildObjectInfo(
  122. pPrintJob->_Parent,
  123. pPrintJob->_Name,
  124. &pObjectInfo
  125. );
  126. BAIL_ON_FAILURE(hr);
  127. //
  128. // Get a handle to the bindery this object resides on.
  129. //
  130. hr = NWApiGetBinderyHandle(
  131. &pPrintJob->_hConn,
  132. pObjectInfo->ComponentArray[0],
  133. pPrintJob->_Credentials
  134. );
  135. BAIL_ON_FAILURE(hr);
  136. //
  137. // Get interface pointer.
  138. //
  139. hr = pPrintJob->QueryInterface(
  140. riid,
  141. (void **)ppvoid
  142. );
  143. BAIL_ON_FAILURE(hr);
  144. pPrintJob->Release();
  145. //
  146. // Return.
  147. //
  148. if (pObjectInfo) {
  149. FreeObjectInfo(pObjectInfo);
  150. }
  151. RRETURN(hr);
  152. error:
  153. if (pObjectInfo) {
  154. FreeObjectInfo(pObjectInfo);
  155. }
  156. delete pPrintJob;
  157. NW_RRETURN_EXP_IF_ERR(hr);
  158. }
  159. //----------------------------------------------------------------------------
  160. //
  161. // Function: CNWCOMPATPrintJob::AllocatePrintJobObject
  162. //
  163. // Synopsis:
  164. //
  165. //----------------------------------------------------------------------------
  166. HRESULT
  167. CNWCOMPATPrintJob::AllocatePrintJobObject(
  168. CNWCOMPATPrintJob ** ppPrintJob
  169. )
  170. {
  171. CDispatchMgr FAR * pDispMgr = NULL;
  172. CPropertyCache FAR * pPropertyCache = NULL;
  173. CNWCOMPATPrintJob FAR * pPrintJob = NULL;
  174. CNWCOMPATFSPrintJobGeneralInfo FAR * pGenInfo = NULL;
  175. CNWCOMPATFSPrintJobOperation FAR * pOperation = NULL;
  176. HRESULT hr = S_OK;
  177. //
  178. // Allocate memory for a PrintJob object.
  179. //
  180. pPrintJob = new CNWCOMPATPrintJob();
  181. if (pPrintJob == NULL) {
  182. hr = E_OUTOFMEMORY;
  183. }
  184. BAIL_ON_FAILURE(hr);
  185. //
  186. // Create a Dispatch Manager object.
  187. //
  188. pDispMgr = new CDispatchMgr;
  189. if (pDispMgr == NULL) {
  190. hr = E_OUTOFMEMORY;
  191. }
  192. BAIL_ON_FAILURE(hr);
  193. //
  194. // Load type info.
  195. //
  196. hr = LoadTypeInfoEntry(
  197. pDispMgr,
  198. LIBID_ADs,
  199. IID_IADsPrintJob,
  200. (IADsPrintJob *) pPrintJob,
  201. DISPID_REGULAR
  202. );
  203. BAIL_ON_FAILURE(hr);
  204. hr = LoadTypeInfoEntry(
  205. pDispMgr,
  206. LIBID_ADs,
  207. IID_IADsPrintJobOperations,
  208. (IADsPrintJobOperations *)pPrintJob,
  209. DISPID_REGULAR
  210. );
  211. BAIL_ON_FAILURE(hr);
  212. hr = LoadTypeInfoEntry(
  213. pDispMgr,
  214. LIBID_ADs,
  215. IID_IADsPropertyList,
  216. (IADsPropertyList *)pPrintJob,
  217. DISPID_VALUE
  218. );
  219. BAIL_ON_FAILURE(hr);
  220. //
  221. // Return.
  222. //
  223. hr = CPropertyCache::createpropertycache(
  224. PrintJobClass,
  225. gdwJobTableSize,
  226. (CCoreADsObject *)pPrintJob,
  227. &pPropertyCache
  228. );
  229. BAIL_ON_FAILURE(hr);
  230. pPrintJob->_pPropertyCache = pPropertyCache;
  231. pPrintJob->_pDispMgr = pDispMgr;
  232. *ppPrintJob = pPrintJob;
  233. RRETURN(hr);
  234. error:
  235. delete pPropertyCache;
  236. delete pDispMgr;
  237. delete pPrintJob;
  238. RRETURN(hr);
  239. }
  240. //----------------------------------------------------------------------------
  241. //
  242. // Function: CNWCOMPATPrintJob::QueryInterface
  243. //
  244. // Synopsis:
  245. //
  246. //----------------------------------------------------------------------------
  247. STDMETHODIMP
  248. CNWCOMPATPrintJob::QueryInterface(
  249. REFIID riid,
  250. LPVOID FAR* ppvObj
  251. )
  252. {
  253. if(!ppvObj){
  254. RRETURN(E_POINTER);
  255. }
  256. if (IsEqualIID(riid, IID_IUnknown))
  257. {
  258. *ppvObj = (IADsPrintJob FAR *)this;
  259. }
  260. else if (IsEqualIID(riid, IID_IDispatch))
  261. {
  262. *ppvObj = (IADsPrintJob *)this;
  263. }
  264. else if (IsEqualIID(riid, IID_ISupportErrorInfo))
  265. {
  266. *ppvObj = (ISupportErrorInfo FAR *) this;
  267. }
  268. else if (IsEqualIID(riid, IID_IADs))
  269. {
  270. *ppvObj = (IADsPrintJob FAR *) this;
  271. }
  272. else if (IsEqualIID(riid, IID_IADsPrintJob))
  273. {
  274. *ppvObj = (IADsPrintJob FAR *) this;
  275. }
  276. else if (IsEqualIID(riid, IID_IADsPrintJobOperations))
  277. {
  278. *ppvObj = (IADsPrintJobOperations FAR *) this;
  279. }
  280. else if (IsEqualIID(riid, IID_IADsPropertyList))
  281. {
  282. *ppvObj = (IADsPropertyList FAR *) this;
  283. }
  284. else
  285. {
  286. *ppvObj = NULL;
  287. RRETURN(E_NOINTERFACE);
  288. }
  289. AddRef();
  290. return NOERROR;
  291. }
  292. //----------------------------------------------------------------------------
  293. //
  294. // Function: CNWCOMPATPrintJob::InterfaceSupportsErrorInfo
  295. //
  296. // Synopsis:
  297. //
  298. //----------------------------------------------------------------------------
  299. STDMETHODIMP
  300. CNWCOMPATPrintJob::InterfaceSupportsErrorInfo(
  301. THIS_ REFIID riid
  302. )
  303. {
  304. if (IsEqualIID(riid, IID_IADs) ||
  305. IsEqualIID(riid, IID_IADsPropertyList) ||
  306. IsEqualIID(riid, IID_IADsPrintJob) ||
  307. IsEqualIID(riid, IID_IADsPrintJobOperations)) {
  308. RRETURN(S_OK);
  309. } else {
  310. RRETURN(S_FALSE);
  311. }
  312. }
  313. //----------------------------------------------------------------------------
  314. //
  315. // Function: CNWCOMPATPrintJob::SetInfo
  316. //
  317. // Synopsis:
  318. //
  319. //----------------------------------------------------------------------------
  320. STDMETHODIMP
  321. CNWCOMPATPrintJob::SetInfo(THIS)
  322. {
  323. HANDLE hPrinter = NULL;
  324. HRESULT hr = S_OK;
  325. LPBYTE lpbBuffer = NULL;
  326. WCHAR szUncPrinterName[MAX_PATH];
  327. //
  328. // Make sure object is bound to a tangible resource.
  329. //
  330. if (GetObjectState() == ADS_OBJECT_UNBOUND) {
  331. hr = CreateObject();
  332. BAIL_ON_FAILURE(hr);
  333. SetObjectState(ADS_OBJECT_BOUND);
  334. }
  335. //
  336. // Make Unc printer name.
  337. //
  338. hr = NWApiUncFromADsPath(
  339. _pszPrinterPath,
  340. szUncPrinterName
  341. );
  342. BAIL_ON_FAILURE(hr);
  343. //
  344. // Open a handle to a printer.
  345. //
  346. hr = NWApiOpenPrinter(
  347. szUncPrinterName,
  348. &hPrinter,
  349. PRINTER_ALL_ACCESS
  350. );
  351. BAIL_ON_FAILURE(hr);
  352. //
  353. // Get job Info structure.
  354. //
  355. hr = NWApiGetJob(
  356. hPrinter,
  357. (DWORD) _lJobId,
  358. WIN32_API_LEVEL_2,
  359. &lpbBuffer
  360. );
  361. BAIL_ON_FAILURE(hr);
  362. //
  363. // Set info.
  364. //
  365. hr = MarshallAndSet(
  366. hPrinter,
  367. (LPJOB_INFO_2) lpbBuffer
  368. );
  369. error:
  370. //
  371. // Close Printer handle.
  372. //
  373. if (hPrinter) {
  374. NWApiClosePrinter(hPrinter);
  375. }
  376. NW_RRETURN_EXP_IF_ERR(hr);
  377. }
  378. //----------------------------------------------------------------------------
  379. //
  380. // Function: CNWCOMPATPrintJob::GetInfo
  381. //
  382. // Synopsis:
  383. //
  384. //----------------------------------------------------------------------------
  385. STDMETHODIMP
  386. CNWCOMPATPrintJob::GetInfo(THIS)
  387. {
  388. _pPropertyCache->flushpropcache();
  389. RRETURN(GetInfo(
  390. TRUE,
  391. JOB_API_LEVEL
  392. ));
  393. }
  394. //----------------------------------------------------------------------------
  395. //
  396. // Function: CNWCOMPATPrintJob::CreateObject
  397. //
  398. // Synopsis:
  399. //
  400. //----------------------------------------------------------------------------
  401. STDMETHODIMP
  402. CNWCOMPATPrintJob::CreateObject()
  403. {
  404. NW_RRETURN_EXP_IF_ERR(E_NOTIMPL);
  405. }
  406. //----------------------------------------------------------------------------
  407. //
  408. // Function: CNWCOMPATPrintJob::GetInfo
  409. //
  410. // Synopsis: Please note that only Level 2 is used, unlike the WinNT side.
  411. // Since Level 1 is simply a subset of Level 2 in this case, its just
  412. // a matter of style which one to use.
  413. //
  414. //----------------------------------------------------------------------------
  415. STDMETHODIMP
  416. CNWCOMPATPrintJob::GetInfo(
  417. THIS_ BOOL fExplicit,
  418. DWORD dwPropertyID
  419. )
  420. {
  421. //
  422. // Please note that the input DWORD is treated as dwApiLevel, unlike ohter
  423. // NWCompat objects that treat it as dwPropertyID. The reason is that Win32
  424. // APIs are used for retrieving information of the object.
  425. //
  426. HANDLE hPrinter = NULL;
  427. HRESULT hr = S_OK;
  428. LPBYTE lpbJobInfo = NULL;
  429. WCHAR szUncPrinterName[MAX_PATH];
  430. //
  431. // Make Unc printer name.
  432. //
  433. hr = NWApiUncFromADsPath(
  434. _pszPrinterPath,
  435. szUncPrinterName
  436. );
  437. BAIL_ON_FAILURE(hr);
  438. //
  439. // Open a handle to a printer.
  440. //
  441. hr = NWApiOpenPrinter(
  442. szUncPrinterName,
  443. &hPrinter,
  444. PRINTER_ALL_ACCESS
  445. );
  446. BAIL_ON_FAILURE(hr);
  447. //
  448. // Get job's info.
  449. //
  450. hr = NWApiGetJob(
  451. hPrinter,
  452. (DWORD) _lJobId,
  453. WIN32_API_LEVEL_2,
  454. &lpbJobInfo
  455. );
  456. BAIL_ON_FAILURE(hr);
  457. //
  458. // Unmarshall.
  459. //
  460. hr = UnMarshall_GeneralInfo(
  461. (LPJOB_INFO_2) lpbJobInfo,
  462. fExplicit
  463. );
  464. BAIL_ON_FAILURE(hr);
  465. hr = UnMarshall_Operation(
  466. (LPJOB_INFO_2) lpbJobInfo,
  467. fExplicit
  468. );
  469. BAIL_ON_FAILURE(hr);
  470. error:
  471. if (hPrinter) {
  472. NWApiClosePrinter(hPrinter);
  473. }
  474. if(lpbJobInfo){
  475. FreeADsMem(lpbJobInfo);
  476. }
  477. NW_RRETURN_EXP_IF_ERR(hr);
  478. }
  479. //----------------------------------------------------------------------------
  480. //
  481. // Function: CNWCOMPATPrintJob::UnMarshall_GeneralInfo
  482. //
  483. // Synopsis:
  484. //
  485. //----------------------------------------------------------------------------
  486. HRESULT
  487. CNWCOMPATPrintJob::UnMarshall_GeneralInfo(
  488. LPJOB_INFO_2 lpJobInfo2,
  489. BOOL fExplicit
  490. )
  491. {
  492. DATE daTemp = 0;
  493. HRESULT hr = S_OK;
  494. LPWSTR lpszTemp = NULL;
  495. WCHAR szTemp[MAX_PATH];
  496. hr = SetLPTSTRPropertyInCache(
  497. _pPropertyCache,
  498. TEXT("HostPrintQueue"),
  499. _pszPrinterPath,
  500. fExplicit
  501. );
  502. hr = SetLPTSTRPropertyInCache(
  503. _pPropertyCache,
  504. TEXT("User"),
  505. lpJobInfo2->pUserName,
  506. fExplicit
  507. );
  508. hr = SetLPTSTRPropertyInCache(
  509. _pPropertyCache,
  510. TEXT("Description"),
  511. lpJobInfo2->pDocument,
  512. fExplicit
  513. );
  514. hr = SetLPTSTRPropertyInCache(
  515. _pPropertyCache,
  516. TEXT("Notify"),
  517. lpJobInfo2->pNotifyName,
  518. fExplicit
  519. );
  520. hr = SetSYSTEMTIMEPropertyInCache(
  521. _pPropertyCache,
  522. TEXT("TimeSubmitted"),
  523. lpJobInfo2->Submitted,
  524. fExplicit
  525. );
  526. hr = SetDWORDPropertyInCache(
  527. _pPropertyCache,
  528. TEXT("Priority"),
  529. lpJobInfo2->Priority,
  530. fExplicit
  531. );
  532. hr = SetDWORDPropertyInCache(
  533. _pPropertyCache,
  534. TEXT("TotalPages"),
  535. lpJobInfo2-> TotalPages,
  536. fExplicit
  537. );
  538. hr = SetDATEPropertyInCache(
  539. _pPropertyCache,
  540. TEXT("StartTime"),
  541. lpJobInfo2->StartTime,
  542. fExplicit
  543. );
  544. hr = SetDATEPropertyInCache(
  545. _pPropertyCache,
  546. TEXT("UntilTime"),
  547. lpJobInfo2->UntilTime,
  548. fExplicit
  549. );
  550. hr = SetDWORDPropertyInCache(
  551. _pPropertyCache,
  552. TEXT("Size"),
  553. lpJobInfo2->Size,
  554. fExplicit
  555. );
  556. hr = S_OK;
  557. RRETURN(hr);
  558. }
  559. //----------------------------------------------------------------------------
  560. //
  561. // Function: CNWCOMPATPrintJob::UnMarshall_Operation
  562. //
  563. // Synopsis:
  564. //
  565. //----------------------------------------------------------------------------
  566. HRESULT
  567. CNWCOMPATPrintJob::UnMarshall_Operation(
  568. LPJOB_INFO_2 lpJobInfo2,
  569. BOOL fExplicit
  570. )
  571. {
  572. HRESULT hr = S_OK;
  573. hr = SetDWORDPropertyInCache(
  574. _pPropertyCache,
  575. TEXT("Position"),
  576. lpJobInfo2->Position,
  577. fExplicit
  578. );
  579. BAIL_ON_FAILURE(hr);
  580. hr = SetDWORDPropertyInCache(
  581. _pPropertyCache,
  582. TEXT("PagesPrinted"),
  583. lpJobInfo2->PagesPrinted,
  584. fExplicit
  585. );
  586. BAIL_ON_FAILURE(hr);
  587. hr = SetDATEPropertyInCache(
  588. _pPropertyCache,
  589. TEXT("TimeElapsed"),
  590. lpJobInfo2->Time,
  591. fExplicit
  592. );
  593. hr = SetDWORDPropertyInCache(
  594. _pPropertyCache,
  595. TEXT("Status"),
  596. lpJobInfo2->Status,
  597. fExplicit
  598. );
  599. BAIL_ON_FAILURE(hr);
  600. _lStatus = lpJobInfo2->Status;
  601. error:
  602. RRETURN(hr);
  603. }
  604. //----------------------------------------------------------------------------
  605. //
  606. // Function: CNWCOMPATPrintJob::MarshallAndSet
  607. //
  608. // Synopsis:
  609. //
  610. //----------------------------------------------------------------------------
  611. HRESULT
  612. CNWCOMPATPrintJob::MarshallAndSet(
  613. HANDLE hPrinter,
  614. LPJOB_INFO_2 lpJobInfo2
  615. )
  616. {
  617. LPTSTR pszDescription = NULL;
  618. LPTSTR pszNotify = NULL;
  619. DWORD dwValue;
  620. HRESULT hr = S_OK;
  621. //
  622. // Set Description.
  623. //
  624. hr = GetLPTSTRPropertyFromCache(
  625. _pPropertyCache,
  626. TEXT("Description"),
  627. &pszDescription
  628. );
  629. if(SUCCEEDED(hr)){
  630. lpJobInfo2->pDocument = pszDescription;
  631. }
  632. //
  633. // Set Notify.
  634. //
  635. hr = GetLPTSTRPropertyFromCache(
  636. _pPropertyCache,
  637. TEXT("Notify"),
  638. &pszNotify
  639. );
  640. if(SUCCEEDED(hr)){
  641. lpJobInfo2->pNotifyName = pszNotify;
  642. }
  643. //
  644. // Set Priority.
  645. //
  646. hr = GetDWORDPropertyFromCache(
  647. _pPropertyCache,
  648. TEXT("Priority"),
  649. &dwValue
  650. );
  651. if(SUCCEEDED(hr)){
  652. lpJobInfo2->Priority = dwValue;
  653. }
  654. //
  655. // Set StartTime.
  656. //
  657. hr = GetDATEPropertyFromCache(
  658. _pPropertyCache,
  659. TEXT("StartTime"),
  660. &dwValue
  661. );
  662. if(SUCCEEDED(hr)){
  663. lpJobInfo2->StartTime = dwValue;
  664. }
  665. //
  666. // Set UntilTime
  667. //
  668. hr = GetDATEPropertyFromCache(
  669. _pPropertyCache,
  670. TEXT("UntilTime"),
  671. &dwValue
  672. );
  673. if(SUCCEEDED(hr)){
  674. lpJobInfo2->UntilTime = dwValue;
  675. }
  676. /* Clarify the precedence between Notify & NotifyPath
  677. hr = UM_GET_BSTR_PROPERTY(_pGenInfo, NotifyPath, fExplicit);
  678. BAIL_ON_FAILURE(hr);
  679. */
  680. //
  681. // Set Position.
  682. //
  683. hr = GetDWORDPropertyFromCache(
  684. _pPropertyCache,
  685. TEXT("Position"),
  686. &dwValue
  687. );
  688. if(SUCCEEDED(hr)){
  689. lpJobInfo2->Position = dwValue;
  690. }
  691. //
  692. // Commit changes.
  693. //
  694. hr = NWApiSetJob(
  695. hPrinter,
  696. (DWORD) _lJobId,
  697. WIN32_API_LEVEL_2,
  698. (LPBYTE) lpJobInfo2,
  699. 0
  700. );
  701. BAIL_ON_FAILURE(hr);
  702. error:
  703. if(pszDescription)
  704. FreeADsStr(pszDescription);
  705. if(pszNotify)
  706. FreeADsStr(pszNotify);
  707. RRETURN(S_OK);
  708. }