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.

814 lines
30 KiB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "windows.h"
  5. VOID
  6. DumpCommState(
  7. IN DCB MyDcb
  8. ) {
  9. printf("Current comm state: \n");
  10. printf("Size of DCB: %d\n",MyDcb.DCBlength);
  11. printf("Baud rate: %d\n",MyDcb.BaudRate);
  12. printf("Binary mode: %s\n",MyDcb.fBinary?"TRUE":"FALSE");
  13. printf("Parity checking: %s\n",MyDcb.fParity?"TRUE":"FALSE");
  14. printf("Out CTS Flow: %s\n",MyDcb.fOutxCtsFlow?"TRUE":"FALSE");
  15. printf("Out DSR Flow: %s\n",MyDcb.fOutxDsrFlow?"TRUE":"FALSE");
  16. printf("DTR control: %s\n",MyDcb.fDtrControl==DTR_CONTROL_DISABLE?"DISABLE":
  17. MyDcb.fDtrControl==DTR_CONTROL_ENABLE?"ENABLE":
  18. MyDcb.fDtrControl==DTR_CONTROL_HANDSHAKE?"HANDSHAKE":"INVALID!");
  19. printf("Dsr sensitive: %s\n",MyDcb.fDsrSensitivity?"TRUE":"FALSE");
  20. printf("Tx Contin xoff: %s\n",MyDcb.fTXContinueOnXoff?"TRUE":"FALSE");
  21. printf("Output X on/off: %s\n",MyDcb.fOutX?"ON":"OFF");
  22. printf("Input X on/off: %s\n",MyDcb.fInX?"ON":"OFF");
  23. printf("Error replace: %s\n",MyDcb.fErrorChar?"ON":"OFF");
  24. printf("Null stripping: %s\n",MyDcb.fNull?"ON":"OFF");
  25. printf("RTS control: %s\n",MyDcb.fRtsControl==RTS_CONTROL_DISABLE?"DISABLE":
  26. MyDcb.fRtsControl==RTS_CONTROL_ENABLE?"ENABLE":
  27. MyDcb.fRtsControl==RTS_CONTROL_HANDSHAKE?"HANDSHAKE":"TOGGLE");
  28. printf("Abort on error: %s\n",MyDcb.fAbortOnError?"ON":"OFF");
  29. printf("Xon Limit: %d\n",MyDcb.XonLim);
  30. printf("Xoff Limit: %d\n",MyDcb.XoffLim);
  31. printf("Valid bits/byte: %d\n",MyDcb.ByteSize);
  32. printf("Parity: %s\n",MyDcb.Parity==EVENPARITY?"EVEN":
  33. MyDcb.Parity==ODDPARITY?"ODD":
  34. MyDcb.Parity==MARKPARITY?"MARK":
  35. MyDcb.Parity==NOPARITY?"NO":"INVALID");
  36. printf("Stop bits: %s\n",MyDcb.StopBits==ONESTOPBIT?"1":
  37. MyDcb.StopBits==TWOSTOPBITS?"2":
  38. MyDcb.StopBits==ONE5STOPBITS?"1.5":"INVALID");
  39. printf("Xoff char: %x\n",MyDcb.XoffChar);
  40. printf("Xon char: %x\n",MyDcb.XonChar);
  41. printf("Error char: %x\n",MyDcb.ErrorChar);
  42. printf("EOF char: %x\n",MyDcb.EofChar);
  43. printf("Evt char: %x\n",MyDcb.EvtChar);
  44. }
  45. int __cdecl main(int argc,char *argv[]) {
  46. HANDLE hFile;
  47. char *MyPort = "COM1";
  48. DWORD ValueFromEscape = 0;
  49. DWORD Func;
  50. DCB MyDcb;
  51. COMMPROP MyCommProp;
  52. DWORD NewXon;
  53. DWORD NewXoff;
  54. int scanfval;
  55. if (argc > 1) {
  56. MyPort = argv[1];
  57. }
  58. if ((hFile = CreateFile(
  59. MyPort,
  60. GENERIC_READ | GENERIC_WRITE,
  61. 0,
  62. NULL,
  63. CREATE_ALWAYS,
  64. FILE_ATTRIBUTE_NORMAL,
  65. NULL
  66. )) != ((HANDLE)-1)) {
  67. printf("We successfully opened the %s port.\n",MyPort);
  68. do {
  69. printf("^z=END 1=SETXOFF 2=SETXON 3=SETRTS\n"
  70. " 4=CLRRTS 5=SETDTR 6=CLRDTR\n"
  71. " 7=RESETDEV 8=SETBREAK 9=CLRBREAK\n"
  72. " SET 10=DSR HAND 11=CTS HAND 12=DCD HAND\n"
  73. " CLEAR 13=DSR HAND 14=CTS HAND 15=DCD HAND\n"
  74. " 16=DTR CONTROL ENABLE\n"
  75. " 17=DTR CONTROL DISABLE\n"
  76. " 18=DTR INPUT HANDSHAKE ENABLE\n"
  77. " 19=DTR INPUT HANDSHARE DISABLE (implies 17)\n"
  78. " 20=RTS CONTROL ENABLE\n"
  79. " 21=RTS CONTROL DISABLE\n"
  80. " 22=RTS INPUT HANDSHAKE ENABLE\n"
  81. " 23=RTS INPUT HANDSHAKE DISABLE (implies 21)\n"
  82. " 24=ClearCommError 25=GetCommModemStatus\n"
  83. " SET 26=AUTO RECEIVE Xxxx 27=AUTO TRANSMIT Xxxx\n"
  84. " CLEAR 28=AUTO RECEIVE Xxxx 29=AUTO TRANSMIT Xxxx: \n"
  85. " 30=Get Queue Sizes\n"
  86. " 31=Get XonLimit 32=Get XoffLimit\n"
  87. " 33=Set XonLimit 34=Set XoffLimit\n"
  88. " 35=Dump Comm State: ");
  89. if ((scanfval = scanf("%d",&Func)) == EOF) {
  90. printf("All done\n");
  91. break;
  92. } else if (scanfval != 1) {
  93. printf("Invalid input\n");
  94. } else {
  95. if ((Func >= 1) && (Func <= 35)) {
  96. switch (Func) {
  97. case 1:
  98. if (!EscapeCommFunction(
  99. hFile,
  100. SETXON
  101. )) {
  102. printf("Bad status from escape: %d\n",GetLastError());
  103. }
  104. break;
  105. case 2:
  106. if (!EscapeCommFunction(
  107. hFile,
  108. SETXOFF
  109. )) {
  110. printf("Bad status from escape: %d\n",GetLastError());
  111. }
  112. break;
  113. case 3:
  114. if (!EscapeCommFunction(
  115. hFile,
  116. SETRTS
  117. )) {
  118. printf("Bad status from escape: %d\n",GetLastError());
  119. }
  120. break;
  121. case 4:
  122. if (!EscapeCommFunction(
  123. hFile,
  124. CLRRTS
  125. )) {
  126. printf("Bad status from escape: %d\n",GetLastError());
  127. }
  128. break;
  129. case 5:
  130. if (!EscapeCommFunction(
  131. hFile,
  132. SETDTR
  133. )) {
  134. printf("Bad status from escape: %d\n",GetLastError());
  135. }
  136. break;
  137. case 6:
  138. if (!EscapeCommFunction(
  139. hFile,
  140. CLRDTR
  141. )) {
  142. printf("Bad status from escape: %d\n",GetLastError());
  143. }
  144. break;
  145. case 7:
  146. if (!EscapeCommFunction(
  147. hFile,
  148. RESETDEV
  149. )) {
  150. printf("Bad status from escape: %d\n",GetLastError());
  151. }
  152. break;
  153. case 8:
  154. if (!EscapeCommFunction(
  155. hFile,
  156. SETBREAK
  157. )) {
  158. printf("Bad status from escape: %d\n",GetLastError());
  159. }
  160. break;
  161. case 9:
  162. if (!EscapeCommFunction(
  163. hFile,
  164. CLRBREAK
  165. )) {
  166. printf("Bad status from escape: %d\n",GetLastError());
  167. }
  168. break;
  169. case 10:
  170. if (!GetCommState(
  171. hFile,
  172. &MyDcb
  173. )) {
  174. printf("Couldn't get comm state: %d\n",GetLastError());
  175. } else {
  176. MyDcb.fOutxDsrFlow = TRUE;
  177. if (!SetCommState(
  178. hFile,
  179. &MyDcb
  180. )) {
  181. printf("Couldn't set comm state: %d\n",GetLastError());
  182. }
  183. }
  184. break;
  185. case 11:
  186. if (!GetCommState(
  187. hFile,
  188. &MyDcb
  189. )) {
  190. printf("Couldn't get comm state: %d\n",GetLastError());
  191. } else {
  192. MyDcb.fOutxCtsFlow = TRUE;
  193. if (!SetCommState(
  194. hFile,
  195. &MyDcb
  196. )) {
  197. printf("Couldn't set comm state: %d\n",GetLastError());
  198. }
  199. }
  200. break;
  201. case 12:
  202. printf("Setting DCD HandShaking not implemented\n");
  203. break;
  204. case 13:
  205. if (!GetCommState(
  206. hFile,
  207. &MyDcb
  208. )) {
  209. printf("Couldn't get comm state: %d\n",GetLastError());
  210. } else {
  211. MyDcb.fOutxDsrFlow = FALSE;
  212. if (!SetCommState(
  213. hFile,
  214. &MyDcb
  215. )) {
  216. printf("Couldn't set comm state: %d\n",GetLastError());
  217. }
  218. }
  219. break;
  220. case 14:
  221. if (!GetCommState(
  222. hFile,
  223. &MyDcb
  224. )) {
  225. printf("Couldn't get comm state: %d\n",GetLastError());
  226. } else {
  227. MyDcb.fOutxCtsFlow = FALSE;
  228. if (!SetCommState(
  229. hFile,
  230. &MyDcb
  231. )) {
  232. printf("Couldn't set comm state: %d\n",GetLastError());
  233. }
  234. }
  235. break;
  236. case 15:
  237. printf("Clearing DCD HandShaking is not implemented.\n");
  238. break;
  239. case 16:
  240. if (!GetCommState(
  241. hFile,
  242. &MyDcb
  243. )) {
  244. printf("Couldn't get comm state: %d\n",GetLastError());
  245. } else {
  246. MyDcb.fDtrControl = DTR_CONTROL_ENABLE;
  247. if (!SetCommState(
  248. hFile,
  249. &MyDcb
  250. )) {
  251. printf("Couldn't set comm state: %d\n",GetLastError());
  252. }
  253. }
  254. break;
  255. case 17:
  256. if (!GetCommState(
  257. hFile,
  258. &MyDcb
  259. )) {
  260. printf("Couldn't get comm state: %d\n",GetLastError());
  261. } else {
  262. MyDcb.fDtrControl = DTR_CONTROL_DISABLE;
  263. if (!SetCommState(
  264. hFile,
  265. &MyDcb
  266. )) {
  267. printf("Couldn't set comm state: %d\n",GetLastError());
  268. }
  269. }
  270. break;
  271. case 18:
  272. if (!GetCommState(
  273. hFile,
  274. &MyDcb
  275. )) {
  276. printf("Couldn't get comm state: %d\n",GetLastError());
  277. } else {
  278. MyDcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
  279. if (!SetCommState(
  280. hFile,
  281. &MyDcb
  282. )) {
  283. printf("Couldn't set comm state: %d\n",GetLastError());
  284. }
  285. }
  286. break;
  287. case 19:
  288. if (!GetCommState(
  289. hFile,
  290. &MyDcb
  291. )) {
  292. printf("Couldn't get comm state: %d\n",GetLastError());
  293. } else {
  294. MyDcb.fDtrControl = DTR_CONTROL_DISABLE;
  295. if (!SetCommState(
  296. hFile,
  297. &MyDcb
  298. )) {
  299. printf("Couldn't set comm state: %d\n",GetLastError());
  300. }
  301. }
  302. break;
  303. case 20:
  304. if (!GetCommState(
  305. hFile,
  306. &MyDcb
  307. )) {
  308. printf("Couldn't get comm state: %d\n",GetLastError());
  309. } else {
  310. MyDcb.fRtsControl = RTS_CONTROL_ENABLE;
  311. if (!SetCommState(
  312. hFile,
  313. &MyDcb
  314. )) {
  315. printf("Couldn't set comm state: %d\n",GetLastError());
  316. }
  317. }
  318. break;
  319. case 21:
  320. if (!GetCommState(
  321. hFile,
  322. &MyDcb
  323. )) {
  324. printf("Couldn't get comm state: %d\n",GetLastError());
  325. } else {
  326. MyDcb.fRtsControl = RTS_CONTROL_DISABLE;
  327. if (!SetCommState(
  328. hFile,
  329. &MyDcb
  330. )) {
  331. printf("Couldn't set comm state: %d\n",GetLastError());
  332. }
  333. }
  334. break;
  335. case 22:
  336. if (!GetCommState(
  337. hFile,
  338. &MyDcb
  339. )) {
  340. printf("Couldn't get comm state: %d\n",GetLastError());
  341. } else {
  342. MyDcb.fRtsControl = DTR_CONTROL_HANDSHAKE;
  343. if (!SetCommState(
  344. hFile,
  345. &MyDcb
  346. )) {
  347. printf("Couldn't set comm state: %d\n",GetLastError());
  348. }
  349. }
  350. break;
  351. case 23:
  352. if (!GetCommState(
  353. hFile,
  354. &MyDcb
  355. )) {
  356. printf("Couldn't get comm state: %d\n",GetLastError());
  357. } else {
  358. MyDcb.fRtsControl = DTR_CONTROL_DISABLE;
  359. if (!SetCommState(
  360. hFile,
  361. &MyDcb
  362. )) {
  363. printf("Couldn't set comm state: %d\n",GetLastError());
  364. }
  365. }
  366. break;
  367. case 24: {
  368. DWORD Errors;
  369. COMSTAT LocalComStat = {0};
  370. if (!ClearCommError(
  371. hFile,
  372. &Errors,
  373. &LocalComStat
  374. )) {
  375. printf("Couldn't clear errors: %d\n",GetLastError());
  376. } else {
  377. if (!Errors) {
  378. printf("No errors\n");
  379. } else {
  380. if (Errors & CE_RXOVER) {
  381. printf("Error: CE_RXOVER\n");
  382. }
  383. if (Errors & CE_OVERRUN) {
  384. printf("Error: CE_OVERRUN\n");
  385. }
  386. if (Errors & CE_RXPARITY) {
  387. printf("Error: CE_RXPARITY\n");
  388. }
  389. if (Errors & CE_FRAME) {
  390. printf("Error: CE_FRAME\n");
  391. }
  392. if (Errors & CE_BREAK) {
  393. printf("Error: CE_BREAK\n");
  394. }
  395. if (Errors & ~(CE_RXOVER |
  396. CE_OVERRUN |
  397. CE_RXPARITY |
  398. CE_FRAME |
  399. CE_BREAK)) {
  400. printf("Unknown errors: %x\n",Errors);
  401. }
  402. }
  403. printf("Comstat values------------\n");
  404. if (LocalComStat.fCtsHold) {
  405. printf("Holding due to Cts\n");
  406. }
  407. if (LocalComStat.fDsrHold) {
  408. printf("Holding due to Dsr\n");
  409. }
  410. if (LocalComStat.fRlsdHold) {
  411. printf("Holding due to Xoff received\n");
  412. }
  413. if (LocalComStat.fXoffSent) {
  414. printf("Holding due to Xoff sent\n");
  415. }
  416. if (LocalComStat.fEof) {
  417. printf("Eof Seen\n");
  418. }
  419. if (LocalComStat.fTxim) {
  420. printf("Immediate character being sent\n");
  421. }
  422. printf("Chars in typeahead: %d\n",LocalComStat.cbInQue);
  423. printf("Chars to be sent: %d\n",LocalComStat.cbOutQue);
  424. }
  425. break;
  426. }
  427. case 25: {
  428. DWORD ModemStat;
  429. if (!GetCommModemStatus(
  430. hFile,
  431. &ModemStat
  432. )) {
  433. printf("Couldn't get the modem stat: %d\n",GetLastError());
  434. } else {
  435. printf("Modem bits set---------\n");
  436. if (ModemStat & MS_CTS_ON) {
  437. printf("CTS is ON\n");
  438. }
  439. if (ModemStat & MS_DSR_ON) {
  440. printf("DSR is ON\n");
  441. }
  442. if (ModemStat & MS_RING_ON) {
  443. printf("RING is ON\n");
  444. }
  445. if (ModemStat & MS_RLSD_ON) {
  446. printf("RLSD is ON\n");
  447. }
  448. if (ModemStat & ~(MS_CTS_ON |
  449. MS_DSR_ON |
  450. MS_RING_ON |
  451. MS_RLSD_ON)) {
  452. printf("Unknown Modem Stat: %x\n",ModemStat);
  453. }
  454. }
  455. break;
  456. }
  457. case 26:
  458. if (!GetCommState(
  459. hFile,
  460. &MyDcb
  461. )) {
  462. printf("Couldn't get comm state: %d\n",GetLastError());
  463. } else {
  464. MyDcb.fInX = TRUE;
  465. if (!SetCommState(
  466. hFile,
  467. &MyDcb
  468. )) {
  469. printf("Couldn't set comm state: %d\n",GetLastError());
  470. }
  471. }
  472. break;
  473. case 27:
  474. if (!GetCommState(
  475. hFile,
  476. &MyDcb
  477. )) {
  478. printf("Couldn't get comm state: %d\n",GetLastError());
  479. } else {
  480. MyDcb.fOutX = TRUE;
  481. if (!SetCommState(
  482. hFile,
  483. &MyDcb
  484. )) {
  485. printf("Couldn't set comm state: %d\n",GetLastError());
  486. }
  487. }
  488. break;
  489. case 28:
  490. if (!GetCommState(
  491. hFile,
  492. &MyDcb
  493. )) {
  494. printf("Couldn't get comm state: %d\n",GetLastError());
  495. } else {
  496. MyDcb.fInX = FALSE;
  497. if (!SetCommState(
  498. hFile,
  499. &MyDcb
  500. )) {
  501. printf("Couldn't set comm state: %d\n",GetLastError());
  502. }
  503. }
  504. break;
  505. case 29:
  506. if (!GetCommState(
  507. hFile,
  508. &MyDcb
  509. )) {
  510. printf("Couldn't get comm state: %d\n",GetLastError());
  511. } else {
  512. MyDcb.fOutX = FALSE;
  513. if (!SetCommState(
  514. hFile,
  515. &MyDcb
  516. )) {
  517. printf("Couldn't set comm state: %d\n",GetLastError());
  518. }
  519. }
  520. break;
  521. case 30:
  522. if (!GetCommProperties(
  523. hFile,
  524. &MyCommProp
  525. )) {
  526. printf("Couldn't get comm state: %d\n",GetLastError());
  527. } else {
  528. printf("Rx Queue Size: %d Tx Queue Size: %d\n",
  529. MyCommProp.dwCurrentRxQueue,
  530. MyCommProp.dwCurrentTxQueue
  531. );
  532. }
  533. break;
  534. case 31:
  535. if (!GetCommState(
  536. hFile,
  537. &MyDcb
  538. )) {
  539. printf("Couldn't get comm state: %d\n",GetLastError());
  540. } else {
  541. printf("XonLimit is %d\n",MyDcb.XonLim);
  542. }
  543. break;
  544. case 32:
  545. if (!GetCommState(
  546. hFile,
  547. &MyDcb
  548. )) {
  549. printf("Couldn't get comm state: %d\n",GetLastError());
  550. } else {
  551. printf("XoffLimit is %d\n",MyDcb.XoffLim);
  552. }
  553. break;
  554. case 33:
  555. printf("The New Xon limit Value: ");
  556. scanf("%d",&NewXon);
  557. printf("Setting Xon limit to %d\n",NewXon);
  558. if (!GetCommState(
  559. hFile,
  560. &MyDcb
  561. )) {
  562. printf("Couldn't get comm state: %d\n",GetLastError());
  563. } else {
  564. MyDcb.XonLim = (WORD)NewXon;
  565. if (!SetCommState(
  566. hFile,
  567. &MyDcb
  568. )) {
  569. printf("Couldn't set comm state: %d\n",GetLastError());
  570. }
  571. }
  572. break;
  573. case 34:
  574. printf("The New Xoff limit Value: ");
  575. scanf("%d",&NewXoff);
  576. printf("Setting Xoff limit to %d\n",NewXoff);
  577. if (!GetCommState(
  578. hFile,
  579. &MyDcb
  580. )) {
  581. printf("Couldn't get comm state: %d\n",GetLastError());
  582. } else {
  583. MyDcb.XoffLim = (WORD)NewXoff;
  584. if (!SetCommState(
  585. hFile,
  586. &MyDcb
  587. )) {
  588. printf("Couldn't set comm state: %d\n",GetLastError());
  589. }
  590. }
  591. break;
  592. case 35:
  593. if (!GetCommState(
  594. hFile,
  595. &MyDcb
  596. )) {
  597. printf("Couldn't get comm state: %d\n",GetLastError());
  598. } else {
  599. DumpCommState(MyDcb);
  600. }
  601. break;
  602. }
  603. } else {
  604. printf("Invalid Input\n");
  605. }
  606. }
  607. } while (TRUE);
  608. CloseHandle(hFile);
  609. } else {
  610. DWORD LastError;
  611. LastError = GetLastError();
  612. printf("Couldn't open the %s device.\n",MyPort);
  613. printf("Status of failed open is: %x\n",LastError);
  614. }
  615. return 1;
  616. }