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.

370 lines
8.2 KiB

  1. /*--------------------------------------------------------------
  2. *
  3. * FILE: SK_Reg.c
  4. *
  5. * PURPOSE: These functions process data to and from the registry
  6. *
  7. * CREATION: June 1994
  8. *
  9. * COPYRIGHT: Black Diamond Software (C) 1994
  10. *
  11. * AUTHOR: Ronald Moak
  12. *
  13. * $Header: %Z% %F% %H% %T% %I%
  14. *
  15. *--- Includes --------------------------------------------*/
  16. #include <windows.h>
  17. #include "w95trace.h"
  18. #include "sk_defs.h"
  19. #include "sk_comm.h"
  20. #include "sk_reg.h"
  21. // Private Functions -------------------------------------------
  22. static DWORD OpenRegistry(int User);
  23. static void CloseRegistry();
  24. static void SetRegistryValues();
  25. static void GetRegistryValues();
  26. // Variables --------------------------------------------
  27. HKEY hKeyApp;
  28. /*---------------------------------------------------------------
  29. *
  30. * FUNCTION BOOL GetUserValues()
  31. *
  32. * TYPE Local
  33. *
  34. * PURPOSE Read the registery an collect the data for the current
  35. * user. This Information is then setup in the comm routines.
  36. * This is called when someone logs into NT.
  37. *
  38. * INPUTS User Type Default or Current User
  39. *
  40. * RETURNS TRUE - User wants Serial Keys Enabled
  41. * FALSE- User wants Serial Keys Disabled
  42. *
  43. *---------------------------------------------------------------*/
  44. BOOL GetUserValues(int User)
  45. {
  46. DWORD Status;
  47. DBPRINTF(TEXT("GetUserValues()\r\n"));
  48. if (!(Status = OpenRegistry(User))) // Did Open Registry Succed?
  49. return(FALSE); // No - Fail
  50. switch (Status) // What is status?
  51. {
  52. // This case should only be true the frist time
  53. // the registry is opened for the current user.
  54. case REG_CREATED_NEW_KEY: // Is this an empty Registry?
  55. SetRegistryValues(); // Yes - Set Default Values
  56. break;
  57. case REG_OPENED_EXISTING_KEY: // Is this an existing Registry?
  58. GetRegistryValues(); // Yes - Get Values
  59. break;
  60. }
  61. CloseRegistry();
  62. return(TRUE);
  63. }
  64. /*---------------------------------------------------------------
  65. *
  66. * FUNCTION void SetUserValues()
  67. *
  68. * TYPE Global
  69. *
  70. * PURPOSE This function writes out information to the
  71. * registry.
  72. *
  73. * INPUTS None
  74. *
  75. * RETURNS TRUE - Write Successful
  76. * FALSE- Write Failed
  77. *
  78. *---------------------------------------------------------------*/
  79. BOOL SetUserValues()
  80. {
  81. DWORD Status;
  82. DBPRINTF(TEXT("SetUserValues()\r\n"));
  83. if (!(Status = OpenRegistry(REG_USER))) // Did Open Registry Succed?
  84. return(FALSE); // No - Fail
  85. SetRegistryValues(); // Set New Values
  86. CloseRegistry(); // Close Registry
  87. return(TRUE);
  88. }
  89. /*---------------------------------------------------------------
  90. *
  91. * Local Functions -
  92. *
  93. /*---------------------------------------------------------------
  94. *
  95. * FUNCTION DWORD OpenRegistry()
  96. *
  97. * TYPE Global
  98. *
  99. * PURPOSE Opens the Registry for reading or writing
  100. *
  101. * INPUTS User Type Default or Current User
  102. *
  103. * RETURNS 0 = Failed
  104. * >0 = REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
  105. *
  106. *---------------------------------------------------------------*/
  107. static DWORD OpenRegistry(int User)
  108. {
  109. LONG ret;
  110. DWORD Disposition = 0;
  111. DBPRINTF(TEXT(" OpenRegistry()\r\n"));
  112. switch (User)
  113. {
  114. case REG_USER: // Current User
  115. ret =RegCreateKeyEx
  116. (
  117. HKEY_CURRENT_USER,
  118. TEXT("Control Panel\\Accessibility\\SerialKeys"),
  119. 0,NULL,
  120. REG_OPTION_NON_VOLATILE,
  121. KEY_ALL_ACCESS,
  122. NULL,
  123. &hKeyApp,
  124. &Disposition
  125. );
  126. break;
  127. case REG_DEF: // Default
  128. ret =RegCreateKeyEx
  129. (
  130. HKEY_USERS,
  131. TEXT(".DEFAULT\\Control Panel\\Accessibility\\SerialKeys"),
  132. 0,NULL,
  133. REG_OPTION_NON_VOLATILE,
  134. KEY_ALL_ACCESS,
  135. NULL,
  136. &hKeyApp,
  137. &Disposition
  138. );
  139. break;
  140. default:
  141. ret = FALSE;
  142. break;
  143. }
  144. if (ret != ERROR_SUCCESS) // Did open succede?
  145. return(FALSE); // No -
  146. return (Disposition);
  147. }
  148. /*---------------------------------------------------------------
  149. *
  150. * FUNCTION void CloseRegistry()
  151. *
  152. * TYPE Global
  153. *
  154. * PURPOSE Closes the Registry for reading or writing
  155. *
  156. * INPUTS None
  157. *
  158. * RETURNS 0 = Failed
  159. * >0 = REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
  160. *
  161. *---------------------------------------------------------------*/
  162. static void CloseRegistry()
  163. {
  164. DBPRINTF(TEXT(" CloseRegistry()\r\n"));
  165. RegCloseKey(hKeyApp);
  166. }
  167. /*---------------------------------------------------------------
  168. *
  169. * FUNCTION void SetRegistryValues()
  170. *
  171. * TYPE Global
  172. *
  173. * PURPOSE Writes the values in the SerialKeys structure to
  174. * the Registry.
  175. *
  176. * INPUTS None
  177. *
  178. * RETURNS None
  179. *
  180. *---------------------------------------------------------------*/
  181. static void SetRegistryValues()
  182. {
  183. long ret;
  184. DWORD dwFlags;
  185. DBPRINTF(TEXT(" SetRegistryValues()\r\n"));
  186. dwFlags = skNewKey.dwFlags | SERKF_AVAILABLE;
  187. ret = RegSetValueEx( // Write dwFlags
  188. hKeyApp,
  189. REG_FLAGS,
  190. 0,REG_DWORD,
  191. (CONST LPBYTE) &dwFlags,
  192. sizeof(DWORD));
  193. if (ret != ERROR_SUCCESS) // Did open succede?
  194. {
  195. DBPRINTF(TEXT("Unable to Set Registry Value\r\n"));
  196. return; // No -
  197. }
  198. if (NULL == skNewKey.lpszActivePort)
  199. {
  200. ret = RegSetValueEx( // Write Active Port
  201. hKeyApp,
  202. REG_ACTIVEPORT,
  203. 0,
  204. REG_SZ,
  205. (CONST LPBYTE) TEXT(""),
  206. 1 * sizeof(*skNewKey.lpszActivePort)); // size of one char, the term null
  207. }
  208. else
  209. {
  210. ret = RegSetValueEx( // Write Active Port
  211. hKeyApp,
  212. REG_ACTIVEPORT,
  213. 0,
  214. REG_SZ,
  215. (CONST LPBYTE) skNewKey.lpszActivePort,
  216. (lstrlen(skNewKey.lpszActivePort) + 1) *
  217. sizeof(*skNewKey.lpszActivePort));
  218. }
  219. if (ret != ERROR_SUCCESS) // Did open succede?
  220. return; // No -
  221. if (NULL == skNewKey.lpszPort)
  222. {
  223. ret = RegSetValueEx( // Write Active Port
  224. hKeyApp,
  225. REG_PORT,
  226. 0,
  227. REG_SZ,
  228. (CONST LPBYTE) TEXT(""),
  229. 1 * sizeof(*skNewKey.lpszPort)); // size of one char, the term null
  230. }
  231. else
  232. {
  233. ret = RegSetValueEx( // Write Active Port
  234. hKeyApp,
  235. REG_PORT,
  236. 0,
  237. REG_SZ,
  238. (CONST LPBYTE)skNewKey.lpszPort,
  239. (lstrlen(skNewKey.lpszPort) + 1) * sizeof(*skNewKey.lpszPort));
  240. }
  241. if (ret != ERROR_SUCCESS) // Did open succede?
  242. return; // No -
  243. ret = RegSetValueEx // Write Active Port
  244. (
  245. hKeyApp,
  246. REG_BAUD,
  247. 0,REG_DWORD,
  248. (CONST LPBYTE) &skNewKey.iBaudRate,
  249. sizeof(skNewKey.iBaudRate)
  250. );
  251. if (ret != ERROR_SUCCESS) // Did open succede?
  252. return; // No -
  253. }
  254. /*---------------------------------------------------------------
  255. *
  256. * FUNCTION void GetRegistryValues()
  257. *
  258. * TYPE Global
  259. *
  260. * PURPOSE Reads the values in the SerialKeys structure to
  261. * the Registry.
  262. *
  263. * INPUTS None
  264. *
  265. * RETURNS None
  266. *
  267. *---------------------------------------------------------------*/
  268. static void GetRegistryValues()
  269. {
  270. long lRet;
  271. DWORD dwType;
  272. DWORD cbData;
  273. DBPRINTF(TEXT(" GetRegistryValues()\r\n"));
  274. skNewKey.dwFlags = 0;
  275. cbData = sizeof(skNewKey.dwFlags);
  276. lRet = RegQueryValueEx(
  277. hKeyApp,
  278. REG_FLAGS,
  279. 0,&dwType,
  280. (LPBYTE)&skNewKey.dwFlags,
  281. &cbData);
  282. skNewKey.dwFlags |= SERKF_AVAILABLE;
  283. if (NULL != skNewKey.lpszActivePort)
  284. {
  285. cbData = MAX_PATH * sizeof(*skNewKey.lpszActivePort);
  286. lRet = RegQueryValueEx(
  287. hKeyApp,
  288. REG_ACTIVEPORT,
  289. 0,&dwType,
  290. (LPBYTE)skNewKey.lpszActivePort,
  291. &cbData);
  292. skNewKey.lpszActivePort[ MAX_PATH - 1 ] = '\0';
  293. if (lRet != ERROR_SUCCESS || dwType != REG_SZ)
  294. {
  295. lstrcpy(skNewKey.lpszActivePort, TEXT("COM1"));
  296. }
  297. }
  298. if (NULL != skNewKey.lpszPort)
  299. {
  300. cbData = MAX_PATH * sizeof(*skNewKey.lpszPort);
  301. lRet = RegQueryValueEx( // Write Active Port
  302. hKeyApp,
  303. REG_PORT,
  304. 0,&dwType,
  305. (LPBYTE)skNewKey.lpszPort,
  306. &cbData);
  307. skNewKey.lpszActivePort[ MAX_PATH - 1 ] = '\0';
  308. if (lRet != ERROR_SUCCESS || dwType != REG_SZ)
  309. {
  310. lstrcpy(skNewKey.lpszPort, TEXT("COM1"));
  311. }
  312. }
  313. cbData = sizeof(skNewKey.iBaudRate);
  314. lRet = RegQueryValueEx( // Write Active Port
  315. hKeyApp,
  316. REG_BAUD,
  317. 0,&dwType,
  318. (LPBYTE)&skNewKey.iBaudRate,
  319. &cbData);
  320. if (ERROR_SUCCESS != lRet)
  321. {
  322. skNewKey.iBaudRate = 300;
  323. }
  324. }