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.

641 lines
10 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1998 - 1998
  3. All rights reserved.
  4. Module Name:
  5. walkreg.cxx
  6. Abstract:
  7. Printer data walking class definition.
  8. Author:
  9. Adina Trufinescu (AdinaTru) 15-Oct-1998
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #include "wlkprn.hxx"
  15. #define gszWack TEXT("\\")
  16. /*++
  17. Title:
  18. WalkPrinterData::WalkPrinterData
  19. Routine Description:
  20. Default constructor
  21. Arguments:
  22. None
  23. Return Value:
  24. None
  25. --*/
  26. WalkPrinterData::
  27. WalkPrinterData(
  28. VOID
  29. )
  30. {
  31. InitializeClassVariables();
  32. }
  33. /*++
  34. Title:
  35. WalkPrinterData
  36. Routine Description:
  37. class constructor
  38. Arguments:
  39. pszPrinterName -- printer name
  40. resource type -- printer / server
  41. access type -- converted to printer access flags
  42. Return Value:
  43. Nothing
  44. --*/
  45. WalkPrinterData::
  46. WalkPrinterData(
  47. IN TString& pszPrinterName,
  48. IN WalkPrinterData::EResourceType eResourceType,
  49. IN WalkPrinterData::EAccessType eAccessType
  50. )
  51. {
  52. InitializeClassVariables();
  53. m_strPrnName.bUpdate( pszPrinterName );
  54. PRINTER_DEFAULTS Access = {0, 0, PrinterAccessFlags( eResourceType, eAccessType ) };
  55. //
  56. // Null string indicate the local server.
  57. //
  58. if(m_strPrnName.bValid())
  59. {
  60. if (OpenPrinter(const_cast<LPTSTR>(static_cast<LPCTSTR>(m_strPrnName)), &m_hPrinter, &Access))
  61. {
  62. m_eAccessType = eAccessType;
  63. }
  64. }
  65. }
  66. /*++
  67. Title:
  68. WalkPrinterData
  69. Routine Description:
  70. constructor
  71. Arguments:
  72. hPrinter -- handle to an open printer
  73. Return Value:
  74. Nothing
  75. --*/
  76. WalkPrinterData::
  77. WalkPrinterData(
  78. IN HANDLE hPrinter
  79. )
  80. {
  81. InitializeClassVariables();
  82. m_hPrinter = hPrinter;
  83. m_bAcceptedHandle = TRUE;
  84. }
  85. /*++
  86. Title:
  87. Bind
  88. Routine Description:
  89. late initialization
  90. Arguments:
  91. hPrinter -- handle to an opened printer
  92. Return Value:
  93. Nothing
  94. --*/
  95. VOID
  96. WalkPrinterData::
  97. BindToPrinter(
  98. IN HANDLE hPrinter
  99. )
  100. {
  101. m_hPrinter = hPrinter;
  102. m_bAcceptedHandle = TRUE;
  103. }
  104. /*++
  105. Title:
  106. ~WalkPrinterData
  107. Routine Description:
  108. class destructor
  109. Arguments:
  110. None
  111. Return Value:
  112. Nothing
  113. --*/
  114. WalkPrinterData::
  115. ~WalkPrinterData(
  116. VOID
  117. )
  118. {
  119. if( !m_bAcceptedHandle && m_hPrinter )
  120. {
  121. ClosePrinter(m_hPrinter);
  122. }
  123. }
  124. /*++
  125. Title:
  126. bValid
  127. Routine Description:
  128. Checks member initialisation
  129. Arguments:
  130. None
  131. Return Value:
  132. TRUE if valid m_hPrinter
  133. --*/
  134. BOOL
  135. WalkPrinterData::
  136. bValid(
  137. VOID
  138. ) const
  139. {
  140. return m_hPrinter != INVALID_HANDLE_VALUE ;
  141. }
  142. /*++
  143. Title:
  144. InitializeClassVariables
  145. Routine Description:
  146. initialise class members
  147. Arguments:
  148. Return Value:
  149. VOID
  150. Last Error:
  151. --*/
  152. VOID
  153. WalkPrinterData::
  154. InitializeClassVariables(
  155. VOID
  156. )
  157. {
  158. m_strPrnName.bUpdate(NULL);
  159. m_hPrinter = NULL;
  160. m_eAccessType = kAccessUnknown;
  161. m_bAcceptedHandle = FALSE;
  162. }
  163. /*++
  164. Title:
  165. PrinterAccessFlags
  166. Routine Description:
  167. Convert class access flags to printer ACCESS_MASK
  168. Arguments:
  169. resource type -- printer / server
  170. access type -- converted to printer access flags
  171. Return Value:
  172. an access mask built upon eResourceType and eAccessType
  173. --*/
  174. ACCESS_MASK
  175. WalkPrinterData::
  176. PrinterAccessFlags(
  177. IN EResourceType eResourceType,
  178. IN EAccessType eAccessType
  179. ) const
  180. {
  181. static const DWORD adwAccessPrinter[] =
  182. {
  183. PRINTER_ALL_ACCESS,
  184. PRINTER_READ | PRINTER_WRITE,
  185. 0,
  186. };
  187. static const DWORD adwAccessServer[] =
  188. {
  189. SERVER_ALL_ACCESS,
  190. SERVER_READ | SERVER_WRITE,
  191. 0,
  192. };
  193. DWORD dwAccess = 0;
  194. UINT uAccessType = eAccessType > 3 ? 2 : eAccessType;
  195. switch( eResourceType )
  196. {
  197. case kResourceServer:
  198. dwAccess = adwAccessServer[uAccessType];
  199. break;
  200. case kResourcePrinter:
  201. dwAccess = adwAccessPrinter[uAccessType];
  202. break;
  203. case kResourceUnknown:
  204. default:
  205. break;
  206. }
  207. return dwAccess;
  208. }
  209. /*++
  210. Title: NextStrT
  211. Routine Description:
  212. Returns next sz string in a multi zero string
  213. Arguments:
  214. lpszStr - ptr to multi zero string
  215. Return Value:
  216. pointer to zero string
  217. --*/
  218. LPTSTR
  219. WalkPrinterData::
  220. NextStrT(
  221. IN LPCTSTR lpszStr
  222. )
  223. {
  224. return const_cast<LPTSTR>(lpszStr) + (_tcslen(lpszStr) + 1);
  225. }
  226. /*++
  227. Title:
  228. bHasSubKeys
  229. Routine Description:
  230. Check if a Printer data key has subkeys
  231. Arguments:
  232. strKey - key string
  233. mszSubKeys - ptr to multi zero string
  234. must be checked at return time ; fnct can return TRUE and
  235. mszSubKeys == NULL -> has subkeys but couldn't allocate mszSubKeys
  236. Return Value:
  237. TRUE if is has subkeys
  238. FALSE if has no sub keys
  239. --*/
  240. BOOL
  241. WalkPrinterData::
  242. bHasSubKeys(
  243. IN TString& strKey,
  244. OUT LPTSTR* mszSubKeys //ORPHAN
  245. )
  246. {
  247. DWORD cbSize;
  248. TStatus Status(DBG_WARN, ERROR_MORE_DATA);
  249. TStatusB bStatus;
  250. bStatus DBGCHK = bValid();
  251. if(bStatus)
  252. {
  253. //
  254. // Determine the size necessary for enumerating all the
  255. // sub-keys for this key.
  256. cbSize = 0;
  257. Status DBGCHK = EnumPrinterKey(m_hPrinter, static_cast<LPCTSTR>(strKey), NULL, 0, &cbSize);
  258. //
  259. // If OK, then proceed to the enumeration.
  260. //
  261. if (cbSize && (Status == ERROR_MORE_DATA))
  262. {
  263. //
  264. // Allocate the space for retrieving the keys.
  265. //
  266. *mszSubKeys = reinterpret_cast<LPTSTR>( AllocMem(cbSize) );
  267. bStatus DBGCHK = (*mszSubKeys != NULL);
  268. if(bStatus)
  269. {
  270. //
  271. // Enumerate the sub-keys for this level in (lpszKey).
  272. //
  273. Status DBGCHK = EnumPrinterKey(m_hPrinter, static_cast<LPCTSTR>(strKey), *mszSubKeys, cbSize, &cbSize);
  274. bStatus DBGCHK = (Status == ERROR_SUCCESS);
  275. if(bStatus)
  276. {
  277. goto End;
  278. }
  279. //
  280. // Free mszSubKeys if EnumPrinterKey fails
  281. //
  282. FreeMem(*mszSubKeys);
  283. }
  284. }
  285. else
  286. {
  287. bStatus DBGCHK = FALSE;
  288. }
  289. }
  290. End:
  291. return bStatus;
  292. }
  293. /*++
  294. Title:
  295. bInternalWalk
  296. Routine Description:
  297. Walking function through printer data keys; calls Walk IN/POST/PRE for every key
  298. Arguments:
  299. strKey - key string
  300. lpcItems - number of keys walked through
  301. Return Value:
  302. TRUE if is has subkeys
  303. FALSE if has no sub keys
  304. --*/
  305. BOOL
  306. WalkPrinterData::
  307. bInternalWalk (
  308. IN TString& strKey,
  309. OUT LPDWORD lpcItems OPTIONAL
  310. )
  311. {
  312. LPTSTR lpszSubKey;
  313. LPTSTR mszSubKeys;
  314. TString strFullSubKey;
  315. DWORD cItems = 0;
  316. TStatusB bStatus;
  317. *lpcItems = 0;
  318. if(bHasSubKeys(strKey, &mszSubKeys))
  319. {
  320. bStatus DBGCHK = (mszSubKeys != NULL);
  321. if(bStatus)
  322. {
  323. //
  324. // Walk PRE before walking subkeys
  325. //
  326. bStatus DBGCHK = bWalkPre(strKey , &cItems);
  327. //
  328. // Browse subkeys multi zero string and call bInternalWalk for every subkey
  329. // this loop will won't execute if bWalkPre failed
  330. //
  331. for (lpszSubKey = mszSubKeys; *lpszSubKey && bStatus; )
  332. {
  333. //
  334. // Builds strSubKey and strFullKey
  335. //
  336. if(strKey.uLen() > 0)
  337. {
  338. bStatus DBGCHK = strFullSubKey.bUpdate(strKey) &&
  339. strFullSubKey.bCat(gszWack) &&
  340. strFullSubKey.bCat(lpszSubKey);
  341. }
  342. else
  343. {
  344. bStatus DBGCHK = strFullSubKey.bUpdate(lpszSubKey);
  345. }
  346. if(bStatus)
  347. {
  348. bStatus DBGCHK = bInternalWalk(strFullSubKey, &cItems);
  349. bStatus ? *lpcItems += cItems : *lpcItems;
  350. lpszSubKey = NextStrT(lpszSubKey);
  351. }
  352. }
  353. FreeMem(mszSubKeys);
  354. }
  355. if(bStatus)
  356. {
  357. //
  358. // Walk POST after walking all subkeys
  359. //
  360. bStatus DBGCHK = bWalkPost(strKey , &cItems);
  361. bStatus ? *lpcItems += cItems : *lpcItems;
  362. }
  363. }
  364. else
  365. {
  366. //
  367. // Current key is not <directory> ,so walk IN!!!
  368. //
  369. bStatus DBGCHK = bWalkIn(strKey , &cItems);
  370. bStatus ? *lpcItems = cItems : *lpcItems;
  371. }
  372. return bStatus;
  373. }
  374. /*++
  375. Title:
  376. bWalkPre
  377. Routine Description:
  378. PRE walking
  379. Arguments:
  380. strKey - key string
  381. Return Value:
  382. TRUE
  383. --*/
  384. BOOL
  385. WalkPrinterData::
  386. bWalkPre(
  387. IN TString& strKey,
  388. OUT LPDWORD lpcItems
  389. )
  390. {
  391. return TRUE;
  392. }
  393. /*++
  394. Title:
  395. bWalkIn
  396. Routine Description:
  397. IN walking
  398. Arguments:
  399. strKey - key string
  400. Return Value:
  401. TRUE
  402. Last Error:
  403. --*/
  404. BOOL
  405. WalkPrinterData::
  406. bWalkIn (
  407. IN TString& str,
  408. OUT LPDWORD lpcItems
  409. )
  410. {
  411. return TRUE;
  412. }
  413. /*++
  414. Title:
  415. bWalkPost
  416. Routine Description:
  417. POST walking
  418. Arguments:
  419. strKey - key string
  420. Return Value:
  421. TRUE
  422. --*/
  423. BOOL
  424. WalkPrinterData::
  425. bWalkPost (
  426. IN TString& strKey,
  427. OUT LPDWORD lpcItem
  428. )
  429. {
  430. return TRUE;
  431. }