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.

407 lines
11 KiB

  1. #include "tcdata.h"
  2. #include "tcsrvc.h"
  3. // The key to the registry where the paramters are present.
  4. //
  5. // Called by CRT when _DllMainCRTStartup is the DLL entry point
  6. //
  7. BOOL
  8. WINAPI
  9. DllMain(
  10. IN HANDLE DllHandle,
  11. IN DWORD Reason,
  12. IN LPVOID Reserved
  13. )
  14. {
  15. HKEY lock,l_hkey;
  16. LONG retVal;
  17. DWORD disposition;
  18. UNREFERENCED_PARAMETER(Reserved);
  19. UNREFERENCED_PARAMETER(Reason);
  20. return TRUE;
  21. }
  22. LONG
  23. TCLock(
  24. PHANDLE lock
  25. )
  26. {
  27. LONG RetVal;
  28. (*lock) = CreateMutex(NULL,
  29. FALSE,
  30. TCSERV_MUTEX_NAME
  31. );
  32. if ((*lock)) {
  33. RetVal = WaitForSingleObject(*lock,INFINITE);
  34. if (RetVal == WAIT_FAILED) {
  35. return GetLastError();
  36. }
  37. else{
  38. return ERROR_SUCCESS;
  39. }
  40. }
  41. else{
  42. return GetLastError();
  43. }
  44. }
  45. VOID
  46. TCUnlock(
  47. HANDLE lock
  48. )
  49. {
  50. if (lock) {
  51. ReleaseMutex(lock);
  52. CloseHandle(lock);
  53. }
  54. return;
  55. }
  56. LONG GetParametersByName(
  57. TCHAR *name,
  58. int *nameLen,
  59. TCHAR *device,
  60. int *deviceLen,
  61. PUCHAR stopBits,
  62. PUCHAR parity,
  63. PUINT baudRate,
  64. PUCHAR wordLen
  65. )
  66. {
  67. LONG RetVal;
  68. HKEY m_child;
  69. DWORD lpcdata, lpType,dat;
  70. HANDLE lock;
  71. HKEY m_hkey;
  72. RetVal = TCLock(&lock);
  73. if (RetVal != ERROR_SUCCESS) {
  74. TCUnlock(lock);
  75. return RetVal;
  76. }
  77. RetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  78. HKEY_TCSERV_PARAMETER_KEY,
  79. 0,
  80. KEY_ALL_ACCESS,
  81. &m_hkey
  82. );
  83. if(RetVal != ERROR_SUCCESS){
  84. TCUnlock(lock);
  85. return RetVal;
  86. }
  87. RetVal= RegOpenKeyEx(m_hkey,
  88. name, // subkey name
  89. 0, // reserved
  90. KEY_ALL_ACCESS, // security access mask
  91. &m_child
  92. );
  93. if(RetVal != ERROR_SUCCESS){
  94. RegCloseKey(m_hkey);
  95. TCUnlock(lock);
  96. return RetVal;
  97. }
  98. RetVal = RegQueryValueEx(m_child,
  99. _TEXT("Device"),
  100. NULL,
  101. &lpType,
  102. (LPBYTE)device,
  103. deviceLen
  104. );
  105. (*deviceLen) = (*deviceLen) - 1;
  106. if(RetVal != ERROR_SUCCESS){
  107. RegCloseKey(m_child);
  108. RegCloseKey(m_hkey);
  109. TCUnlock(lock);
  110. return RetVal;
  111. }
  112. lpcdata = sizeof(UINT);
  113. RetVal = RegQueryValueEx(m_child,
  114. _TEXT("Baud Rate"),
  115. NULL,
  116. &lpType,
  117. (LPBYTE)baudRate,
  118. &lpcdata
  119. );
  120. lpcdata = sizeof(DWORD);
  121. dat = (DWORD) *stopBits;
  122. RetVal = RegQueryValueEx(m_child,
  123. _TEXT("Stop Bits"),
  124. NULL,
  125. &lpType,
  126. (LPBYTE)&dat,
  127. &lpcdata
  128. );
  129. *stopBits = (UCHAR) dat;
  130. dat = (DWORD) *wordLen;
  131. lpcdata = sizeof(DWORD);
  132. RetVal = RegQueryValueEx(m_child,
  133. _TEXT("Word Length"),
  134. NULL,
  135. &lpType,
  136. (LPBYTE)&dat,
  137. &lpcdata
  138. );
  139. *wordLen = (UCHAR) dat;
  140. lpcdata = sizeof(DWORD);
  141. dat = (DWORD) *parity;
  142. RetVal = RegQueryValueEx(m_child,
  143. _TEXT("Parity"),
  144. NULL,
  145. &lpType,
  146. (LPBYTE)&dat,
  147. &lpcdata
  148. );
  149. *parity = (UCHAR) dat;
  150. RegCloseKey(m_child);
  151. RegCloseKey(m_hkey);
  152. TCUnlock(lock);
  153. return ERROR_SUCCESS;
  154. }
  155. LONG
  156. GetParametersAtIndex(
  157. int index,
  158. TCHAR *name,
  159. int *nameLen,
  160. TCHAR *device,
  161. int *deviceLen,
  162. PUCHAR stopBits,
  163. PUCHAR parity,
  164. PUINT baudRate,
  165. PUCHAR wordLen
  166. )
  167. {
  168. LONG RetVal;
  169. FILETIME lpftLastWriteTime;
  170. HANDLE lock;
  171. HKEY m_hkey;
  172. if ((name == NULL) || (device == NULL)) {
  173. return -1;
  174. }
  175. RetVal = TCLock(&lock);
  176. if (RetVal != ERROR_SUCCESS) {
  177. TCUnlock(lock);
  178. return RetVal;
  179. }
  180. RetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  181. HKEY_TCSERV_PARAMETER_KEY,
  182. 0,
  183. KEY_ALL_ACCESS,
  184. &m_hkey
  185. );
  186. if(RetVal != ERROR_SUCCESS){
  187. TCUnlock(lock);
  188. return RetVal;
  189. }
  190. RetVal = RegEnumKeyEx(m_hkey,
  191. index,
  192. name,
  193. nameLen,
  194. NULL,
  195. NULL,
  196. NULL,
  197. &lpftLastWriteTime
  198. );
  199. if(RetVal != ERROR_SUCCESS){
  200. RegCloseKey(m_hkey);
  201. TCUnlock(lock);
  202. return RetVal;
  203. }
  204. RegCloseKey(m_hkey);
  205. TCUnlock(lock);
  206. return (GetParametersByName(name,
  207. nameLen,
  208. device,
  209. deviceLen,
  210. stopBits,
  211. parity,
  212. baudRate,
  213. wordLen));
  214. }
  215. LONG
  216. SetParameters(
  217. TCHAR *name,
  218. TCHAR *device,
  219. PUCHAR stopBits,
  220. PUCHAR parity,
  221. PUINT baudRate,
  222. PUCHAR wordLen
  223. )
  224. {
  225. LONG RetVal;
  226. HKEY m_child, m_hkey;
  227. int lpcdata;
  228. DWORD dat;
  229. HANDLE lock;
  230. if ((name == NULL) || (device == NULL)) {
  231. return -1;
  232. }
  233. if(_tcslen(name) == 0 || _tcslen(device) == 0){
  234. return -1;
  235. }
  236. RetVal = TCLock(&lock);
  237. if (RetVal != ERROR_SUCCESS) {
  238. TCUnlock(lock);
  239. return RetVal;
  240. }
  241. RetVal = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  242. HKEY_TCSERV_PARAMETER_KEY,
  243. 0,
  244. NULL,
  245. 0,
  246. KEY_ALL_ACCESS,
  247. NULL,
  248. &m_hkey,
  249. NULL
  250. );
  251. if(RetVal != ERROR_SUCCESS){
  252. RegCloseKey(m_hkey);
  253. TCUnlock(lock);
  254. return RetVal;
  255. }
  256. RetVal= RegCreateKeyEx(m_hkey,
  257. name, // subkey name
  258. 0, // reserved
  259. NULL,
  260. 0,
  261. KEY_ALL_ACCESS, // security access mask
  262. NULL,
  263. &m_child,
  264. NULL
  265. );
  266. if(RetVal != ERROR_SUCCESS){
  267. RegCloseKey(m_hkey);
  268. TCUnlock(lock);
  269. return RetVal;
  270. }
  271. lpcdata = _tcslen(device)*sizeof(TCHAR);
  272. RetVal = RegSetValueEx(m_child,
  273. _TEXT("Device"),
  274. 0,
  275. REG_SZ,
  276. (LPBYTE)device,
  277. lpcdata
  278. );
  279. if(RetVal != ERROR_SUCCESS){
  280. RegCloseKey(m_child);
  281. RegCloseKey(m_hkey);
  282. TCUnlock(lock);
  283. return RetVal;
  284. }
  285. if(baudRate){
  286. lpcdata = sizeof(UINT);
  287. RetVal = RegSetValueEx(m_child,
  288. _TEXT("Baud Rate"),
  289. 0,
  290. REG_DWORD,
  291. (LPBYTE)baudRate,
  292. lpcdata
  293. );
  294. if(RetVal != ERROR_SUCCESS){
  295. RegCloseKey(m_child);
  296. RegCloseKey(m_hkey);
  297. TCUnlock(lock);
  298. return RetVal;
  299. }
  300. }
  301. if(stopBits){
  302. dat =(DWORD) *stopBits;
  303. lpcdata = sizeof(DWORD);
  304. RetVal = RegSetValueEx(m_child,
  305. _TEXT("Stop Bits"),
  306. 0,
  307. REG_DWORD,
  308. (LPBYTE)&dat,
  309. lpcdata
  310. );
  311. if(RetVal != ERROR_SUCCESS){
  312. RegCloseKey(m_child);
  313. RegCloseKey(m_hkey);
  314. TCUnlock(lock);
  315. return RetVal;
  316. }
  317. }
  318. if(wordLen){
  319. lpcdata = sizeof(DWORD);
  320. dat = (DWORD) *wordLen;
  321. RetVal = RegSetValueEx(m_child,
  322. _TEXT("Word Length"),
  323. 0,
  324. REG_DWORD,
  325. (LPBYTE)&dat,
  326. lpcdata
  327. );
  328. if(RetVal != ERROR_SUCCESS){
  329. RegCloseKey(m_child);
  330. RegCloseKey(m_hkey);
  331. TCUnlock(lock);
  332. return RetVal;
  333. }
  334. }
  335. if(parity){
  336. lpcdata = sizeof(DWORD);
  337. dat = (DWORD) *parity;
  338. RetVal = RegSetValueEx(m_child,
  339. _TEXT("Parity"),
  340. 0,
  341. REG_DWORD,
  342. (LPBYTE)&dat,
  343. lpcdata
  344. );
  345. if(RetVal != ERROR_SUCCESS){
  346. RegCloseKey(m_child);
  347. RegCloseKey(m_hkey);
  348. TCUnlock(lock);
  349. return RetVal;
  350. }
  351. }
  352. RegCloseKey(m_child);
  353. RegCloseKey(m_hkey);
  354. TCUnlock(lock);
  355. return ERROR_SUCCESS;
  356. }
  357. LONG
  358. DeleteKey(
  359. LPCTSTR name
  360. )
  361. {
  362. LONG RetVal;
  363. HANDLE lock;
  364. HKEY m_hkey;
  365. RetVal = TCLock(&lock);
  366. if (RetVal != ERROR_SUCCESS) {
  367. return RetVal;
  368. }
  369. RetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  370. HKEY_TCSERV_PARAMETER_KEY,
  371. 0,
  372. KEY_ALL_ACCESS,
  373. &m_hkey
  374. );
  375. if(RetVal != ERROR_SUCCESS){
  376. RegCloseKey(m_hkey);
  377. TCUnlock(lock);
  378. return RetVal;
  379. }
  380. RetVal = RegDeleteKey(m_hkey,
  381. name
  382. );
  383. RegCloseKey(m_hkey);
  384. TCUnlock(lock);
  385. return RetVal;
  386. }