Source code of Windows XP (NT5)
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.

318 lines
6.8 KiB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "windows.h"
  5. unsigned char writebuff[10][10000];
  6. OVERLAPPED WriteOl[10];
  7. HANDLE hFile;
  8. HANDLE StartTrailingWriteEvent;
  9. HANDLE EndedTrailingWrites;
  10. DWORD
  11. QueueOtherWrites(
  12. LPVOID Trash
  13. )
  14. {
  15. DWORD k;
  16. UNREFERENCED_PARAMETER(Trash);
  17. WaitForSingleObject(StartTrailingWriteEvent,-1);
  18. printf("Wait Satisfied Sleeping for 3 seconds\n");
  19. Sleep(3000);
  20. for (
  21. k = 5;
  22. k <= 9;
  23. k++
  24. ) {
  25. DWORD NumberWritten;
  26. BOOL WriteDone;
  27. WriteDone = WriteFile(
  28. hFile,
  29. writebuff[k],
  30. 10000,
  31. &NumberWritten,
  32. &WriteOl[k]
  33. );
  34. if (!WriteDone) {
  35. DWORD LastError;
  36. LastError = GetLastError();
  37. if (LastError != ERROR_IO_PENDING) {
  38. printf("Status of failed write %d is: %d\n",k,LastError);
  39. }
  40. }
  41. }
  42. printf("Trailing Writes are all queued thread waits until all are done\n");
  43. printf("Thread exiting so all trailing writes should cancel\n");
  44. return 1;
  45. }
  46. void main(int argc,char *argv[]) {
  47. DCB MyDcb;
  48. char *MyPort = "COM1";
  49. DWORD j;
  50. DWORD ThreadId;
  51. if (argc > 1) {
  52. MyPort = argv[1];
  53. }
  54. for (
  55. j = 0;
  56. j <= 9;
  57. j++
  58. ) {
  59. if (!(WriteOl[j].hEvent = CreateEvent(
  60. NULL,
  61. FALSE,
  62. FALSE,
  63. NULL
  64. ))) {
  65. printf("Could not create the write event %d.\n",j);
  66. } else {
  67. WriteOl[j].Internal = 0;
  68. WriteOl[j].InternalHigh = 0;
  69. WriteOl[j].Offset = 0;
  70. WriteOl[j].OffsetHigh = 0;
  71. }
  72. }
  73. if (!(StartTrailingWriteEvent = CreateEvent(
  74. NULL,
  75. TRUE,
  76. FALSE,
  77. NULL
  78. ))) {
  79. printf("Could not create trailing write event\n");
  80. goto cleanup;
  81. }
  82. if (!(EndedTrailingWrites = CreateEvent(
  83. NULL,
  84. TRUE,
  85. FALSE,
  86. NULL
  87. ))) {
  88. printf("Could not create ending trailing write event\n");
  89. goto cleanup;
  90. }
  91. if ((hFile = CreateFile(
  92. MyPort,
  93. GENERIC_READ | GENERIC_WRITE,
  94. 0,
  95. NULL,
  96. CREATE_ALWAYS,
  97. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  98. NULL
  99. )) != ((HANDLE)-1)) {
  100. printf("We successfully opened the %s port.\n",MyPort);
  101. //
  102. // Create the thread that will wait for the
  103. // trailing write event before it queues its reads.
  104. //
  105. if (!CreateThread(
  106. NULL,
  107. 0,
  108. QueueOtherWrites,
  109. NULL,
  110. 0,
  111. &ThreadId
  112. )) {
  113. printf("Could not create the second thread.\n");
  114. goto cleanup;
  115. }
  116. //
  117. // We've successfully opened the file. Set the state of
  118. // the comm device. First we get the old values and
  119. // adjust to our own.
  120. //
  121. if (!GetCommState(
  122. hFile,
  123. &MyDcb
  124. )) {
  125. printf("Couldn't get the comm state: %d\n",GetLastError());
  126. exit(1);
  127. }
  128. MyDcb.BaudRate = 19200;
  129. MyDcb.ByteSize = 8;
  130. MyDcb.Parity = NOPARITY;
  131. MyDcb.StopBits = ONESTOPBIT;
  132. if (SetCommState(
  133. hFile,
  134. &MyDcb
  135. )) {
  136. DWORD k;
  137. for (
  138. k = 0;
  139. k <= 4;
  140. k++
  141. ) {
  142. DWORD NumberWritten;
  143. BOOL WriteDone;
  144. WriteDone = WriteFile(
  145. hFile,
  146. writebuff[k],
  147. 10000,
  148. &NumberWritten,
  149. &WriteOl[k]
  150. );
  151. if (!WriteDone) {
  152. DWORD LastError;
  153. LastError = GetLastError();
  154. if (LastError != ERROR_IO_PENDING) {
  155. printf("Status of failed write %d is: %d\n",k,LastError);
  156. goto cleanup;
  157. }
  158. }
  159. }
  160. SetEvent(StartTrailingWriteEvent);
  161. if (!FlushFileBuffers(hFile)) {
  162. printf("Couldn't flush %d\n",GetLastError());
  163. } else {
  164. printf("Flushed\n");
  165. }
  166. //
  167. // Insure that the first five writes are done.
  168. //
  169. for (
  170. k = 0;
  171. k <= 4;
  172. k++
  173. ) {
  174. DWORD NumberTransferred;
  175. if (!GetOverlappedResult(
  176. hFile,
  177. &WriteOl[k],
  178. &NumberTransferred,
  179. FALSE
  180. )) {
  181. printf("Write %d was not complete!!\n",k);
  182. } else {
  183. if (NumberTransferred != 10000) {
  184. printf("Write %d transferred only %d bytes\n",k,NumberTransferred);
  185. }
  186. }
  187. }
  188. //
  189. // Wait for the last writes to complete.
  190. //
  191. for (
  192. k = 9;
  193. k >= 5;
  194. k--
  195. ) {
  196. DWORD NumberTransferred;
  197. if (!GetOverlappedResult(
  198. hFile,
  199. &WriteOl[k],
  200. &NumberTransferred,
  201. TRUE
  202. )) {
  203. printf("Bad status from write %d status is %d\n",k,GetLastError());
  204. } else {
  205. if (NumberTransferred != 10000) {
  206. printf("Write %d transferred only %d bytes\n",k,NumberTransferred);
  207. }
  208. }
  209. }
  210. SetEvent(EndedTrailingWrites);
  211. } else {
  212. printf("Couldn't set the comm state\n");
  213. }
  214. } else {
  215. printf("Couldn't open the comm port\n");
  216. }
  217. cleanup:;
  218. }