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.

997 lines
27 KiB

  1. /*
  2. * Copyright (c) Microsoft Corporation
  3. *
  4. * Module Name :
  5. * utils.c
  6. *
  7. * Contains all the work needed to present the console
  8. *
  9. *
  10. * Sadagopan Rajaram -- Dec 20, 1999
  11. *
  12. */
  13. #include "tcadmin.h"
  14. BOOL AnsiStatus = (sizeof(TCHAR) == sizeof(CHAR));
  15. BOOL
  16. Browse(
  17. )
  18. {
  19. int index,j;
  20. LONG retVal;
  21. DWORD len;
  22. TCHAR name[MAX_BUFFER_SIZE];
  23. LPTSTR message[NUMBER_FIELDS + 1];
  24. TCHAR device[MAX_BUFFER_SIZE];
  25. int nameLen, deviceLen;
  26. UINT baudRate;
  27. LPTSTR temp;
  28. UCHAR stopBits, parity, wordLen;
  29. BOOL cont = TRUE;
  30. TCHAR key;
  31. LPTSTR confirm;
  32. BOOL readRet;
  33. for(index = 0; index < NUMBER_FIELDS; index ++){
  34. message[index] = RetreiveMessageText(MSG_NAME_PROMPT+index);
  35. if (!message[index]) {
  36. for(j=index-1; j>=0;j--){
  37. TCFree(message[j]);
  38. }
  39. _tprintf(_T("Cannot find Resources\n"));
  40. return FALSE;
  41. }
  42. temp = message[index];
  43. while(*temp != _T('%')){
  44. temp++;
  45. }
  46. *temp = (TCHAR) 0;
  47. }
  48. message[index] = RetreiveMessageText(MSG_NAME_PROMPT+index);
  49. if (!message[index]) {
  50. for(j=index-1; j>=0;j--){
  51. TCFree(message[j]);
  52. }
  53. _tprintf(_T("Cannot find Resources\n"));
  54. index ++;
  55. return FALSE;
  56. }
  57. confirm = RetreiveMessageText(MSG_CONFIRM_PROMPT);
  58. if (!confirm) {
  59. for(j=index-1; j>=0;j--){
  60. TCFree(message[j]);
  61. }
  62. _tprintf(_T("Cannot find Resources\n"));
  63. index ++;
  64. return FALSE;
  65. }
  66. confirm[_tcslen(confirm) - 2] = _T('\0');
  67. index = 0;
  68. do{
  69. parity = NO_PARITY;
  70. baudRate = DEFAULT_BAUD_RATE;
  71. stopBits = 0;
  72. wordLen = 8;
  73. nameLen = deviceLen =MAX_BUFFER_SIZE;
  74. name[0] = device[0] = (TCHAR) 0;
  75. retVal = (LONG) (getparams)(index,
  76. name,
  77. &nameLen,
  78. device,
  79. &deviceLen,
  80. &stopBits,
  81. &parity,
  82. &baudRate,
  83. &wordLen
  84. );
  85. if(retVal != ERROR_SUCCESS){
  86. if (retVal == ERROR_NO_MORE_ITEMS){
  87. index --;
  88. if(index < 0 ) {
  89. cont=FALSE;
  90. continue;
  91. }
  92. goto input;
  93. }
  94. else{
  95. _tprintf(_T("%d\n"),retVal);
  96. cont=FALSE;
  97. }
  98. continue;
  99. }
  100. DisplayParameters(message,
  101. name,
  102. device,
  103. baudRate,
  104. wordLen,
  105. parity,
  106. stopBits
  107. );
  108. _tprintf(_T("%s"),message[6]);
  109. input:
  110. readRet = ReadFile(hConsoleInput,
  111. &key,
  112. sizeof(TCHAR),
  113. &len,
  114. NULL
  115. );
  116. if(!readRet || !len){
  117. exit(1);
  118. }
  119. if(lastChar == _T('\r') && key == _T('\n')){
  120. lastChar = key;
  121. goto input;
  122. }
  123. lastChar = key;
  124. switch(key){
  125. case _T('p'):
  126. case _T('P'):
  127. if(index == 0){
  128. goto input;
  129. }
  130. index --;
  131. break;
  132. case _T('n'):
  133. case _T('N'):
  134. index ++;
  135. break;
  136. case _T('m'):
  137. case _T('M'):
  138. cont=FALSE;
  139. break;
  140. case _T('d'):
  141. case _T('D'):
  142. retVal = (LONG) (deletekey)(name);
  143. if(index > 0){
  144. index --;
  145. }
  146. break;
  147. case _T('e'):
  148. case _T('E'):
  149. retVal = DisplayEditMenu(name,
  150. _tcslen(name),
  151. device,
  152. _tcslen(device),
  153. &baudRate,
  154. &wordLen,
  155. &parity,
  156. &stopBits
  157. );
  158. if(retVal == ERROR_SUCCESS){
  159. DisplayParameters(message,
  160. name,
  161. device,
  162. baudRate,
  163. wordLen,
  164. parity,
  165. stopBits
  166. );
  167. _tprintf(_T("%s"),confirm);
  168. do{
  169. readRet = ReadFile(hConsoleInput,
  170. &key,
  171. sizeof(TCHAR),
  172. &len,
  173. NULL
  174. );
  175. if(!readRet || !len){
  176. exit(1);
  177. }
  178. if(lastChar != _T('\r') || key != _T('\n')){
  179. lastChar = key;
  180. break;
  181. }
  182. lastChar = key;
  183. }while(1);
  184. if((key == _T('y'))
  185. || (key == _T('Y'))){
  186. retVal = (LONG) (setparams)(name,
  187. device,
  188. &stopBits,
  189. &parity,
  190. &baudRate,
  191. &wordLen
  192. );
  193. if(retVal != ERROR_SUCCESS){
  194. temp = RetreiveMessageText(MSG_ERROR_SET);
  195. temp[_tcslen(temp) - 2 ] = _T('\0');
  196. if(temp){
  197. _tprintf(_T("%s %d"),temp,retVal);
  198. }
  199. TCFree(temp);
  200. }
  201. }
  202. }
  203. break;
  204. default:
  205. goto input;
  206. }
  207. }while(cont);
  208. for(index = 0; index <= NUMBER_FIELDS; index ++){
  209. TCFree(message[index]);
  210. }
  211. return TRUE;
  212. }
  213. VOID DisplayScreen(
  214. UINT MessageID
  215. )
  216. {
  217. LPTSTR Message;
  218. DWORD len;
  219. Message = RetreiveMessageText(MessageID);
  220. if (!Message) {
  221. _tprintf(_T("Cannot retreive message\n"));
  222. return;
  223. }
  224. _tprintf(_T("%s"),Message);
  225. TCFree(Message);
  226. return;
  227. }
  228. int
  229. DisplayEditMenu(
  230. TCHAR *name,
  231. int nameLen,
  232. TCHAR *device,
  233. int deviceLen,
  234. UINT *BaudRate,
  235. UCHAR *WordLen,
  236. UCHAR *Parity,
  237. UCHAR *StopBits
  238. )
  239. {
  240. LPTSTR message,temp,curr;
  241. int i;
  242. DWORD len;
  243. BOOL ret;
  244. TCHAR buffer[MAX_BUFFER_SIZE];
  245. int dat;
  246. message = RetreiveMessageText(MSG_NAME_PROMPT);
  247. if(! message){
  248. return -1;
  249. }
  250. temp = message;
  251. while(*temp != _T('%')){
  252. temp ++;
  253. }
  254. *temp = (TCHAR) 0;
  255. _tprintf(_T("%s"),message);
  256. TCFree(message);
  257. while(!GetLine(name, nameLen, MAX_BUFFER_SIZE));
  258. message = RetreiveMessageText(MSG_DEVICE_PROMPT);
  259. if(! message){
  260. return -1;
  261. }
  262. temp = message;
  263. while(*temp != _T('%')){
  264. temp ++;
  265. }
  266. *temp = (TCHAR) 0;
  267. _tprintf(_T("%s"),message);
  268. TCFree(message);
  269. while(!GetLine(device, deviceLen, MAX_BUFFER_SIZE));
  270. message = RetreiveMessageText(MSG_BAUD_PROMPT);
  271. if(! message){
  272. return -1;
  273. }
  274. temp = message;
  275. while(*temp != _T('%')){
  276. temp ++;
  277. }
  278. *temp = (TCHAR) 0;
  279. _tprintf(_T("%s"),message);
  280. TCFree(message);
  281. _stprintf(buffer,_T("%d"),*BaudRate);
  282. while(!GetLine(buffer,_tcslen(buffer) , MAX_BUFFER_SIZE));
  283. _stscanf(buffer,_T("%d"),BaudRate);
  284. message = RetreiveMessageText(MSG_WORD_PROMPT);
  285. if(! message){
  286. return -1;
  287. }
  288. temp = message;
  289. while(*temp != _T('%')){
  290. temp ++;
  291. }
  292. *temp = (TCHAR) 0;
  293. _tprintf(_T("%s"),message);
  294. TCFree(message);
  295. _stprintf(buffer,_T("%d"),*WordLen);
  296. while(!GetLine(buffer,_tcslen(buffer) , MAX_BUFFER_SIZE));
  297. _stscanf(buffer,_T("%d"),&dat);
  298. *WordLen = (UCHAR) dat;
  299. message = RetreiveMessageText(MSG_PARITY_PROMPT2);
  300. if(! message){
  301. return -1;
  302. }
  303. temp = curr = message;
  304. while(*temp != _T('%')){
  305. temp ++;
  306. }
  307. *temp = (TCHAR) 0;
  308. _tprintf(_T("%s%d"),curr,NO_PARITY);
  309. curr = &(temp[1]);
  310. while(*temp != _T('%')){
  311. temp ++;
  312. }
  313. *temp = (TCHAR) 0;
  314. _tprintf(_T("%s%d"),curr,ODD_PARITY);
  315. curr = &(temp[1]);
  316. while(*temp != _T('%')){
  317. temp ++;
  318. }
  319. *temp = (TCHAR) 0;
  320. _tprintf(_T("%s%d"),curr,EVEN_PARITY);
  321. curr = &(temp[1]);
  322. while(*temp != _T('%')){
  323. temp ++;
  324. }
  325. *temp = (TCHAR) 0;
  326. _tprintf(_T("%s%d"),curr,MARK_PARITY);
  327. curr = &(temp[1]);
  328. while(*temp != _T('%')){
  329. temp ++;
  330. }
  331. *temp = (TCHAR) 0;
  332. _tprintf(_T("%s%d"),curr,SPACE_PARITY);
  333. curr = &(temp[1]);
  334. while(*temp != _T('%')){
  335. temp ++;
  336. }
  337. *temp = (TCHAR) 0;
  338. _tprintf(_T("%s"),curr);
  339. TCFree(message);
  340. _stprintf(buffer,_T("%d"),*Parity);
  341. while(!GetLine(buffer,_tcslen(buffer) , MAX_BUFFER_SIZE));
  342. _stscanf(buffer,_T("%d"),&dat);
  343. *Parity = (UCHAR) dat;
  344. message = RetreiveMessageText(MSG_STOP_PROMPT2);
  345. if(! message){
  346. return -1;
  347. }
  348. temp = curr = message;
  349. while(*temp != _T('%')){
  350. temp ++;
  351. }
  352. *temp = (TCHAR) 0;
  353. _tprintf(_T("%s%d"),curr,STOP_BIT_1);
  354. curr = &(temp[1]);
  355. while(*temp != _T('%')){
  356. temp ++;
  357. }
  358. *temp = (TCHAR) 0;
  359. _tprintf(_T("%s%d"),curr,STOP_BITS_1_5);
  360. curr = &(temp[1]);
  361. while(*temp != _T('%')){
  362. temp ++;
  363. }
  364. *temp = (TCHAR) 0;
  365. _tprintf(_T("%s%d"),curr,STOP_BITS_2);
  366. curr = &(temp[1]);
  367. while(*temp != _T('%')){
  368. temp ++;
  369. }
  370. *temp = (TCHAR) 0;
  371. _tprintf(_T("%s"),curr);
  372. TCFree(message);
  373. _stprintf(buffer,_T("%d"),*StopBits);
  374. while(!GetLine(buffer,_tcslen(buffer) , MAX_BUFFER_SIZE));
  375. _stscanf(buffer,_T("%d"),&dat);
  376. *StopBits = (UCHAR) dat;
  377. return ERROR_SUCCESS;
  378. }
  379. LPTSTR
  380. RetreiveMessageText(
  381. IN ULONG MessageId
  382. )
  383. {
  384. ULONG LenBytes;
  385. PMESSAGE_RESOURCE_ENTRY MessageEntry;
  386. BOOLEAN IsUnicode;
  387. ANSI_STRING AnsiString;
  388. UNICODE_STRING UnicodeString;
  389. NTSTATUS Status;
  390. LPTSTR MessageText;
  391. Status = RtlFindMessage(
  392. ResourceImageBase,
  393. (ULONG)(ULONG_PTR)RT_MESSAGETABLE,
  394. 0,
  395. MessageId,
  396. &MessageEntry
  397. );
  398. if(!NT_SUCCESS(Status)) {
  399. _tprintf(_T("TCADMIN: Can't find message 0x%lx\n"),MessageId);
  400. return(NULL);
  401. }
  402. IsUnicode = (BOOLEAN)((MessageEntry->Flags &
  403. MESSAGE_RESOURCE_UNICODE) != 0);
  404. //
  405. // Get the size in bytes of a buffer large enough to hold the
  406. // message and its terminating nul wchar. If the message is
  407. // unicode, then this value is equal to the size of the message.
  408. // If the message is not unicode, then we have to calculate this value.
  409. //
  410. if(IsUnicode) {
  411. #ifdef UNICODE
  412. LenBytes = (wcslen((PWSTR)MessageEntry->Text)
  413. + 1)*sizeof(WCHAR);
  414. #else
  415. LenBytes = wcstombs(NULL,(PWSTR)MessageEntry->Text, 0);
  416. #endif
  417. } else {
  418. //
  419. // RtlAnsiStringToUnicodeSize includes an implied wide-nul terminator
  420. // in the count it returns.
  421. //
  422. #ifdef UNICODE
  423. AnsiString.Buffer = MessageEntry->Text;
  424. AnsiString.Length = (USHORT)strlen(MessageEntry->Text);
  425. AnsiString.MaximumLength = AnsiString.Length;
  426. LenBytes = RtlAnsiStringToUnicodeSize(&AnsiString);
  427. #else
  428. LenBytes = strlen((PCHAR) MessageEntry->Text);
  429. #endif
  430. }
  431. LenBytes += sizeof(TCHAR);
  432. //
  433. // allocate a buffer.
  434. //
  435. MessageText = (LPTSTR) TCAlloc(LenBytes);
  436. if(MessageText == NULL) {
  437. return(NULL);
  438. }
  439. memset(MessageText,0,LenBytes);
  440. if(IsUnicode) {
  441. //
  442. // Message is already unicode; just copy it into the buffer.
  443. //
  444. #ifdef UNICODE
  445. wcscpy(MessageText,(PWSTR)MessageEntry->Text);
  446. #else
  447. LenBytes = wcstombs((PCHAR) MessageText,
  448. (PWCHAR) MessageEntry->Text,
  449. LenBytes);
  450. #endif
  451. } else {
  452. //
  453. // Message is not unicode; convert in into the buffer.
  454. //
  455. #ifdef UNICODE
  456. UnicodeString.Buffer = MessageText;
  457. UnicodeString.Length = 0;
  458. UnicodeString.MaximumLength = (USHORT)LenBytes;
  459. RtlAnsiStringToUnicodeString(&UnicodeString,
  460. &AnsiString,
  461. FALSE
  462. );
  463. #else
  464. strcpy((PCHAR) MessageText, (PCHAR) MessageEntry->Text);
  465. #endif
  466. }
  467. return(MessageText);
  468. }
  469. LONG
  470. GetLine(
  471. LPTSTR str,
  472. int index,
  473. int MaxLength
  474. )
  475. {
  476. DWORD len;
  477. DWORD size;
  478. TCHAR buffer[3];
  479. BOOL ret;
  480. str[index] = (TCHAR) 0;
  481. FlushConsoleInputBuffer(hConsoleInput);
  482. _tprintf(_T("%s"),str);
  483. buffer[0] = buffer[2] = (TCHAR) 0x8;
  484. buffer[1] = (TCHAR) 0;
  485. do{
  486. if (index == MaxLength) {
  487. index--;
  488. }
  489. //
  490. // Read a (possibly) partial command line.
  491. //
  492. do{
  493. ret = ReadFile(hConsoleInput,
  494. &(str[index]),
  495. sizeof(TCHAR),
  496. &len,
  497. NULL
  498. );
  499. if(!ret || !len){
  500. exit(1);
  501. }
  502. if(lastChar != _T('\r') || str[index] != _T('\n')){
  503. //ignore \r\n combinations
  504. lastChar = str[index];
  505. break;
  506. }
  507. lastChar = str[index];
  508. }while(1);
  509. lastChar = str[index];
  510. if ((str[index] == (TCHAR) 0x8) || // backspace (^h)
  511. (str[index] == (TCHAR) 0x7F)) { // delete
  512. if (index > 0) {
  513. WriteConsole(hConsoleOutput,
  514. buffer,
  515. 3,
  516. &len,
  517. NULL
  518. );
  519. index--;
  520. }
  521. } else {
  522. WriteConsole(hConsoleOutput,
  523. &(str[index]),
  524. 1,
  525. &len,
  526. NULL
  527. );
  528. index++;
  529. }
  530. } while ((index == 0) || ((str[index - 1] != _T('\n')) &&(str[index-1] != _T('\r'))));
  531. if(str[index-1] == _T('\r')){
  532. buffer[0] = '\n';
  533. str[index - 1] = '\0';
  534. }
  535. else{
  536. buffer[0] = _T('\r');
  537. str[index-1] ='\0';
  538. }
  539. WriteConsole(hConsoleOutput,
  540. buffer,
  541. 1,
  542. &len,
  543. NULL
  544. );
  545. FlushConsoleInputBuffer(hConsoleInput);
  546. return index;
  547. }
  548. VOID
  549. DisplayParameters(
  550. LPCTSTR *message,
  551. LPCTSTR name,
  552. LPCTSTR device,
  553. UINT baudRate,
  554. UCHAR wordLen,
  555. UCHAR parity,
  556. UCHAR stopBits
  557. )
  558. {
  559. _tprintf(_T("%s "),message[0]);
  560. _tprintf(_T("%s\n"),name);
  561. _tprintf(_T("%s "),message[1]);
  562. _tprintf(_T("%s\n"),device);
  563. _tprintf(_T("%s "),message[2]);
  564. _tprintf(_T("%d\n"),baudRate);
  565. _tprintf(_T("%s "),message[3]);
  566. _tprintf(_T("%d\n"),wordLen);
  567. _tprintf(_T("%s "),message[4]);
  568. switch(parity){
  569. case NO_PARITY:
  570. _tprintf(_T("NONE\n"));
  571. break;
  572. case ODD_PARITY:
  573. _tprintf(_T("ODD\n"));
  574. break;
  575. case EVEN_PARITY:
  576. _tprintf(_T("EVEN\n"));
  577. break;
  578. case MARK_PARITY:
  579. _tprintf(_T("MARK\n"));
  580. break;
  581. case SPACE_PARITY:
  582. _tprintf(_T("SPACE\n"));
  583. break;
  584. default:
  585. _tprintf(_T("NONE\n"));
  586. break;
  587. }
  588. _tprintf(_T("%s "),message[5]);
  589. switch(stopBits){
  590. case STOP_BIT_1:
  591. _tprintf(_T("1\n"));
  592. break;
  593. case STOP_BITS_1_5:
  594. _tprintf(_T("1.5\n"));
  595. break;
  596. case STOP_BITS_2:
  597. _tprintf(_T("2\n"));
  598. break;
  599. default:
  600. _tprintf(_T("1\n"));
  601. break;
  602. }
  603. }
  604. VOID SendParameterChange(
  605. )
  606. {
  607. SC_HANDLE sc_handle;
  608. SC_HANDLE tc_handle;
  609. LPTSTR temp;
  610. BOOL ret;
  611. SERVICE_STATUS status;
  612. sc_handle = OpenSCManager(NULL,
  613. NULL,
  614. GENERIC_READ
  615. );
  616. if (sc_handle == NULL){
  617. temp = RetreiveMessageText(CANNOT_OPEN_SERVICE_MANAGER);
  618. if(temp){
  619. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  620. _tprintf(_T("%s"), temp);
  621. return;
  622. }
  623. }
  624. tc_handle = OpenService(sc_handle,
  625. TCSERV_NAME,
  626. SERVICE_ALL_ACCESS
  627. );
  628. if (tc_handle == NULL){
  629. temp = RetreiveMessageText(CANNOT_OPEN_SERVICE);
  630. if(temp){
  631. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  632. _tprintf(_T("%s %d"), temp,GetLastError());
  633. TCFree(temp);
  634. CloseServiceHandle(sc_handle);
  635. return;
  636. }
  637. }
  638. ret = ControlService(tc_handle,
  639. SERVICE_CONTROL_PARAMCHANGE,
  640. &status
  641. );
  642. if(ret == FALSE){
  643. temp = RetreiveMessageText(CANNOT_SEND_PARAMETER_CHANGE);
  644. if(temp){
  645. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  646. _tprintf(_T("%s %d"), temp, GetLastError());
  647. TCFree(temp);
  648. CloseServiceHandle(tc_handle);
  649. CloseServiceHandle(sc_handle);
  650. return;
  651. }
  652. }
  653. temp = RetreiveMessageText(SUCCESSFULLY_SENT_PARAMETER_CHANGE);
  654. if(temp){
  655. _tprintf(_T("%s"), temp);
  656. TCFree(temp);
  657. }
  658. CloseServiceHandle(tc_handle);
  659. CloseServiceHandle(sc_handle);
  660. return;
  661. }
  662. VOID
  663. GetStatus(
  664. )
  665. {
  666. SC_HANDLE sc_handle;
  667. SC_HANDLE tc_handle;
  668. DWORD len;
  669. int i;
  670. LPTSTR temp;
  671. BOOL ret;
  672. SERVICE_STATUS_PROCESS status;
  673. DWORD val[] = {
  674. SERVICE_STOPPED,
  675. SERVICE_START_PENDING,
  676. SERVICE_STOP_PENDING,
  677. SERVICE_RUNNING,
  678. SERVICE_CONTINUE_PENDING,
  679. SERVICE_PAUSE_PENDING,
  680. SERVICE_PAUSED
  681. };
  682. sc_handle = OpenSCManager(NULL,
  683. NULL,
  684. GENERIC_READ
  685. );
  686. if (sc_handle == NULL){
  687. temp = RetreiveMessageText(CANNOT_OPEN_SERVICE_MANAGER);
  688. if(temp){
  689. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  690. _tprintf(_T("%s"), temp);
  691. return;
  692. }
  693. }
  694. tc_handle = OpenService(sc_handle,
  695. TCSERV_NAME,
  696. SERVICE_ALL_ACCESS
  697. );
  698. if (tc_handle == NULL){
  699. temp = RetreiveMessageText(CANNOT_OPEN_SERVICE);
  700. if(temp){
  701. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  702. _tprintf(_T("%s %d"), temp,GetLastError());
  703. TCFree(temp);
  704. CloseServiceHandle(sc_handle);
  705. return;
  706. }
  707. }
  708. ret = QueryServiceStatusEx(tc_handle,
  709. SC_STATUS_PROCESS_INFO,
  710. (LPBYTE) &status,
  711. sizeof(status),
  712. &len
  713. );
  714. if(ret == FALSE){
  715. temp = RetreiveMessageText(CANNOT_QUERY_STATUS);
  716. if(temp){
  717. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  718. _tprintf(_T("%s %d"), temp, GetLastError());
  719. TCFree(temp);
  720. CloseServiceHandle(tc_handle);
  721. CloseServiceHandle(sc_handle);
  722. return;
  723. }
  724. }
  725. temp = RetreiveMessageText(QUERY_STATUS_SUCCESS);
  726. if(temp){
  727. temp[_tcslen(temp) -2 ] = (TCHAR) 0;
  728. _tprintf(_T("%s"), temp);
  729. TCFree(temp);
  730. for(i = 0 ; i<NUMBER_OF_STATES; i++){
  731. if(val[i] == status.dwCurrentState) break;
  732. }
  733. temp = RetreiveMessageText(SERVICE_STOPPED_MESSAGE + i);
  734. if(temp){
  735. _tprintf(_T("%s"),temp);
  736. TCFree(temp);
  737. }
  738. }
  739. CloseServiceHandle(tc_handle);
  740. CloseServiceHandle(sc_handle);
  741. return;
  742. }
  743. VOID StopTCService(
  744. )
  745. {
  746. SC_HANDLE sc_handle;
  747. SC_HANDLE tc_handle;
  748. LPTSTR temp;
  749. BOOL ret;
  750. SERVICE_STATUS status;
  751. sc_handle = OpenSCManager(NULL,
  752. NULL,
  753. GENERIC_READ
  754. );
  755. if (sc_handle == NULL){
  756. temp = RetreiveMessageText(CANNOT_OPEN_SERVICE_MANAGER);
  757. if(temp){
  758. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  759. _tprintf(_T("%s"), temp);
  760. return;
  761. }
  762. }
  763. tc_handle = OpenService(sc_handle,
  764. TCSERV_NAME,
  765. SERVICE_ALL_ACCESS
  766. );
  767. if (tc_handle == NULL){
  768. temp = RetreiveMessageText(CANNOT_OPEN_SERVICE);
  769. if(temp){
  770. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  771. _tprintf(_T("%s %d"), temp,GetLastError());
  772. TCFree(temp);
  773. CloseServiceHandle(sc_handle);
  774. return;
  775. }
  776. }
  777. ret = ControlService(tc_handle,
  778. SERVICE_CONTROL_STOP,
  779. &status
  780. );
  781. if(ret == FALSE){
  782. temp = RetreiveMessageText(CANNOT_SEND_STOP);
  783. if(temp){
  784. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  785. _tprintf(_T("%s %d"), temp, GetLastError());
  786. TCFree(temp);
  787. CloseServiceHandle(tc_handle);
  788. CloseServiceHandle(sc_handle);
  789. return;
  790. }
  791. }
  792. temp = RetreiveMessageText(SUCCESSFULLY_SENT_STOP);
  793. if(temp){
  794. _tprintf(_T("%s"), temp);
  795. TCFree(temp);
  796. }
  797. CloseServiceHandle(tc_handle);
  798. CloseServiceHandle(sc_handle);
  799. return;
  800. }
  801. VOID StartTCService(
  802. )
  803. {
  804. SC_HANDLE sc_handle;
  805. SC_HANDLE tc_handle;
  806. LPTSTR temp;
  807. BOOL ret;
  808. SERVICE_STATUS status;
  809. sc_handle = OpenSCManager(NULL,
  810. NULL,
  811. GENERIC_READ
  812. );
  813. if (sc_handle == NULL){
  814. temp = RetreiveMessageText(CANNOT_OPEN_SERVICE_MANAGER);
  815. if(temp){
  816. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  817. _tprintf(_T("%s"), temp);
  818. return;
  819. }
  820. }
  821. tc_handle = OpenService(sc_handle,
  822. TCSERV_NAME,
  823. SERVICE_ALL_ACCESS
  824. );
  825. if (tc_handle == NULL){
  826. temp = RetreiveMessageText(CANNOT_OPEN_SERVICE);
  827. if(temp){
  828. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  829. _tprintf(_T("%s %d"), temp,GetLastError());
  830. TCFree(temp);
  831. CloseServiceHandle(sc_handle);
  832. return;
  833. }
  834. }
  835. ret = StartService(tc_handle,
  836. 0,
  837. NULL
  838. );
  839. if(ret == FALSE){
  840. temp = RetreiveMessageText(CANNOT_SEND_START);
  841. if(temp){
  842. temp[_tcslen(temp) - 2] = (TCHAR) 0;
  843. _tprintf(_T("%s %d"), temp, GetLastError());
  844. TCFree(temp);
  845. CloseServiceHandle(tc_handle);
  846. CloseServiceHandle(sc_handle);
  847. return;
  848. }
  849. }
  850. temp = RetreiveMessageText(SUCCESSFULLY_SENT_START);
  851. if(temp){
  852. _tprintf(_T("%s"), temp);
  853. TCFree(temp);
  854. }
  855. CloseServiceHandle(tc_handle);
  856. CloseServiceHandle(sc_handle);
  857. return;
  858. }
  859. VOID
  860. AddAllComPorts(
  861. )
  862. /*
  863. * Adds all the Com ports in the system as parameters to bridge
  864. * Reads key HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
  865. * Each value name is a com device name. The friendly name is the
  866. * value data
  867. *
  868. */
  869. {
  870. DWORD index;
  871. HKEY m_hkey;
  872. ULONG retVal;
  873. LPTSTR temp;
  874. TCHAR device[MAX_BUFFER_SIZE*2];
  875. TCHAR name[MAX_BUFFER_SIZE];
  876. UINT baudRate;
  877. DWORD type;
  878. DWORD deviceLen,nameLen;
  879. UCHAR stopBits, parity, wordLen;
  880. index = 0;
  881. retVal = RegOpenKey(HKEY_LOCAL_MACHINE,
  882. SERIAL_DEVICE_KEY,
  883. &m_hkey
  884. );
  885. if(retVal != ERROR_SUCCESS){
  886. _tprintf(_T("%d\n"),retVal);
  887. return;
  888. }
  889. index = 0;
  890. while(1){
  891. deviceLen = nameLen = MAX_BUFFER_SIZE;
  892. retVal = RegEnumValue(m_hkey,
  893. index,
  894. device,
  895. &deviceLen,
  896. NULL,
  897. &type,
  898. name,
  899. &nameLen
  900. );
  901. if(retVal != ERROR_SUCCESS){
  902. if(retVal != ERROR_NO_MORE_ITEMS){
  903. _tprintf(_T("%d\n"),retVal);
  904. }
  905. break;
  906. }
  907. stopBits = STOP_BIT_1;
  908. parity = NO_PARITY;
  909. baudRate = DEFAULT_BAUD_RATE;
  910. wordLen = SERIAL_DATABITS_8;
  911. _stprintf(device, "\\??\\%s",name);
  912. retVal = (ULONG) (setparams)(name,
  913. device,
  914. &stopBits,
  915. &parity,
  916. &baudRate,
  917. &wordLen
  918. );
  919. if(retVal != ERROR_SUCCESS){
  920. temp = RetreiveMessageText(MSG_ERROR_SET);
  921. temp[_tcslen(temp) - 2 ] = _T('\0');
  922. if(temp){
  923. _tprintf(_T("%s %d"),temp,retVal);
  924. }
  925. TCFree(temp);
  926. break;
  927. }
  928. index ++;
  929. }
  930. retVal = RegCloseKey(m_hkey);
  931. }