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.

230 lines
5.1 KiB

  1. #include <windows.h>
  2. #include <faxutil.h>
  3. #include <winfax.h>
  4. #include "faxstat.h"
  5. HANDLE hFax;
  6. LPBYTE StatusBuffer;
  7. HANDLE FaxPortHandle;
  8. VOID
  9. WorkerThread(
  10. PINSTANCE_DATA InstanceData
  11. )
  12. {
  13. PFAX_EVENT FaxEvent;
  14. HANDLE CompletionPort = NULL;
  15. BOOL Rval;
  16. DWORD Bytes;
  17. DWORD CompletionKey;
  18. PFAX_PORT_INFO PortInfo;
  19. DWORD PortCount;
  20. DWORD LastEventId = 0;
  21. DWORD EventId;
  22. PFAX_DEVICE_STATUS FaxDeviceStatus;
  23. while (TRUE) {
  24. if (FaxPortHandle) {
  25. FaxClose( FaxPortHandle );
  26. FaxPortHandle = NULL;
  27. }
  28. if (hFax) {
  29. FaxClose( hFax );
  30. hFax = NULL;
  31. }
  32. if (CompletionPort) {
  33. CloseHandle( CompletionPort );
  34. CompletionPort = NULL;
  35. }
  36. if( !FaxConnectFaxServer( InstanceData->ServerName, &hFax ) ){
  37. hFax = NULL;
  38. PostMessage( InstanceData->hWnd, STATUSUPDATE, FEI_FAXSVC_ENDED, 0 );
  39. goto sleep;
  40. }
  41. CompletionPort = CreateIoCompletionPort(
  42. INVALID_HANDLE_VALUE,
  43. NULL,
  44. 0,
  45. 1
  46. );
  47. if (!CompletionPort) {
  48. goto sleep;
  49. }
  50. if (!FaxInitializeEventQueue( hFax, CompletionPort, 0 )) {
  51. goto sleep;
  52. }
  53. PortInfo = MyFaxEnumPorts( hFax, &PortCount );
  54. if (!PortInfo) {
  55. goto sleep;
  56. }
  57. if (PortCount == 0) {
  58. //
  59. // BUGBUG - should do something more intelligent if there are no ports
  60. //
  61. ExitProcess(0);
  62. }
  63. Rval = FaxOpenPort( hFax, PortInfo[0].DeviceId, PORT_OPEN_EVENTS, &FaxPortHandle );
  64. if (!Rval) {
  65. goto sleep;
  66. }
  67. FaxFreeBuffer( PortInfo );
  68. if (StatusBuffer != NULL) {
  69. FaxFreeBuffer( StatusBuffer );
  70. StatusBuffer = NULL;
  71. }
  72. FaxDeviceStatus = (PFAX_DEVICE_STATUS) StatusBuffer;
  73. Rval = FaxGetDeviceStatus( FaxPortHandle, &FaxDeviceStatus );
  74. if (!Rval) {
  75. goto sleep;
  76. }
  77. EventId = MapStatusIdToEventId( FaxDeviceStatus->Status );
  78. PrintStatus( FaxDeviceStatus );
  79. SendMessage( InstanceData->hWnd, STATUSUPDATE, EventId, (LPARAM) FaxDeviceStatus );
  80. while (TRUE) {
  81. Rval = GetQueuedCompletionStatus(
  82. CompletionPort,
  83. &Bytes,
  84. &CompletionKey,
  85. (LPOVERLAPPED*) &FaxEvent,
  86. INFINITE
  87. );
  88. if (!Rval) {
  89. return;
  90. }
  91. LastEventId = EventId;
  92. EventId = FaxEvent->EventId;
  93. DebugPrint(( TEXT( "Got event %x" ), EventId ));
  94. switch (EventId) {
  95. case FEI_SENDING:
  96. case FEI_RECEIVING:
  97. case FEI_DIALING:
  98. if (EventId != LastEventId) {
  99. if (StatusBuffer != NULL) {
  100. FaxFreeBuffer( StatusBuffer );
  101. StatusBuffer = NULL;
  102. }
  103. FaxDeviceStatus = (PFAX_DEVICE_STATUS) StatusBuffer;
  104. Rval = FaxGetDeviceStatus( FaxPortHandle, &FaxDeviceStatus );
  105. PrintStatus( FaxDeviceStatus );
  106. }
  107. }
  108. SendMessage( InstanceData->hWnd, STATUSUPDATE, EventId, (LPARAM) FaxDeviceStatus );
  109. LocalFree( FaxEvent );
  110. if (EventId == FEI_FAXSVC_ENDED) {
  111. break;
  112. }
  113. }
  114. sleep:
  115. if (EventId != FEI_FAXSVC_ENDED) {
  116. SendMessage( InstanceData->hWnd, STATUSUPDATE, FEI_FAXSVC_ENDED, 0 );
  117. }
  118. Sleep(60000);
  119. }
  120. }
  121. VOID
  122. WorkerThreadInitialize(
  123. PINSTANCE_DATA InstanceData
  124. )
  125. {
  126. HANDLE WorkerThreadHandle;
  127. DWORD WorkerThreadId;
  128. WorkerThreadHandle = CreateThread(
  129. NULL,
  130. 0,
  131. (LPTHREAD_START_ROUTINE) WorkerThread,
  132. InstanceData,
  133. 0,
  134. &WorkerThreadId
  135. );
  136. }
  137. VOID
  138. Disconnect(
  139. VOID
  140. )
  141. {
  142. if (FaxPortHandle) {
  143. FaxClose( FaxPortHandle );
  144. FaxPortHandle = NULL;
  145. }
  146. if (hFax) {
  147. FaxClose( hFax );
  148. hFax = NULL;
  149. }
  150. }
  151. PFAX_PORT_INFO
  152. MyFaxEnumPorts(
  153. HANDLE hFaxSvc,
  154. LPDWORD pcPorts
  155. )
  156. {
  157. PVOID pSvcPorts = NULL;
  158. if (!FaxEnumPorts(hFaxSvc, (PFAX_PORT_INFO*) &pSvcPorts, pcPorts))
  159. {
  160. pSvcPorts = NULL;
  161. }
  162. return pSvcPorts;
  163. }
  164. VOID
  165. PrintStatus(
  166. PFAX_DEVICE_STATUS FaxStatus
  167. )
  168. {
  169. DebugPrint(( TEXT( "Status 0x%x" ), FaxStatus->Status ));
  170. DebugPrint(( TEXT( "Csid %s" ), FaxStatus->Csid ));
  171. DebugPrint(( TEXT( "Tsid %s" ), FaxStatus->Tsid ));
  172. DebugPrint(( TEXT( "PhoneNumber %s" ), FaxStatus->PhoneNumber ));
  173. DebugPrint(( TEXT( "CurrentPage %d" ), FaxStatus->CurrentPage ));
  174. DebugPrint(( TEXT( "TotalPages %d" ), FaxStatus->TotalPages ));
  175. DebugPrint(( TEXT( "--------------------") ));
  176. }