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.

209 lines
5.0 KiB

  1. // This is special in that it trys to cause an interval timeout.
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include "windows.h"
  7. #define BIGWRITE 256000
  8. unsigned char writebuff[BIGWRITE];
  9. void main(int argc,char *argv[]) {
  10. HANDLE hFile;
  11. DCB MyDcb;
  12. char *MyPort = "COM1";
  13. DWORD NumberActuallyWritten;
  14. DWORD NumberToWrite = 0;
  15. DWORD UseBaud = 19200;
  16. COMMTIMEOUTS To;
  17. if (argc > 1) {
  18. sscanf(argv[1],"%d",&NumberToWrite);
  19. if (argc > 2) {
  20. sscanf(argv[2],"%d",&UseBaud);
  21. if (argc > 3) {
  22. MyPort = argv[3];
  23. }
  24. }
  25. }
  26. if (((NumberToWrite / 2)*2) != NumberToWrite) {
  27. printf("Number to write must be even!\n");
  28. exit(1);
  29. }
  30. printf("Will try to write %d characters.\n",NumberToWrite);
  31. printf("Will try to write at %d baud.\n",UseBaud);
  32. printf("Using port: %s\n",MyPort);
  33. if ((hFile = CreateFile(
  34. MyPort,
  35. GENERIC_READ | GENERIC_WRITE,
  36. 0,
  37. NULL,
  38. CREATE_ALWAYS,
  39. FILE_ATTRIBUTE_NORMAL,
  40. NULL
  41. )) != ((HANDLE)-1)) {
  42. printf("We successfully opened the %s port.\n",MyPort);
  43. //
  44. // We've successfully opened the file. Set the state of
  45. // the comm device. First we get the old values and
  46. // adjust to our own.
  47. //
  48. if (!GetCommState(
  49. hFile,
  50. &MyDcb
  51. )) {
  52. printf("Couldn't get the comm state: %d\n",GetLastError());
  53. exit(1);
  54. }
  55. if (!GetCommTimeouts(
  56. hFile,
  57. &To
  58. )) {
  59. printf("Couldn't get the timeouts: %d\n",GetLastError());
  60. exit(1);
  61. }
  62. To.ReadIntervalTimeout = 0;
  63. To.ReadTotalTimeoutMultiplier = 0;
  64. To.ReadTotalTimeoutConstant = 0;
  65. To.WriteTotalTimeoutMultiplier = 0;
  66. To.WriteTotalTimeoutConstant = 0;
  67. if (!SetCommTimeouts(
  68. hFile,
  69. &To
  70. )) {
  71. printf("Couldn't set the timeouts: %d\n",GetLastError());
  72. exit(1);
  73. }
  74. MyDcb.BaudRate = UseBaud;
  75. MyDcb.ByteSize = 8;
  76. MyDcb.Parity = NOPARITY;
  77. MyDcb.StopBits = ONESTOPBIT;
  78. if (SetCommState(
  79. hFile,
  80. &MyDcb
  81. )) {
  82. unsigned char j;
  83. DWORD TotalCount;
  84. printf("We successfully set the state of the %s port.\n",MyPort);
  85. for (
  86. TotalCount = 0;
  87. TotalCount < NumberToWrite;
  88. ) {
  89. for (
  90. j = 0;
  91. j <= 9;
  92. j++
  93. ) {
  94. writebuff[TotalCount] = j;
  95. TotalCount++;
  96. if (TotalCount >= NumberToWrite) {
  97. break;
  98. }
  99. }
  100. }
  101. if (!WriteFile(
  102. hFile,
  103. writebuff,
  104. NumberToWrite/2,
  105. &NumberActuallyWritten,
  106. NULL
  107. )) {
  108. DWORD LastError;
  109. LastError = GetLastError();
  110. printf("Couldn't write the %s device.\n",MyPort);
  111. printf("Status of failed write is: %x\n",LastError);
  112. } else if (NumberActuallyWritten != (NumberToWrite/2)) {
  113. printf("Didn't write out the correct number: %d\n",NumberActuallyWritten);
  114. }
  115. //
  116. // 15 seconds till the next write.
  117. //
  118. Sleep(15000);
  119. if (!WriteFile(
  120. hFile,
  121. &writebuff[(NumberToWrite/2)],
  122. NumberToWrite/2,
  123. &NumberActuallyWritten,
  124. NULL
  125. )) {
  126. DWORD LastError;
  127. LastError = GetLastError();
  128. printf("Couldn't write the %s device.\n",MyPort);
  129. printf("Status of failed write is: %x\n",LastError);
  130. } else if (NumberActuallyWritten != (NumberToWrite/2)) {
  131. printf("Didn't write out the correct number: %d\n",NumberActuallyWritten);
  132. }
  133. } else {
  134. DWORD LastError;
  135. LastError = GetLastError();
  136. printf("Couldn't set the %s device.\n",MyPort);
  137. printf("Status of failed set is: %x\n",LastError);
  138. }
  139. CloseHandle(hFile);
  140. } else {
  141. DWORD LastError;
  142. LastError = GetLastError();
  143. printf("Couldn't open the %s device.\n",MyPort);
  144. printf("Status of failed open is: %x\n",LastError);
  145. }
  146. }