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.

342 lines
6.3 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. prndata.c
  5. Abstract:
  6. Functions for accessing printer property data in the registry
  7. Environment:
  8. Fax driver, user and kernel mode
  9. Revision History:
  10. 01/09/96 -davidx-
  11. Created it.
  12. mm/dd/yy -author-
  13. description
  14. --*/
  15. #include "faxlib.h"
  16. LPTSTR
  17. GetPrinterDataStr(
  18. HANDLE hPrinter,
  19. LPTSTR pRegKey
  20. )
  21. /*++
  22. Routine Description:
  23. Get a string value from the PrinterData registry key
  24. Arguments:
  25. hPrinter - Identifies the printer object
  26. pRegKey - Specifies the name of registry value
  27. Return Value:
  28. pBuffer
  29. --*/
  30. {
  31. DWORD type, cb;
  32. PVOID pBuffer = NULL;
  33. //
  34. // We should really pass NULL for pData parameter here. But to workaround
  35. // a bug in the spooler API GetPrinterData, we must pass in a valid pointer here.
  36. //
  37. if (GetPrinterData(hPrinter, pRegKey, &type, (PBYTE) &type, 0, &cb) == ERROR_MORE_DATA &&
  38. (pBuffer = MemAlloc(cb)) &&
  39. GetPrinterData(hPrinter, pRegKey, &type, pBuffer, cb, &cb) == ERROR_SUCCESS &&
  40. (type == REG_SZ || type == REG_MULTI_SZ || type == REG_EXPAND_SZ))
  41. {
  42. return pBuffer;
  43. }
  44. Error(("Couldn't get printer data string %ws: %d\n", pRegKey, GetLastError()));
  45. MemFree(pBuffer);
  46. return NULL;
  47. }
  48. DWORD
  49. GetPrinterDataDWord(
  50. HANDLE hPrinter,
  51. PWSTR pRegKey,
  52. DWORD defaultValue
  53. )
  54. /*++
  55. Routine Description:
  56. Retrieve a DWORD value under PrinterData registry key
  57. Arguments:
  58. hPrinter - Specifies the printer in question
  59. pRegKey - Specifies the name of registry value
  60. defaultValue - Specifies the default value to be used if no data exists in registry
  61. Return Value:
  62. Current value for the requested registry key
  63. --*/
  64. {
  65. DWORD value, type, cb;
  66. if (GetPrinterData(hPrinter,
  67. pRegKey,
  68. &type,
  69. (PBYTE) &value,
  70. sizeof(value),
  71. &cb) == ERROR_SUCCESS)
  72. {
  73. return value;
  74. }
  75. return defaultValue;
  76. }
  77. PVOID
  78. MyGetPrinter(
  79. HANDLE hPrinter,
  80. DWORD level
  81. )
  82. /*++
  83. Routine Description:
  84. Wrapper function for GetPrinter spooler API
  85. Arguments:
  86. hPrinter - Identifies the printer in question
  87. level - Specifies the level of PRINTER_INFO_x structure requested
  88. Return Value:
  89. Pointer to a PRINTER_INFO_x structure, NULL if there is an error
  90. --*/
  91. {
  92. PBYTE pPrinterInfo = NULL;
  93. DWORD cbNeeded;
  94. #ifdef SPOOLERBUG
  95. if (level == 9) {
  96. cbNeeded = sizeof(DRVDEVMODE);
  97. pPrinterInfo = MemAlloc( cbNeeded );
  98. if (GetPrinter(hPrinter,9,pPrinterInfo,cbNeeded,&cbNeeded)) {
  99. return pPrinterInfo;
  100. }
  101. } else
  102. #endif
  103. if (!GetPrinter(hPrinter, level, NULL, 0, &cbNeeded) &&
  104. GetLastError() == ERROR_INSUFFICIENT_BUFFER &&
  105. (pPrinterInfo = MemAlloc(cbNeeded)) &&
  106. GetPrinter(hPrinter, level, pPrinterInfo, cbNeeded, &cbNeeded))
  107. {
  108. return pPrinterInfo;
  109. }
  110. Error(("GetPrinter failed: %d\n", GetLastError()));
  111. MemFree(pPrinterInfo);
  112. return NULL;
  113. }
  114. #ifndef KERNEL_MODE
  115. BOOL
  116. SetPrinterDataStr(
  117. HANDLE hPrinter,
  118. LPTSTR pRegKey,
  119. LPTSTR pValue
  120. )
  121. /*++
  122. Routine Description:
  123. Save a string value to the PrinterData registry key
  124. Arguments:
  125. hPrinter - Identifies the printer object
  126. pRegKey - Specifies the name of registry value
  127. pValue - Points to string value to be saved
  128. Return Value:
  129. TRUE if successful, FALSE if there is an error
  130. --*/
  131. {
  132. if (SetPrinterData(hPrinter,
  133. pRegKey,
  134. REG_SZ,
  135. (PBYTE) pValue,
  136. sizeof(TCHAR) * (_tcslen(pValue) + 1)) != ERROR_SUCCESS)
  137. {
  138. Error(("Couldn't save registry key %ws: %d\n", pRegKey, GetLastError()));
  139. return FALSE;
  140. }
  141. return TRUE;
  142. }
  143. BOOL
  144. SetPrinterDataDWord(
  145. HANDLE hPrinter,
  146. PWSTR pRegKey,
  147. DWORD value
  148. )
  149. /*++
  150. Routine Description:
  151. Save a DWORD value under PrinterData registry key
  152. Arguments:
  153. hPrinter - Specifies the printer in question
  154. pRegKey - Specifies the name of registry value
  155. value - Specifies the value to be saved
  156. Return Value:
  157. TRUE if successful, FALSE otherwise
  158. --*/
  159. {
  160. if (SetPrinterData(hPrinter,
  161. pRegKey,
  162. REG_DWORD,
  163. (PBYTE) &value,
  164. sizeof(value)) != ERROR_SUCCESS)
  165. {
  166. Error(("Couldn't save registry key %ws: %d\n", pRegKey, GetLastError()));
  167. return FALSE;
  168. }
  169. return TRUE;
  170. }
  171. PVOID
  172. MyGetPrinterDriver(
  173. HANDLE hPrinter,
  174. DWORD level
  175. )
  176. /*++
  177. Routine Description:
  178. Wrapper function for GetPrinterDriver spooler API
  179. Arguments:
  180. hPrinter - Identifies the printer in question
  181. level - Specifies the level of DRIVER_INFO_x structure requested
  182. Return Value:
  183. Pointer to a DRIVER_INFO_x structure, NULL if there is an error
  184. --*/
  185. {
  186. PBYTE pDriverInfo = NULL;
  187. DWORD cbNeeded;
  188. if (!GetPrinterDriver(hPrinter, NULL, level, NULL, 0, &cbNeeded) &&
  189. GetLastError() == ERROR_INSUFFICIENT_BUFFER &&
  190. (pDriverInfo = MemAlloc(cbNeeded)) &&
  191. GetPrinterDriver(hPrinter, NULL, level, pDriverInfo, cbNeeded, &cbNeeded))
  192. {
  193. return pDriverInfo;
  194. }
  195. Error(("GetPrinterDriver failed: %d\n", GetLastError()));
  196. MemFree(pDriverInfo);
  197. return NULL;
  198. }
  199. LPTSTR
  200. MyGetPrinterDriverDirectory(
  201. LPTSTR pServerName,
  202. LPTSTR pEnvironment
  203. )
  204. /*++
  205. Routine Description:
  206. Wrapper function for GetPrinterDriverDirectory spooler API
  207. Arguments:
  208. pServerName - Specifies the name of the print server, NULL for local machine
  209. pEnvironment - Specifies the processor architecture
  210. Return Value:
  211. Pointer to the printer driver directory on the specified print server
  212. NULL if there is an error
  213. --*/
  214. {
  215. PVOID pDriverDir = NULL;
  216. DWORD cb;
  217. if (! GetPrinterDriverDirectory(pServerName, pEnvironment, 1, NULL, 0, &cb) &&
  218. GetLastError() == ERROR_INSUFFICIENT_BUFFER &&
  219. (pDriverDir = MemAlloc(cb)) &&
  220. GetPrinterDriverDirectory(pServerName, pEnvironment, 1, pDriverDir, cb, &cb))
  221. {
  222. return pDriverDir;
  223. }
  224. Error(("GetPrinterDriverDirectory failed: %d\n", GetLastError()));
  225. MemFree(pDriverDir);
  226. return NULL;
  227. }
  228. #endif // !KERNEL_MODE