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.

188 lines
5.7 KiB

  1. // ----------------------------------------------------------------------------
  2. // LogWatch.cpp
  3. // ----------------------------------------------------------------------------
  4. #include <pch.hxx>
  5. #include <richedit.h>
  6. #include "resource.h"
  7. // ------------------------------------------------------------------------------------
  8. // Prototypes
  9. // ------------------------------------------------------------------------------------
  10. INT_PTR CALLBACK LogWatchDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  11. // ----------------------------------------------------------------------------
  12. // WinMain
  13. // ----------------------------------------------------------------------------
  14. int _stdcall ModuleEntry(void)
  15. {
  16. // Locals
  17. LPTSTR pszCmdLine;
  18. // Load RichEdit
  19. HINSTANCE hRichEdit = LoadLibrary("RICHED32.DLL");
  20. // Get the commandline
  21. pszCmdLine = GetCommandLine();
  22. // Fixup Command line
  23. if ( *pszCmdLine == TEXT('\"') ) {
  24. /*
  25. * Scan, and skip over, subsequent characters until
  26. * another double-quote or a null is encountered.
  27. */
  28. while ( *++pszCmdLine && (*pszCmdLine
  29. != TEXT('\"')) );
  30. /*
  31. * If we stopped on a double-quote (usual case), skip
  32. * over it.
  33. */
  34. if ( *pszCmdLine == TEXT('\"') )
  35. pszCmdLine++;
  36. }
  37. else {
  38. while (*pszCmdLine > TEXT(' '))
  39. pszCmdLine++;
  40. }
  41. /*
  42. * Skip past any white space preceeding the second token.
  43. */
  44. while (*pszCmdLine && (*pszCmdLine <= TEXT(' '))) {
  45. pszCmdLine++;
  46. }
  47. // Launch the Dialog
  48. DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_LOGWATCH), NULL, LogWatchDlgProc, (LPARAM)pszCmdLine);
  49. // Free RichEdit
  50. if (hRichEdit)
  51. FreeLibrary(hRichEdit);
  52. // Done
  53. return 1;
  54. }
  55. // ------------------------------------------------------------------------------------
  56. // LogWatchDlgProc
  57. // ------------------------------------------------------------------------------------
  58. INT_PTR CALLBACK LogWatchDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  59. {
  60. // Locals
  61. LPSTR pszFilePath;
  62. ULONG cbRead;
  63. BYTE rgb[4096];
  64. static HANDLE s_hFile=INVALID_HANDLE_VALUE;
  65. static HWND s_hwndEdit=NULL;
  66. // Handle Message
  67. switch (uMsg)
  68. {
  69. case WM_INITDIALOG:
  70. // Get the file
  71. pszFilePath = (LPSTR)lParam;
  72. // No File
  73. if (NULL == pszFilePath || '\0' == *pszFilePath)
  74. {
  75. MessageBox(hwnd, "You must specify a file name on the command line.\r\n\r\nFor example: LogWatch.exe c:\\test.log", "Microsoft LogWatch", MB_OK | MB_ICONSTOP);
  76. EndDialog(hwnd, IDOK);
  77. return FALSE;
  78. }
  79. // Open the file
  80. s_hFile = CreateFile(pszFilePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  81. // Failure
  82. if (INVALID_HANDLE_VALUE == s_hFile)
  83. {
  84. wsprintf((LPSTR)rgb, "The file '%s' could not be opened by LogWatch. The file does not exist or is in use by another application.", pszFilePath);
  85. MessageBox(NULL, (LPSTR)rgb, "Microsoft LogWatch", MB_OK | MB_ICONSTOP);
  86. EndDialog(hwnd, IDOK);
  87. return FALSE;
  88. }
  89. // Set Title Batr
  90. wsprintf((LPSTR)rgb, "Microsoft LogWatch - %s", pszFilePath);
  91. SetWindowText(hwnd, (LPSTR)rgb);
  92. // Seek to the end of the file - 256 bytes
  93. SetFilePointer(s_hFile, (256 > GetFileSize(s_hFile, NULL)) ? 0 : - 256, NULL, FILE_END);
  94. // Create the RichEdit Control
  95. s_hwndEdit = GetDlgItem(hwnd, IDC_EDIT);
  96. // Read a Buffer
  97. ReadFile(s_hFile, rgb, sizeof(rgb) - 1, &cbRead, NULL);
  98. // Hide Selection
  99. SendMessage(s_hwndEdit, EM_HIDESELECTION , TRUE, TRUE);
  100. // Done
  101. if (cbRead)
  102. {
  103. // Append to end of text
  104. rgb[cbRead] = '\0';
  105. LPSTR psz = (LPSTR)rgb;
  106. while(*psz && '\n' != *psz)
  107. psz++;
  108. SendMessage(s_hwndEdit, EM_SETSEL, (WPARAM)-1, (LPARAM)-1);
  109. SendMessage(s_hwndEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)psz);
  110. SendMessage(s_hwndEdit, EM_SCROLLCARET, 0, 0);
  111. }
  112. // Kick off the time
  113. SetTimer(hwnd, WM_USER + 1024, 2000, NULL);
  114. SetFocus(s_hwndEdit);
  115. // Done
  116. return FALSE;
  117. case WM_TIMER:
  118. if (wParam == (WM_USER + 1024))
  119. {
  120. // Read to end
  121. while(1)
  122. {
  123. // Read a Buffer
  124. ReadFile(s_hFile, rgb, sizeof(rgb) - 1, &cbRead, NULL);
  125. // Done
  126. if (!cbRead)
  127. break;
  128. // Append to end of text
  129. rgb[cbRead] = '\0';
  130. SendMessage(s_hwndEdit, EM_SETSEL, (WPARAM)-1, (LPARAM)-1);
  131. SendMessage(s_hwndEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)rgb);
  132. SendMessage(s_hwndEdit, EM_SCROLLCARET, 0, 0);
  133. }
  134. }
  135. break;
  136. case WM_SIZE:
  137. SetWindowPos(s_hwndEdit,0,0,0, LOWORD(lParam), HIWORD(lParam),SWP_NOACTIVATE|SWP_NOZORDER);
  138. break;
  139. #if 0
  140. case WM_COMMAND:
  141. switch(GET_WM_COMMAND_ID(wParam,lParam))
  142. {
  143. }
  144. break;
  145. #endif
  146. case WM_CLOSE:
  147. KillTimer(hwnd, WM_USER + 1024);
  148. EndDialog(hwnd, IDOK);
  149. break;
  150. case WM_DESTROY:
  151. if (INVALID_HANDLE_VALUE != s_hFile)
  152. CloseHandle(s_hFile);
  153. return FALSE;
  154. }
  155. // Done
  156. return FALSE;
  157. }