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.

613 lines
15 KiB

  1. /*++
  2. Copyright (c) 1995-1996 Microsoft Corporation
  3. Module Name:
  4. jobhlp.cxx
  5. Abstract:
  6. Helper functions for Job Object
  7. Author:
  8. Ram Viswanathan (ramv) 11-18-95
  9. Revision History:
  10. --*/
  11. #include "winnt.hxx"
  12. #pragma hdrstop
  13. #define INITGUID
  14. //
  15. // mapping WinNT Status Codes to ADs Status Codes and vice versa
  16. //
  17. typedef struct _JobStatusList {
  18. DWORD dwWinNTJobStatus;
  19. DWORD dwADsJobStatus;
  20. } JOB_STATUS_LIST, *PJOB_STATUS_LIST;
  21. JOB_STATUS_LIST JobStatusList[] =
  22. {
  23. {JOB_STATUS_PAUSED, ADS_JOB_PAUSED },
  24. {JOB_STATUS_ERROR, ADS_JOB_ERROR},
  25. {JOB_STATUS_DELETING, ADS_JOB_DELETING},
  26. {JOB_STATUS_SPOOLING, ADS_JOB_SPOOLING},
  27. {JOB_STATUS_PRINTING, ADS_JOB_PRINTING},
  28. {JOB_STATUS_OFFLINE, ADS_JOB_OFFLINE},
  29. {JOB_STATUS_PAPEROUT, ADS_JOB_PAPEROUT},
  30. {JOB_STATUS_PRINTED, ADS_JOB_PRINTED}
  31. };
  32. BOOL JobStatusWinNTToADs( DWORD dwWinNTStatus,
  33. DWORD *pdwADsStatus)
  34. {
  35. BOOL found = FALSE;
  36. int i;
  37. for (i=0;i<8;i++){
  38. if(dwWinNTStatus == JobStatusList[i].dwWinNTJobStatus){
  39. *pdwADsStatus = JobStatusList[i].dwADsJobStatus;
  40. found = TRUE;
  41. break;
  42. }
  43. }
  44. return (found);
  45. }
  46. BOOL JobStatusADsToWinNT( DWORD dwADsStatus,
  47. DWORD *pdwWinNTStatus)
  48. {
  49. BOOL found = FALSE;
  50. int i;
  51. for (i=0;i<8;i++){
  52. if(dwADsStatus == JobStatusList[i].dwADsJobStatus){
  53. *pdwWinNTStatus = JobStatusList[i].dwWinNTJobStatus;
  54. found = TRUE;
  55. break;
  56. }
  57. }
  58. return (found);
  59. }
  60. //+---------------------------------------------------------------------------
  61. //
  62. // Function: MarshallAndSet
  63. //
  64. // Synopsis: Marshalls information from a Print Job object to a
  65. // JOB_INFO_2 structure and sets it
  66. //
  67. // Arguments: [lpJobInfo2] -- Pointer to a JOB_INFO_2 struct.
  68. //
  69. // Returns: HRESULT
  70. //
  71. // Modifies:
  72. //
  73. // History: 11/09/95 RamV Created
  74. //
  75. //-----------------------------------------------------------------
  76. HRESULT
  77. CWinNTPrintJob::MarshallAndSet(
  78. LPJOB_INFO_2 lpJobInfo2,
  79. BSTR bstrPrinterName,
  80. LONG lJobId
  81. )
  82. {
  83. HRESULT hr =S_OK;
  84. LPTSTR pszUserName = NULL;
  85. LPTSTR pszDescription = NULL;
  86. LPTSTR pszNotify = NULL;
  87. LPTSTR pszDocument = NULL;
  88. DWORD dwPriority;
  89. DWORD dwPosition;
  90. DWORD dwStartTime;
  91. DWORD dwUntilTime;
  92. DWORD dwTotalPages;
  93. DWORD dwSize;
  94. DWORD dwPagesPrinted;
  95. SYSTEMTIME stTimeSubmitted;
  96. hr = GetLPTSTRPropertyFromCache(
  97. _pPropertyCache,
  98. TEXT("User"),
  99. &pszUserName
  100. );
  101. if(SUCCEEDED(hr)){
  102. lpJobInfo2->pUserName = pszUserName;
  103. }
  104. hr = GetLPTSTRPropertyFromCache(
  105. _pPropertyCache,
  106. TEXT("Description"),
  107. &pszDocument
  108. );
  109. if(SUCCEEDED(hr)){
  110. lpJobInfo2->pDocument = pszDocument;
  111. }
  112. hr = GetLPTSTRPropertyFromCache(
  113. _pPropertyCache,
  114. TEXT("Notify"),
  115. &pszNotify
  116. );
  117. if(SUCCEEDED(hr)){
  118. lpJobInfo2->pNotifyName = pszNotify;
  119. }
  120. hr = GetDWORDPropertyFromCache(
  121. _pPropertyCache,
  122. TEXT("Priority"),
  123. &dwPriority
  124. );
  125. if(SUCCEEDED(hr)){
  126. lpJobInfo2->Priority = dwPriority;
  127. }
  128. hr = GetDWORDPropertyFromCache(
  129. _pPropertyCache,
  130. TEXT("Position"),
  131. &dwPosition
  132. );
  133. if(SUCCEEDED(hr)){
  134. lpJobInfo2->Position = dwPosition;
  135. }
  136. hr = GetDWORDPropertyFromCache(
  137. _pPropertyCache,
  138. TEXT("TotalPages"),
  139. &dwTotalPages
  140. );
  141. if(SUCCEEDED(hr)){
  142. lpJobInfo2->TotalPages = dwTotalPages;
  143. }
  144. hr = GetDWORDPropertyFromCache(
  145. _pPropertyCache,
  146. TEXT("Size"),
  147. &dwSize
  148. );
  149. if(SUCCEEDED(hr)){
  150. lpJobInfo2->Size = dwSize;
  151. }
  152. hr = GetDWORDPropertyFromCache(
  153. _pPropertyCache,
  154. TEXT("PagesPrinted"),
  155. &dwPagesPrinted
  156. );
  157. if(SUCCEEDED(hr)){
  158. lpJobInfo2->PagesPrinted = dwPagesPrinted;
  159. }
  160. hr = GetDATEPropertyFromCache(
  161. _pPropertyCache,
  162. TEXT("StartTime"),
  163. &dwStartTime
  164. );
  165. if(SUCCEEDED(hr)){
  166. lpJobInfo2->StartTime = dwStartTime;
  167. }
  168. hr = GetDATEPropertyFromCache(
  169. _pPropertyCache,
  170. TEXT("UntilTime"),
  171. &dwUntilTime
  172. );
  173. if(SUCCEEDED(hr)){
  174. lpJobInfo2->UntilTime = dwUntilTime;
  175. }
  176. hr = GetSYSTEMTIMEPropertyFromCache(
  177. _pPropertyCache,
  178. TEXT("TimeSubmitted"),
  179. &stTimeSubmitted
  180. );
  181. if(SUCCEEDED(hr)){
  182. lpJobInfo2->Submitted = stTimeSubmitted;
  183. }
  184. //
  185. // set the relevant information
  186. //
  187. hr = Set(lpJobInfo2,
  188. bstrPrinterName,
  189. lJobId);
  190. if(pszUserName){
  191. FreeADsStr(pszUserName);
  192. }
  193. if(pszDescription){
  194. FreeADsStr(pszDescription);
  195. }
  196. if(pszNotify){
  197. FreeADsStr(pszNotify);
  198. }
  199. if(pszDocument){
  200. FreeADsStr(pszDocument);
  201. }
  202. RRETURN(hr);
  203. }
  204. //+----------------------------------------------------------------
  205. //
  206. // Function: UnmarshallLevel2
  207. //
  208. // Synopsis: Unmarshalls information from a JOB_INFO_2 to a
  209. // WinNT Print JOB object.
  210. //
  211. // Arguments: [lpJobInfo2] -- Pointer to a JOB_INFO_2 struct
  212. //
  213. // Returns: HRESULT
  214. //
  215. // Modifies: GeneralInfo and Operation Functional sets
  216. //
  217. // History: 11/08/95 RamV Created
  218. //
  219. //----------------------------------------------------------------------------
  220. HRESULT
  221. CWinNTPrintJob::UnMarshallLevel2(
  222. LPJOB_INFO_2 lpJobInfo2,
  223. BOOL fExplicit
  224. )
  225. {
  226. HRESULT hr S_OK;
  227. hr = SetDWORDPropertyInCache(_pPropertyCache,
  228. TEXT("TimeElapsed"),
  229. lpJobInfo2->Time,
  230. fExplicit
  231. );
  232. hr = SetDWORDPropertyInCache(_pPropertyCache,
  233. TEXT("PagesPrinted"),
  234. lpJobInfo2->PagesPrinted,
  235. fExplicit
  236. );
  237. hr = SetDWORDPropertyInCache(_pPropertyCache,
  238. TEXT("Position"),
  239. lpJobInfo2->Position,
  240. fExplicit
  241. );
  242. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  243. TEXT("HostPrintQueue"),
  244. _pszPrinterPath,
  245. fExplicit
  246. );
  247. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  248. TEXT("User"),
  249. lpJobInfo2->pUserName,
  250. fExplicit
  251. );
  252. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  253. TEXT("Description"),
  254. lpJobInfo2->pDocument,
  255. fExplicit
  256. );
  257. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  258. TEXT("Notify"),
  259. lpJobInfo2->pNotifyName,
  260. fExplicit
  261. );
  262. hr = SetDWORDPropertyInCache(_pPropertyCache,
  263. TEXT("Priority"),
  264. lpJobInfo2->Priority,
  265. fExplicit
  266. );
  267. hr = SetDATEPropertyInCache(_pPropertyCache,
  268. TEXT("StartTime"),
  269. lpJobInfo2->StartTime,
  270. fExplicit
  271. );
  272. hr = SetDATEPropertyInCache(_pPropertyCache,
  273. TEXT("UntilTime"),
  274. lpJobInfo2-> UntilTime,
  275. fExplicit
  276. );
  277. hr = SetSYSTEMTIMEPropertyInCache(_pPropertyCache,
  278. TEXT("TimeSubmitted"),
  279. lpJobInfo2->Submitted,
  280. fExplicit
  281. );
  282. hr = SetDWORDPropertyInCache(_pPropertyCache,
  283. TEXT("TotalPages"),
  284. lpJobInfo2->TotalPages,
  285. fExplicit
  286. );
  287. hr = SetDWORDPropertyInCache(_pPropertyCache,
  288. TEXT("Size"),
  289. lpJobInfo2->Size,
  290. fExplicit
  291. );
  292. hr = SetLPTSTRPropertyInCache(
  293. _pPropertyCache,
  294. TEXT("Name"),
  295. _Name,
  296. fExplicit
  297. );
  298. RRETURN(S_OK);
  299. }
  300. HRESULT
  301. CWinNTPrintJob::UnMarshallLevel1(
  302. LPJOB_INFO_1 lpJobInfo1,
  303. BOOL fExplicit
  304. )
  305. {
  306. HRESULT hr;
  307. hr = SetDWORDPropertyInCache(_pPropertyCache,
  308. TEXT("PagesPrinted"),
  309. lpJobInfo1->PagesPrinted,
  310. fExplicit
  311. );
  312. hr = SetDWORDPropertyInCache(_pPropertyCache,
  313. TEXT("Position"),
  314. lpJobInfo1->Position,
  315. fExplicit
  316. );
  317. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  318. TEXT("User"),
  319. lpJobInfo1->pUserName,
  320. fExplicit
  321. );
  322. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  323. TEXT("Description"),
  324. lpJobInfo1->pDocument,
  325. fExplicit
  326. );
  327. hr = SetDWORDPropertyInCache(_pPropertyCache,
  328. TEXT("Priority"),
  329. lpJobInfo1->Priority,
  330. fExplicit
  331. );
  332. hr = SetDWORDPropertyInCache(_pPropertyCache,
  333. TEXT("TotalPages"),
  334. lpJobInfo1->TotalPages,
  335. fExplicit
  336. );
  337. hr = SetSYSTEMTIMEPropertyInCache(_pPropertyCache,
  338. TEXT("TimeSubmitted"),
  339. lpJobInfo1->Submitted,
  340. fExplicit
  341. );
  342. RRETURN(S_OK);
  343. }
  344. //+---------------------------------------------------------------------------
  345. //
  346. // Function: Set
  347. //
  348. // Synopsis: Helper function called by CADsPrintJob:SetInfo
  349. //
  350. // Arguments:
  351. //
  352. // Returns: HRESULT.
  353. //
  354. // Modifies:
  355. //
  356. // History: 11-08-95 RamV Created
  357. //
  358. //----------------------------------------------------------------------------
  359. HRESULT
  360. Set(
  361. LPJOB_INFO_2 lpJobInfo2,
  362. BSTR bstrPrinterName,
  363. LONG lJobId
  364. )
  365. {
  366. BOOL fStatus = FALSE;
  367. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ALL_ACCESS};
  368. HANDLE hPrinter = NULL;
  369. HRESULT hr = S_OK;
  370. DWORD LastError = 0;
  371. ADsAssert(bstrPrinterName);
  372. fStatus = OpenPrinter((LPTSTR)bstrPrinterName,
  373. &hPrinter,
  374. &PrinterDefaults
  375. );
  376. if (!fStatus) {
  377. goto error;
  378. }
  379. fStatus = SetJob(hPrinter,
  380. lJobId,
  381. 2,
  382. (LPBYTE)lpJobInfo2,
  383. 0
  384. );
  385. if (!fStatus) {
  386. goto error;
  387. }
  388. RRETURN(S_OK);
  389. error:
  390. hr =HRESULT_FROM_WIN32(GetLastError());
  391. if(hPrinter)
  392. fStatus = ClosePrinter(hPrinter);
  393. RRETURN(hr);
  394. }
  395. HRESULT
  396. GetJobInfo( DWORD dwLevel,
  397. LPBYTE *ppJobInfo,
  398. LPWSTR pszPrinterName,
  399. LONG lJobId)
  400. {
  401. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_USE |
  402. READ_CONTROL};
  403. BOOL fStatus = FALSE;
  404. DWORD dwPassed = 0, dwNeeded = 0;
  405. DWORD LastError = 0;
  406. HANDLE hPrinter = NULL;
  407. LPBYTE pMem = NULL;
  408. HRESULT hr = S_OK;
  409. ADsAssert(dwLevel ==1 || dwLevel == 2);
  410. fStatus = OpenPrinter(pszPrinterName,
  411. &hPrinter,
  412. &PrinterDefaults
  413. );
  414. if (!fStatus) {
  415. LastError = GetLastError();
  416. switch (LastError) {
  417. case ERROR_ACCESS_DENIED:
  418. {
  419. PRINTER_DEFAULTS PrinterDefaults = {0, 0, PRINTER_ACCESS_USE};
  420. fStatus = OpenPrinter(pszPrinterName,
  421. &hPrinter,
  422. &PrinterDefaults
  423. );
  424. if (fStatus) {
  425. break;
  426. }
  427. }
  428. default:
  429. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  430. }
  431. }
  432. pMem = (LPBYTE)AllocADsMem(dwPassed);
  433. if (!pMem) {
  434. hr = E_OUTOFMEMORY;
  435. goto cleanup;
  436. }
  437. fStatus = GetJob(hPrinter,
  438. lJobId,
  439. dwLevel,
  440. (LPBYTE)pMem,
  441. dwPassed,
  442. &dwNeeded
  443. );
  444. if (!fStatus) {
  445. LastError = GetLastError();
  446. switch (LastError) {
  447. case ERROR_INSUFFICIENT_BUFFER:
  448. if(pMem){
  449. FreeADsMem(pMem);
  450. }
  451. dwPassed = dwNeeded;
  452. pMem = (LPBYTE)AllocADsMem(dwPassed);
  453. if (!pMem) {
  454. hr = E_OUTOFMEMORY;
  455. goto cleanup;
  456. }
  457. fStatus = GetJob(hPrinter,
  458. lJobId,
  459. dwLevel,
  460. (LPBYTE)pMem,
  461. dwPassed,
  462. &dwNeeded
  463. );
  464. if (!fStatus) {
  465. hr = HRESULT_FROM_WIN32(GetLastError());
  466. goto cleanup;
  467. }
  468. break;
  469. default:
  470. hr = HRESULT_FROM_WIN32(GetLastError());
  471. goto cleanup;
  472. }
  473. }
  474. *ppJobInfo = pMem;
  475. cleanup:
  476. if(hPrinter)
  477. ClosePrinter(hPrinter);
  478. RRETURN(hr);
  479. }