Windows NT 4.0 source code leak
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.

401 lines
10 KiB

4 years ago
  1. #include "printman.h"
  2. TCHAR szRegPrintManager[] = TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Print Manager");
  3. /* WriteRegistryData
  4. *
  5. * Writes a bunch of information to the registry
  6. *
  7. * Parameters:
  8. *
  9. * pEntryNode - The node under Print Manager which should be created
  10. * or opened for this data.
  11. *
  12. * pEntryName - The name of the value under pEntryNode to be set.
  13. *
  14. * pData - Pointer to the value data to be written.
  15. *
  16. * pEntry - A pointer to a REGISTRY_ENTRY structure which contains
  17. * the offset of the data pointed at by pData.
  18. *
  19. *
  20. * This routine is fairly generic, apart from the name of the top-level node.
  21. *
  22. * The data are stored in the following registry tree:
  23. *
  24. * HKEY_CURRENT_USER
  25. *
  26. * Software
  27. *
  28. * Microsoft
  29. *
  30. * Windows NT
  31. *
  32. * CurrentVersion
  33. *
  34. * Print Manager
  35. *
  36. * Printers
  37. *
  38. * My Favourite Printer (e.g.)
  39. *
  40. * My Laserjet Printer (e.g.)
  41. *
  42. * \\NTPRINT\Laserjet (e.g.)
  43. *
  44. * Servers
  45. *
  46. * \\NTPRINT (e.g.)
  47. *
  48. *
  49. * Return:
  50. *
  51. * Registry status return (NO_ERROR is good)
  52. *
  53. *
  54. * Andrew Bell (andrewbe) wrote it, 10 September 1992
  55. *
  56. */
  57. DWORD WriteRegistryData( LPTSTR pEntryNode,
  58. LPTSTR pEntryName,
  59. LPBYTE pData,
  60. PREGISTRY_ENTRY pEntry )
  61. {
  62. DWORD Status;
  63. HKEY hkeyPrintManager;
  64. HKEY hkeyEntryNode;
  65. /* Open or create the top-level node. For Print Manager this is:
  66. * "Software\\Microsoft\\Windows NT\\CurrentVersion\\Print Manager"
  67. */
  68. Status = RegCreateKeyEx( HKEY_CURRENT_USER, szRegPrintManager, 0,
  69. NULL, 0, KEY_WRITE, NULL, &hkeyPrintManager, NULL );
  70. if( Status == NO_ERROR )
  71. {
  72. /* Open or create the sub-node. For Print Manager this is:
  73. * "Printers", "Servers" or NULL.
  74. */
  75. if( pEntryNode )
  76. Status = RegCreateKeyEx( hkeyPrintManager, pEntryNode, 0,
  77. NULL, 0, KEY_WRITE, NULL, &hkeyEntryNode, NULL );
  78. else
  79. hkeyEntryNode = hkeyPrintManager;
  80. if( Status == NO_ERROR )
  81. {
  82. Status = RegSetValueEx( hkeyEntryNode,
  83. pEntryName,
  84. 0,
  85. pEntry->Type,
  86. pData,
  87. pEntry->Size );
  88. if( pEntryNode )
  89. RegCloseKey( hkeyEntryNode );
  90. }
  91. else
  92. {
  93. DBGMSG( DBG_ERROR, ( "RegCreateKeyEx (%s) failed: Error = %d\n",
  94. pEntryNode, Status ) );
  95. }
  96. RegCloseKey( hkeyPrintManager );
  97. }
  98. else
  99. {
  100. DBGMSG( DBG_ERROR, ( "RegCreateKeyEx (%s) failed: Error = %d\n",
  101. szRegPrintManager, Status ) );
  102. }
  103. return Status;
  104. }
  105. /* ReadRegistryData
  106. *
  107. * Reads information from the registry
  108. *
  109. * Parameters:
  110. *
  111. * pEntryNode - The node under Print Manager which should be opened
  112. * for this data.
  113. *
  114. * pEntryName - The name of the value under pEntryNode to be retrieved.
  115. *
  116. * pData - Pointer to a buffer to receive the value data.
  117. *
  118. * pEntry - A pointer to a REGISTRY_ENTRY structure which contains
  119. * the name for each of the entries and the offset of the corresponding
  120. * data in pData.
  121. *
  122. *
  123. * Return:
  124. *
  125. * Registry status return (NO_ERROR is good)
  126. *
  127. *
  128. * Andrew Bell (andrewbe) wrote it, 10 September 1992
  129. *
  130. */
  131. DWORD ReadRegistryData( LPTSTR pEntryNode,
  132. LPTSTR pEntryName,
  133. LPBYTE pData,
  134. PREGISTRY_ENTRY pEntry )
  135. {
  136. DWORD Status;
  137. HKEY hkeyPrintManager;
  138. HKEY hkeyEntryNode;
  139. DWORD Size;
  140. /* Open the top-level node. For Print Manager this is:
  141. * "Software\\Microsoft\\Windows NT\\CurrentVersion\\Print Manager"
  142. */
  143. Status = RegOpenKeyEx( HKEY_CURRENT_USER, szRegPrintManager, 0,
  144. KEY_READ, &hkeyPrintManager );
  145. if( Status == NO_ERROR )
  146. {
  147. /* Open the sub-node. For Print Manager this is:
  148. * "Printers", "Servers" or NULL.
  149. */
  150. if( pEntryNode )
  151. Status = RegOpenKeyEx( hkeyPrintManager, pEntryNode, 0,
  152. KEY_READ, &hkeyEntryNode );
  153. else
  154. hkeyEntryNode = hkeyPrintManager;
  155. if( Status == NO_ERROR )
  156. {
  157. Size = pEntry->Size;
  158. /* Read the entry from the registry:
  159. */
  160. Status = RegQueryValueEx( hkeyEntryNode,
  161. pEntryName,
  162. 0,
  163. NULL,
  164. pData,
  165. &Size );
  166. if( pEntryNode )
  167. RegCloseKey( hkeyEntryNode );
  168. }
  169. else
  170. {
  171. DBGMSG( DBG_INFO, ( "RegOpenKeyEx (%s) failed: Error = %d\n",
  172. pEntryNode, Status ) );
  173. }
  174. RegCloseKey( hkeyPrintManager );
  175. }
  176. else
  177. {
  178. DBGMSG( DBG_INFO, ( "RegOpenKeyEx (%s) failed: Error = %d\n",
  179. szRegPrintManager, Status ) );
  180. }
  181. return Status;
  182. }
  183. /* EnumRegistryValues
  184. *
  185. * Enumerates the values of the specified key and calls the supplied routine.
  186. *
  187. * Arguments:
  188. *
  189. * pEntryNode - The node whose values are to enumerated.
  190. *
  191. * pfnEnum - A callback routine which will be called with pContext
  192. * and a pointer to the enumerated information.
  193. * This should return NO_ERROR as long
  194. * as the enumeration should continue.
  195. *
  196. * pContext - A pointer to the caller's data. This is passed to the callback
  197. * routine.
  198. *
  199. *
  200. * Return:
  201. *
  202. * Registry status return (NO_ERROR is good)
  203. *
  204. *
  205. * Andrew Bell (andrewbe) wrote it, 10 September 1992
  206. *
  207. */
  208. DWORD EnumRegistryValues( LPTSTR pEntryNode,
  209. ENUMREGPROC pfnEnum,
  210. PVOID pContext )
  211. {
  212. DWORD Status;
  213. HKEY hkeyPrintManager;
  214. HKEY hkeyEntryNode;
  215. TCHAR Buffer[MAX_PATH];
  216. DWORD BufferSize;
  217. DWORD i;
  218. /* Open the top-level node. For Print Manager this is:
  219. * "Software\\Microsoft\\Windows NT\\CurrentVersion\\Print Manager"
  220. */
  221. Status = RegOpenKeyEx( HKEY_CURRENT_USER, szRegPrintManager, 0,
  222. KEY_READ, &hkeyPrintManager );
  223. if( Status == NO_ERROR )
  224. {
  225. /* Open the sub-node. For Print Manager this is
  226. * "Printers" or "Servers".
  227. */
  228. Status = RegOpenKeyEx( hkeyPrintManager, pEntryNode, 0,
  229. KEY_READ, &hkeyEntryNode );
  230. if( Status == NO_ERROR )
  231. {
  232. i = 0;
  233. while( Status == NO_ERROR )
  234. {
  235. BufferSize = sizeof Buffer/sizeof(TCHAR);
  236. Status = RegEnumValue( hkeyEntryNode, i, Buffer, &BufferSize,
  237. NULL, NULL, NULL, NULL );
  238. if( Status == NO_ERROR )
  239. {
  240. Status = (*pfnEnum)( pContext, Buffer, NULL );
  241. }
  242. i++;
  243. }
  244. /* We expect RegEnumKeyEx to return ERROR_NO_MORE_ITEMS
  245. * when it gets to the end of the keys, so reset the status:
  246. */
  247. if( Status == ERROR_NO_MORE_ITEMS )
  248. Status = NO_ERROR;
  249. RegCloseKey( hkeyEntryNode );
  250. }
  251. else
  252. {
  253. DBGMSG( DBG_INFO, ( "RegOpenKeyEx (%s) failed: Error = %d\n",
  254. pEntryNode, Status ) );
  255. }
  256. RegCloseKey( hkeyPrintManager );
  257. }
  258. else
  259. {
  260. DBGMSG( DBG_INFO, ( "RegOpenKeyEx (%s) failed: Error = %d\n",
  261. szRegPrintManager, Status ) );
  262. }
  263. return Status;
  264. }
  265. /* DeleteRegistryValues
  266. *
  267. * Enumerates the values of the specified key and deletes them.
  268. *
  269. * Argument:
  270. *
  271. * pNode - The key whose values are to be deleted.
  272. *
  273. *
  274. * Return:
  275. *
  276. * Registry status return (NO_ERROR is good)
  277. *
  278. *
  279. * Andrew Bell (andrewbe) wrote it, 10 September 1992
  280. *
  281. */
  282. DWORD DeleteRegistryValues( LPTSTR pNode )
  283. {
  284. DWORD Status;
  285. DWORD DeleteStatus;
  286. HKEY hkeyPrintManager;
  287. HKEY hkeyNode;
  288. TCHAR Buffer[MAX_PATH];
  289. DWORD BufferSize;
  290. DWORD i;
  291. /* Open the top-level node. For Print Manager this is:
  292. * "Software\\Microsoft\\Windows NT\\CurrentVersion\\Print Manager"
  293. */
  294. Status = RegOpenKeyEx( HKEY_CURRENT_USER, szRegPrintManager, 0,
  295. KEY_READ | KEY_WRITE, &hkeyPrintManager );
  296. if( Status == NO_ERROR )
  297. {
  298. /* Open the sub-node. For Print Manager this is
  299. * "Printers" or "Servers".
  300. */
  301. Status = RegOpenKeyEx( hkeyPrintManager, pNode, 0,
  302. KEY_READ | KEY_WRITE, &hkeyNode );
  303. if( Status == NO_ERROR )
  304. {
  305. i = 0;
  306. while( Status == NO_ERROR )
  307. {
  308. BufferSize = sizeof Buffer;
  309. Status = RegEnumValue( hkeyNode, i, Buffer, &BufferSize,
  310. NULL, NULL, NULL, NULL );
  311. if( Status == NO_ERROR )
  312. {
  313. DeleteStatus = RegDeleteValue( hkeyNode, Buffer );
  314. if( DeleteStatus != NO_ERROR )
  315. {
  316. DBGMSG( DBG_ERROR, ( "RegDeleteValue (%s) failed: Error = %d\n",
  317. Buffer, DeleteStatus ) );
  318. }
  319. }
  320. i++;
  321. }
  322. if( Status == ERROR_NO_MORE_ITEMS )
  323. Status = NO_ERROR;
  324. RegCloseKey( hkeyNode );
  325. }
  326. else
  327. {
  328. DBGMSG( DBG_INFO, ( "RegOpenKeyEx (%s) failed: Error = %d\n",
  329. pNode, Status ) );
  330. }
  331. RegCloseKey( hkeyPrintManager );
  332. }
  333. else
  334. {
  335. DBGMSG( DBG_INFO, ( "RegOpenKeyEx (%s) failed: Error = %d\n",
  336. szRegPrintManager, Status ) );
  337. }
  338. return Status;
  339. }