Leaked source code of windows server 2003
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.

647 lines
13 KiB

  1. //=================================================================
  2. //
  3. // WinSpoolApi.h
  4. //
  5. // Copyright (c) 1999-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #ifndef _WinSpoolAPI_H_
  9. #define _WinSpoolAPI_H_
  10. #include <winspool.h>
  11. // Define a mutex class to single thread Winspool APIs under 9x
  12. #ifdef WIN9XONLY
  13. #include <winbase.h>
  14. #define PRINTER_NAMED_MUTEX _T("WMI_Win_PrintSpool_Named_Mutex")
  15. class CPrintMutex
  16. {
  17. private:
  18. HANDLE m_hHandle ;
  19. public:
  20. CPrintMutex()
  21. {
  22. if( m_hHandle = ::OpenMutex(MUTEX_ALL_ACCESS, TRUE, PRINTER_NAMED_MUTEX) )
  23. {
  24. WaitForSingleObject(m_hHandle, INFINITE);
  25. }
  26. }
  27. ~CPrintMutex()
  28. {
  29. if( m_hHandle )
  30. {
  31. ::ReleaseMutex( m_hHandle ) ;
  32. }
  33. }
  34. };
  35. #define LOCK_WINSPOOL_9X CPrintMutex t_oWinSpool_abcdefg
  36. #else
  37. #define LOCK_WINSPOOL_9X
  38. #endif
  39. /******************************************************************************
  40. * #includes to Register this class with the CResourceManager.
  41. *****************************************************************************/
  42. extern const GUID g_guidWinSpoolApi;
  43. extern const TCHAR g_tstrWinSpool[];
  44. /******************************************************************************
  45. * Function pointer typedefs. Add new functions here as required.
  46. *****************************************************************************/
  47. typedef BOOL (WINAPI *PFN_WinSpool_AddPrinterConnection)
  48. (
  49. IN LPWSTR pName
  50. );
  51. typedef BOOL (WINAPI *PFN_WinSpool_DeletePrinterConnection)
  52. (
  53. IN LPWSTR pName
  54. );
  55. typedef BOOL (WINAPI *PFN_WinSpool_ClosePrinter)
  56. (
  57. IN HANDLE hPrinter
  58. );
  59. #ifdef UNICODE
  60. typedef int ( WINAPI *PFN_WinSpool_DeviceCapabilities )
  61. (
  62. IN LPCWSTR,
  63. IN LPCWSTR,
  64. IN WORD,
  65. OUT LPWSTR,
  66. IN CONST DEVMODEW *
  67. );
  68. #else
  69. typedef int ( WINAPI *PFN_WinSpool_DeviceCapabilities)
  70. (
  71. IN LPCSTR,
  72. IN LPCSTR,
  73. IN WORD,
  74. OUT LPCSTR,
  75. IN CONST DEVMODEA *
  76. );
  77. #endif
  78. #ifdef UNICODE
  79. typedef LONG ( WINAPI *PFN_WinSpool_DocumentProperties )
  80. (
  81. IN HWND hWnd,
  82. IN HANDLE hPrinter,
  83. IN LPWSTR pDeviceName,
  84. OUT PDEVMODEW pDevModeOutput,
  85. IN PDEVMODEW pDevModeInput,
  86. IN DWORD fMode
  87. );
  88. #else
  89. typedef LONG ( WINAPI *PFN_WinSpool_DocumentProperties )
  90. (
  91. IN HWND hWnd,
  92. IN HANDLE hPrinter,
  93. IN LPSTR pDeviceName,
  94. OUT PDEVMODEA pDevModeOutput,
  95. IN PDEVMODEA pDevModeInput,
  96. IN DWORD fMode
  97. );
  98. #endif
  99. typedef BOOL ( WINAPI *PFN_WinSpool_EnumJobs )
  100. (
  101. IN HANDLE hPrinter,
  102. IN DWORD FirstJob,
  103. IN DWORD NoJobs,
  104. IN DWORD Level,
  105. OUT LPBYTE pJob,
  106. IN DWORD cbBuf,
  107. OUT LPDWORD pcbNeeded,
  108. OUT LPDWORD pcReturned
  109. );
  110. #ifdef UNICODE
  111. typedef BOOL ( WINAPI *PFN_WinSpool_EnumPrinterDrivers )
  112. (
  113. IN LPWSTR pName,
  114. IN LPWSTR pEnvironment,
  115. IN DWORD Level,
  116. OUT LPBYTE pDriverInfo,
  117. IN DWORD cbBuf,
  118. OUT LPDWORD pcbNeeded,
  119. OUT LPDWORD pcReturned
  120. );
  121. #else
  122. typedef BOOL ( WINAPI *PFN_WinSpool_EnumPrinterDrivers )
  123. (
  124. IN LPSTR pName,
  125. IN LPSTR pEnvironment,
  126. IN DWORD Level,
  127. OUT LPBYTE pDriverInfo,
  128. IN DWORD cbBuf,
  129. OUT LPDWORD pcbNeeded,
  130. OUT LPDWORD pcReturned
  131. );
  132. #endif
  133. #ifdef UNICODE
  134. typedef BOOL ( WINAPI *PFN_WinSpool_EnumPrinters )
  135. (
  136. IN DWORD Flags,
  137. IN LPWSTR Name,
  138. IN DWORD Level,
  139. OUT LPBYTE pPrinterEnum,
  140. IN DWORD cbBuf,
  141. OUT LPDWORD pcbNeeded,
  142. OUT LPDWORD pcReturned
  143. );
  144. #else
  145. typedef BOOL ( WINAPI *PFN_WinSpool_EnumPrinters )
  146. (
  147. IN DWORD Flags,
  148. IN LPSTR Name,
  149. IN DWORD Level,
  150. OUT LPBYTE pPrinterEnum,
  151. IN DWORD cbBuf,
  152. OUT LPDWORD pcbNeeded,
  153. OUT LPDWORD pcReturned
  154. );
  155. #endif
  156. #ifdef UNICODE
  157. typedef BOOL ( WINAPI *PFN_WinSpool_EnumPorts )
  158. (
  159. IN LPWSTR Name,
  160. IN DWORD Level,
  161. OUT LPBYTE pPortEnum,
  162. IN DWORD cbBuf,
  163. OUT LPDWORD pcbNeeded,
  164. OUT LPDWORD pcReturned
  165. );
  166. #else
  167. typedef BOOL ( WINAPI *PFN_WinSpool_EnumPorts )
  168. (
  169. IN LPSTR Name,
  170. IN DWORD Level,
  171. OUT LPBYTE pPortEnum,
  172. IN DWORD cbBuf,
  173. OUT LPDWORD pcbNeeded,
  174. OUT LPDWORD pcReturned
  175. );
  176. #endif
  177. typedef BOOL ( WINAPI *PFN_WinSpool_GetJob )
  178. (
  179. IN HANDLE hPrinter,
  180. IN DWORD JobId,
  181. IN DWORD Level,
  182. OUT LPBYTE pJob,
  183. IN DWORD cbBuf,
  184. OUT LPDWORD pcbNeeded
  185. );
  186. typedef BOOL ( WINAPI *PFN_WinSpool_SetJob )
  187. (
  188. IN HANDLE hPrinter,
  189. IN DWORD JobId,
  190. IN DWORD Level,
  191. IN LPBYTE pJob,
  192. IN DWORD cbBuf
  193. );
  194. typedef BOOL ( WINAPI *PFN_WinSpool_GetPrinter )
  195. (
  196. IN HANDLE hPrinter,
  197. IN DWORD Level,
  198. OUT LPBYTE pPrinter,
  199. IN DWORD cbBuf,
  200. OUT LPDWORD pcbNeeded
  201. );
  202. typedef BOOL ( WINAPI *PFN_WinSpool_GetPrinterDriver )
  203. (
  204. IN HANDLE hPrinter,
  205. IN LPTSTR pEnvironment,
  206. IN DWORD Level,
  207. OUT LPBYTE pPrinter,
  208. IN DWORD cbBuf,
  209. OUT LPDWORD pcbNeeded
  210. );
  211. typedef BOOL ( WINAPI *PFN_WinSpool_SetPrinter )
  212. (
  213. IN HANDLE hPrinter,
  214. IN DWORD Level,
  215. IN LPBYTE pPrinter,
  216. IN DWORD cbBuf
  217. );
  218. typedef BOOL ( WINAPI *PFN_WinSpool_DeletePrinter )
  219. (
  220. IN HANDLE hPrinter
  221. );
  222. #ifdef UNICODE
  223. typedef BOOL ( WINAPI *PFN_WinSpool_OpenPrinter )
  224. (
  225. IN LPWSTR pPrinterName,
  226. OUT LPHANDLE phPrinter,
  227. IN LPPRINTER_DEFAULTSW pDefault
  228. );
  229. #else
  230. typedef BOOL ( WINAPI *PFN_WinSpool_OpenPrinter )
  231. (
  232. IN LPSTR pPrinterName,
  233. OUT LPHANDLE phPrinter,
  234. IN LPPRINTER_DEFAULTSA pDefault
  235. );
  236. #endif
  237. typedef BOOL ( WINAPI *PFN_WinSpool_GetDefaultPrinter )
  238. (
  239. IN LPTSTR pszBuffer,
  240. IN LPDWORD pcchBuffer
  241. ) ;
  242. typedef BOOL ( WINAPI *PFN_WinSpool_SetDefaultPrinter )
  243. (
  244. IN LPTSTR pszBuffer
  245. ) ;
  246. #ifdef UNICODE
  247. #if NTONLY == 5
  248. typedef BOOL ( WINAPI *PFN_WinSpool_XcvData )
  249. (
  250. IN HANDLE hXcv,
  251. IN PCWSTR pszDataName,
  252. IN PBYTE pInputData,
  253. IN DWORD cbInputData,
  254. OUT PBYTE pOutputData,
  255. IN DWORD cbOutputData,
  256. OUT PDWORD pcbOutputNeeded,
  257. OUT PDWORD pdwStatus
  258. );
  259. #endif
  260. #endif
  261. /******************************************************************************
  262. * Wrapper class for WinSpool load/unload, for registration with CResourceManager.
  263. *****************************************************************************/
  264. class CWinSpoolApi : public CDllWrapperBase
  265. {
  266. private:
  267. // Member variables (function pointers) pointing to WinSpool functions.
  268. // Add new functions here as required.
  269. PFN_WinSpool_ClosePrinter m_pfnClosePrinter;
  270. PFN_WinSpool_DeviceCapabilities m_pfnDeviceCapabilities;
  271. PFN_WinSpool_DocumentProperties m_pfnDocumentProperties;
  272. PFN_WinSpool_EnumJobs m_pfnEnumJobs;
  273. PFN_WinSpool_EnumPrinterDrivers m_pfnEnumPrinterDrivers;
  274. PFN_WinSpool_EnumPrinters m_pfnEnumPrinters;
  275. PFN_WinSpool_EnumPorts m_pfnEnumPorts;
  276. PFN_WinSpool_GetJob m_pfnGetJob;
  277. PFN_WinSpool_GetPrinter m_pfnGetPrinter;
  278. PFN_WinSpool_GetPrinterDriver m_pfnGetPrinterDriver;
  279. PFN_WinSpool_SetPrinter m_pfnSetPrinter;
  280. PFN_WinSpool_OpenPrinter m_pfnOpenPrinter;
  281. PFN_WinSpool_GetDefaultPrinter m_pfnGetDefaultPrinter;
  282. PFN_WinSpool_SetJob m_pfnSetJob;
  283. PFN_WinSpool_DeletePrinter m_pfnDeletePrinter;
  284. PFN_WinSpool_SetDefaultPrinter m_pfnSetDefaultPrinter;
  285. #ifdef UNICODE
  286. #if NTONLY == 5
  287. PFN_WinSpool_XcvData m_pfnXcvData;
  288. #endif
  289. PFN_WinSpool_AddPrinterConnection m_pfnAddPrinterConnection;
  290. PFN_WinSpool_DeletePrinterConnection m_pfnDeletePrinterConnection;
  291. #endif
  292. HANDLE m_hPrintMutex;
  293. public:
  294. // Constructor and destructor:
  295. CWinSpoolApi(LPCTSTR a_tstrWrappedDllName);
  296. ~CWinSpoolApi();
  297. // Initialization function to check function pointers.
  298. virtual bool Init();
  299. // Member functions wrapping WinSpool functions.
  300. // Add new functions here as required:
  301. BOOL ClosePrinter (
  302. IN HANDLE hPrinter
  303. );
  304. #ifdef UNICODE
  305. int DeviceCapabilities (
  306. IN LPCWSTR pDevice ,
  307. IN LPCWSTR pPort,
  308. IN WORD fwCapability ,
  309. OUT LPWSTR pOutput ,
  310. IN CONST DEVMODEW *pDevMode
  311. );
  312. #else
  313. int DeviceCapabilities (
  314. IN LPCSTR pDevice ,
  315. IN LPCSTR pPort,
  316. IN WORD fwCapability ,
  317. OUT LPCSTR pOutput,
  318. IN CONST DEVMODEA *pDevMode
  319. );
  320. #endif
  321. #ifdef UNICODE
  322. LONG DocumentProperties (
  323. IN HWND hWnd,
  324. IN HANDLE hPrinter,
  325. IN LPWSTR pDeviceName,
  326. OUT PDEVMODEW pDevModeOutput,
  327. IN PDEVMODEW pDevModeInput,
  328. IN DWORD fMode
  329. );
  330. #else
  331. LONG DocumentProperties (
  332. IN HWND hWnd,
  333. IN HANDLE hPrinter,
  334. IN LPSTR pDeviceName,
  335. OUT PDEVMODEA pDevModeOutput,
  336. IN PDEVMODEA pDevModeInput,
  337. IN DWORD fMode
  338. );
  339. #endif
  340. BOOL EnumJobs (
  341. IN HANDLE hPrinter,
  342. IN DWORD FirstJob,
  343. IN DWORD NoJobs,
  344. IN DWORD Level,
  345. OUT LPBYTE pJob,
  346. IN DWORD cbBuf,
  347. OUT LPDWORD pcbNeeded,
  348. OUT LPDWORD pcReturned
  349. );
  350. #ifdef UNICODE
  351. BOOL EnumPrinterDrivers (
  352. IN LPWSTR pName,
  353. IN LPWSTR pEnvironment,
  354. IN DWORD Level,
  355. OUT LPBYTE pDriverInfo,
  356. IN DWORD cbBuf,
  357. OUT LPDWORD pcbNeeded,
  358. OUT LPDWORD pcReturned
  359. );
  360. #else
  361. BOOL EnumPrinterDrivers (
  362. IN LPSTR pName,
  363. IN LPSTR pEnvironment,
  364. IN DWORD Level,
  365. OUT LPBYTE pDriverInfo,
  366. IN DWORD cbBuf,
  367. OUT LPDWORD pcbNeeded,
  368. OUT LPDWORD pcReturned
  369. );
  370. #endif
  371. #ifdef UNICODE
  372. BOOL EnumPrinters (
  373. IN DWORD Flags,
  374. IN LPWSTR Name,
  375. IN DWORD Level,
  376. OUT LPBYTE pPrinterEnum,
  377. IN DWORD cbBuf,
  378. OUT LPDWORD pcbNeeded,
  379. OUT LPDWORD pcReturned
  380. );
  381. #else
  382. BOOL EnumPrinters (
  383. IN DWORD Flags,
  384. IN LPSTR Name,
  385. IN DWORD Level,
  386. OUT LPBYTE pPrinterEnum,
  387. IN DWORD cbBuf,
  388. OUT LPDWORD pcbNeeded,
  389. OUT LPDWORD pcReturned
  390. );
  391. #endif
  392. #ifdef UNICODE
  393. BOOL EnumPorts (
  394. IN LPWSTR pName,
  395. IN DWORD Level,
  396. OUT LPBYTE pPortInfo,
  397. IN DWORD cbBuf,
  398. OUT LPDWORD pcbNeeded,
  399. OUT LPDWORD pcReturned
  400. );
  401. #else
  402. BOOL EnumPorts (
  403. IN LPSTR pName,
  404. IN DWORD Level,
  405. OUT LPBYTE pPortInfo,
  406. IN DWORD cbBuf,
  407. OUT LPDWORD pcbNeeded,
  408. OUT LPDWORD pcReturned
  409. );
  410. #endif
  411. BOOL GetJob (
  412. IN HANDLE hPrinter,
  413. IN DWORD JobId,
  414. IN DWORD Level,
  415. OUT LPBYTE pJob,
  416. IN DWORD cbBuf,
  417. OUT LPDWORD pcbNeeded
  418. );
  419. BOOL SetJob (
  420. IN HANDLE hPrinter,
  421. IN DWORD JobId,
  422. IN DWORD Level,
  423. IN LPBYTE pJob,
  424. IN DWORD cbBuf
  425. );
  426. BOOL GetPrinter (
  427. IN HANDLE hPrinter,
  428. IN DWORD Level,
  429. OUT LPBYTE pPrinter,
  430. IN DWORD cbBuf,
  431. OUT LPDWORD pcbNeeded
  432. );
  433. BOOL GetPrinterDriver (
  434. IN HANDLE hPrinter,
  435. IN LPTSTR pEnvironment,
  436. IN DWORD Level,
  437. OUT LPBYTE pPrinter,
  438. IN DWORD cbBuf,
  439. OUT LPDWORD pcbNeeded
  440. );
  441. BOOL SetPrinter (
  442. IN HANDLE hPrinter,
  443. IN DWORD Level,
  444. IN LPBYTE pPrinter,
  445. IN DWORD cbBuf
  446. );
  447. BOOL DeletePrinter (
  448. IN HANDLE hPrinter
  449. );
  450. BOOL XcvDataW(
  451. IN HANDLE hXcv,
  452. IN PCWSTR pszDataName,
  453. IN PBYTE pInputData,
  454. IN DWORD cbInputData,
  455. OUT PBYTE pOutputData,
  456. IN DWORD cbOutputData,
  457. OUT PDWORD pcbOutputNeeded,
  458. OUT PDWORD pdwStatus
  459. );
  460. #ifdef UNICODE
  461. BOOL OpenPrinter (
  462. IN LPWSTR pPrinterName,
  463. OUT LPHANDLE phPrinter,
  464. IN LPPRINTER_DEFAULTSW pDefault
  465. );
  466. #else
  467. BOOL OpenPrinter (
  468. IN LPSTR pPrinterName,
  469. OUT LPHANDLE phPrinter,
  470. IN LPPRINTER_DEFAULTSA pDefault
  471. );
  472. #endif
  473. BOOL GetDefaultPrinter (
  474. IN LPTSTR pszBuffer,
  475. IN LPDWORD pcchBuffer
  476. );
  477. BOOL SetDefaultPrinter (
  478. IN LPTSTR pszBuffer
  479. );
  480. #if NTONLY >= 5
  481. BOOL AddPrinterConnection(
  482. IN LPTSTR pName
  483. );
  484. BOOL DeletePrinterConnection(
  485. IN LPTSTR pName
  486. );
  487. #endif
  488. };
  489. class SmartClosePrinter
  490. {
  491. private:
  492. HANDLE m_h;
  493. public:
  494. SmartClosePrinter () : m_h ( INVALID_HANDLE_VALUE ) {}
  495. SmartClosePrinter ( HANDLE h ) : m_h ( h ) {}
  496. ~SmartClosePrinter()
  497. {
  498. if (m_h!=INVALID_HANDLE_VALUE)
  499. {
  500. CWinSpoolApi *pWinSpoolApi = ( CWinSpoolApi * )CResourceManager::sm_TheResourceManager.GetResource ( g_guidWinSpoolApi, NULL ) ;
  501. if ( pWinSpoolApi )
  502. {
  503. pWinSpoolApi->ClosePrinter(m_h);
  504. CResourceManager::sm_TheResourceManager.ReleaseResource ( g_guidWinSpoolApi , pWinSpoolApi ) ;
  505. }
  506. }
  507. }
  508. HANDLE operator =(HANDLE h)
  509. {
  510. if (m_h!=INVALID_HANDLE_VALUE)
  511. {
  512. CWinSpoolApi *pWinSpoolApi = ( CWinSpoolApi * )CResourceManager::sm_TheResourceManager.GetResource ( g_guidWinSpoolApi, NULL ) ;
  513. if ( pWinSpoolApi )
  514. {
  515. pWinSpoolApi->ClosePrinter(m_h);
  516. CResourceManager::sm_TheResourceManager.ReleaseResource ( g_guidWinSpoolApi , pWinSpoolApi ) ;
  517. }
  518. }
  519. m_h=h;
  520. return h;
  521. }
  522. operator HANDLE() const {return m_h;}
  523. HANDLE* operator &()
  524. {
  525. if (m_h!=INVALID_HANDLE_VALUE)
  526. {
  527. CWinSpoolApi *pWinSpoolApi = ( CWinSpoolApi * )CResourceManager::sm_TheResourceManager.GetResource ( g_guidWinSpoolApi, NULL ) ;
  528. if ( pWinSpoolApi )
  529. {
  530. pWinSpoolApi->ClosePrinter(m_h);
  531. CResourceManager::sm_TheResourceManager.ReleaseResource ( g_guidWinSpoolApi , pWinSpoolApi ) ;
  532. }
  533. }
  534. m_h = INVALID_HANDLE_VALUE;
  535. return &m_h;
  536. }
  537. };
  538. #endif