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.

187 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. util.c
  5. Abstract:
  6. This module implements utility functions for the fax service provider
  7. --*/
  8. #include "newfsp.h"
  9. BOOL
  10. OpenLogFile(
  11. BOOL bLoggingEnabled,
  12. LPWSTR lpszLoggingDirectory
  13. )
  14. /*++
  15. Routine Description:
  16. Open the log file
  17. Arguments:
  18. bLoggingEnabled - indicates if logging is enabled
  19. lpszLoggingDirectory - indicates the logging directory
  20. pDeviceInfo - pointer to the virtual fax devices
  21. Return Value:
  22. TRUE on success
  23. --*/
  24. {
  25. // szLoggingFilename is the logging file name
  26. WCHAR szLoggingFilename[MAX_PATH];
  27. // cUnicodeBOM is the Unicode BOM
  28. WCHAR cUnicodeBOM = 0xFEFF;
  29. DWORD dwSize;
  30. if (bLoggingEnabled == TRUE) {
  31. // Set the logging file name
  32. lstrcpy(szLoggingFilename, lpszLoggingDirectory);
  33. lstrcat(szLoggingFilename, L"\\");
  34. lstrcat(szLoggingFilename, NEWFSP_LOG_FILE);
  35. // Create the new log file
  36. g_hLogFile = CreateFile(szLoggingFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  37. if (g_hLogFile == INVALID_HANDLE_VALUE) {
  38. return FALSE;
  39. }
  40. // Write the Unicode BOM to the log file
  41. WriteFile(g_hLogFile, &cUnicodeBOM, sizeof(WCHAR), &dwSize, NULL);
  42. }
  43. else {
  44. g_hLogFile = INVALID_HANDLE_VALUE;
  45. }
  46. return TRUE;
  47. }
  48. VOID
  49. CloseLogFile(
  50. )
  51. /*++
  52. Routine Description:
  53. Close the log file
  54. Return Value:
  55. None
  56. --*/
  57. {
  58. if (g_hLogFile != INVALID_HANDLE_VALUE) {
  59. CloseHandle(g_hLogFile);
  60. g_hLogFile = INVALID_HANDLE_VALUE;
  61. }
  62. }
  63. VOID
  64. WriteDebugString(
  65. LPWSTR lpszFormatString,
  66. ...
  67. )
  68. /*++
  69. Routine Description:
  70. Write a debug string to the debugger and log file
  71. Arguments:
  72. lpszFormatString - pointer to the string
  73. Return Value:
  74. None
  75. --*/
  76. {
  77. va_list varg_ptr;
  78. SYSTEMTIME SystemTime;
  79. // szOutputString is the output string
  80. WCHAR szOutputString[1024];
  81. DWORD cb;
  82. // Initialize the buffer
  83. ZeroMemory(szOutputString, sizeof(szOutputString));
  84. // Get the current time
  85. GetLocalTime(&SystemTime);
  86. wsprintf(szOutputString, L"%02d.%02d.%04d@%02d:%02d:%02d.%03d:\n", SystemTime.wMonth, SystemTime.wDay, SystemTime.wYear, SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds);
  87. cb = lstrlen(szOutputString);
  88. va_start(varg_ptr, lpszFormatString);
  89. _vsnwprintf(&szOutputString[cb], sizeof(szOutputString) - cb, lpszFormatString, varg_ptr);
  90. // Write the string to the debugger
  91. OutputDebugString(szOutputString);
  92. if (g_hLogFile != INVALID_HANDLE_VALUE) {
  93. // Write the string to the log file
  94. WriteFile(g_hLogFile, szOutputString, lstrlen(szOutputString) * sizeof(WCHAR), &cb, NULL);
  95. }
  96. }
  97. VOID
  98. PostJobStatus(
  99. HANDLE CompletionPort,
  100. ULONG_PTR CompletionKey,
  101. DWORD StatusId,
  102. DWORD ErrorCode
  103. )
  104. /*++
  105. Routine Description:
  106. Post a completion packet for a fax service provider fax job status change
  107. Arguments:
  108. CompletionPort - specifies a handle to an I/O completion port
  109. CompletionKey - specifies a completion port key value
  110. StatusId - specifies a fax status code
  111. ErrorCode - specifies one of the Win32 error codes that the fax service provider should use to report an error that occurs
  112. Return Value:
  113. TRUE on success
  114. --*/
  115. {
  116. // pFaxDevStatus is a pointer to the completion packet
  117. PFAX_DEV_STATUS pFaxDevStatus;
  118. // Allocate a block of memory for the completion packet
  119. pFaxDevStatus = MemAllocMacro(sizeof(FAX_DEV_STATUS));
  120. if (pFaxDevStatus != NULL) {
  121. // Set the completion packet's structure size
  122. pFaxDevStatus->SizeOfStruct = sizeof(FAX_DEV_STATUS);
  123. // Copy the completion packet's fax status identifier
  124. pFaxDevStatus->StatusId = StatusId;
  125. // Set the completion packet's string resource identifier to 0
  126. pFaxDevStatus->StringId = 0;
  127. // Set the completion packet's current page number to 0
  128. pFaxDevStatus->PageCount = 0;
  129. // Set the completion packet's remote fax device identifier to NULL
  130. pFaxDevStatus->CSI = NULL;
  131. // Set the completion packet's calling fax device identifier to NULL
  132. pFaxDevStatus->CallerId = NULL;
  133. // Set the completion packet's routing string to NULL
  134. pFaxDevStatus->RoutingInfo = NULL;
  135. // Copy the completion packet's Win32 error code
  136. pFaxDevStatus->ErrorCode = ErrorCode;
  137. // Post the completion packet
  138. PostQueuedCompletionStatus(CompletionPort, sizeof(FAX_DEV_STATUS), CompletionKey, (LPOVERLAPPED) pFaxDevStatus);
  139. }
  140. }