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.

371 lines
8.8 KiB

  1. #include "setuplog.h"
  2. #include <wtypes.h> // to define HRESULT for richedit.h
  3. #include <richedit.h>
  4. #include <malloc.h>
  5. #include <assert.h>
  6. #pragma hdrstop
  7. SETUPLOG_CONTEXT SetuplogContext;
  8. PSETUPLOG_CONTEXT Context;
  9. LPCTSTR pszAppName = TEXT("ViewLog"); // class name
  10. HANDLE hModule; // handle of this instance
  11. HANDLE hRichedDLL; // DLL used for rich edit
  12. HANDLE hWndMain; // handle to main window
  13. VOID
  14. ReportError (
  15. IN LogSeverity Severity,
  16. ...
  17. );
  18. static PVOID
  19. pOpenFileCallback(
  20. IN LPCTSTR Filename,
  21. IN BOOL WipeLogFile
  22. )
  23. {
  24. WCHAR CompleteFilename[MAX_PATH];
  25. HANDLE hFile;
  26. //
  27. // Form the pathname of the logfile.
  28. //
  29. GetWindowsDirectory(CompleteFilename,MAX_PATH);
  30. ConcatenatePaths(CompleteFilename,Filename,MAX_PATH,NULL);
  31. //
  32. // If we're wiping the logfile clean, attempt to delete
  33. // what's there.
  34. //
  35. if(WipeLogFile) {
  36. SetFileAttributes(CompleteFilename,FILE_ATTRIBUTE_NORMAL);
  37. DeleteFile(CompleteFilename);
  38. }
  39. //
  40. // Open existing file or create a new one.
  41. //
  42. hFile = CreateFile(
  43. CompleteFilename,
  44. GENERIC_READ | GENERIC_WRITE,
  45. FILE_SHARE_READ | FILE_SHARE_WRITE,
  46. NULL,
  47. OPEN_ALWAYS,
  48. FILE_ATTRIBUTE_NORMAL,
  49. NULL
  50. );
  51. return (PVOID)hFile;
  52. }
  53. static BOOL
  54. pWriteFile (
  55. IN PVOID LogFile,
  56. IN LPCTSTR Buffer
  57. )
  58. {
  59. PCSTR AnsiBuffer;
  60. BOOL Status;
  61. DWORD BytesWritten;
  62. if(AnsiBuffer = UnicodeToAnsi (Buffer)) {
  63. Status = WriteFile (
  64. LogFile,
  65. AnsiBuffer,
  66. lstrlenA(AnsiBuffer),
  67. &BytesWritten,
  68. NULL
  69. );
  70. MyFree (AnsiBuffer);
  71. } else {
  72. Status = FALSE;
  73. }
  74. return Status;
  75. }
  76. VOID
  77. InitializeSetupLog(
  78. IN PSETUPLOG_CONTEXT Context
  79. )
  80. /*++
  81. Routine Description:
  82. Initialize the setup action log. This file is a textual description
  83. of actions performed during setup.
  84. The log file is called setuplog.txt and it exists in the windows dir.
  85. Arguments:
  86. WipeLogFile - if TRUE, any existing log file is deleted before logging
  87. begins.
  88. Return Value:
  89. Boolean value indicating whether initialization was sucessful.
  90. --*/
  91. {
  92. UINT i;
  93. Context->OpenFile = pOpenFileCallback;
  94. Context->CloseFile = CloseHandle;
  95. Context->AllocMem = malloc;
  96. Context->FreeMem = free;
  97. Context->Format = RetrieveAndFormatMessageV;
  98. Context->Write = pWriteFile;
  99. //
  100. // Initialize the log severity descriptions.
  101. //
  102. Context->SeverityDescriptions[0] = TEXT("Information");
  103. Context->SeverityDescriptions[1] = TEXT("Warning");
  104. Context->SeverityDescriptions[2] = TEXT("Error");
  105. Context->SeverityDescriptions[3] = TEXT("Fatal Error");
  106. SetuplogInitialize (Context, TRUE);
  107. SetuplogError(
  108. LogSevInformation,
  109. TEXT("This is the beginning of the Setup Log.\r\n"),
  110. 0,0,0);
  111. }
  112. static VOID
  113. CreateLog (
  114. )
  115. {
  116. #if 0
  117. InitializeSetupActionLog(TRUE);
  118. LogItem(LogSevInformation, L"STO test: Information\r\n");
  119. LogItem(LogSevWarning, L"STO test: Warning\r\n");
  120. LogItem(LogSevError, L"STO test: Error\r\n");
  121. LogItem(LogSevFatalError, L"STO test: Fatal Error\r\n");
  122. LogItem0(LogSevError,MSG_LOG_INF_CORRUPT,L"INF File Name");
  123. LogItem1(LogSevWarning,MSG_LOG_PRINTUPG_FAILED,MSG_LOG_X_RETURNED_WINERR,
  124. L"GetPrinterDriverDirectory",(DWORD)123);
  125. LogItem2(
  126. LogSevWarning,
  127. MSG_LOG_CREATESVC_FAIL,
  128. L"Service Name",
  129. MSG_LOG_X_RETURNED_WINERR,
  130. L"OpenSCManager",
  131. (DWORD) 123
  132. );
  133. LogItem3(
  134. LogSevError,
  135. MSG_LOG_SAVEHIVE_FAIL,
  136. L"Subkey",
  137. L"Filename",
  138. MSG_LOG_X_RETURNED_WINERR,
  139. L"RegSaveKey",
  140. (LONG) 123
  141. );
  142. ReportError (LogSevError,MSG_LOG_INF_CORRUPT,L"INF File Name",0,0);
  143. ReportError (LogSevWarning,MSG_LOG_PRINTUPG_FAILED,0,
  144. MSG_LOG_X_RETURNED_WINERR,
  145. L"GetPrinterDriverDirectory",(DWORD)123,0,0);
  146. ReportError (
  147. LogSevWarning,
  148. MSG_LOG_CREATESVC_FAIL,
  149. L"Service Name",0,
  150. MSG_LOG_X_RETURNED_WINERR,
  151. L"OpenSCManager",
  152. (DWORD) 123,0,0
  153. );
  154. ReportError (
  155. LogSevError,
  156. MSG_LOG_SAVEHIVE_FAIL,
  157. L"Subkey",
  158. L"Filename",0,
  159. MSG_LOG_X_RETURNED_WINERR,
  160. L"RegSaveKey",
  161. (LONG) 123,0,0
  162. );
  163. ReportError (LogSevError, MSG_LOG_OPTIONAL_COMPONENT_ERROR, 0,
  164. ERROR_NOT_ENOUGH_MEMORY, 0,0);
  165. ReportError (LogSevError, MSG_LOG_CANT_OPEN_INF,
  166. L"optional.inf", 0,0);
  167. ReportError (LogSevError, MSG_LOG_BAD_SECTION, L"SectionName",
  168. L"InfFileName", 0, 5, 0,0);
  169. ReportError (LogSevError, MSG_LOG_OC_REGISTRY_ERROR, L"RegKeyName", 0,
  170. 5, 0,0);
  171. TerminateSetupActionLog();
  172. #endif
  173. Context = &SetuplogContext;
  174. InitializeSetupLog (Context);
  175. #if 0 // MSG_* not defined
  176. SetuplogError(
  177. LogSevError,
  178. SETUPLOG_USE_MESSAGEID,
  179. MSG_LOG_CANT_OPEN_INF,
  180. L"optional.inf",
  181. 0,0);
  182. ReportError(
  183. LogSevError,
  184. SETUPLOG_USE_MESSAGEID,
  185. MSG_LOG_OC_REGISTRY_ERROR,
  186. L"RegKeyName", 0,
  187. SETUPLOG_USE_MESSAGEID,
  188. 5, 0,0);
  189. #endif
  190. SetuplogError(
  191. LogSevInformation,
  192. L"This is a string info message.\r\n",
  193. 0,0,0);
  194. SetuplogError(
  195. LogSevWarning,
  196. L"This is a string warning message.\r\n",
  197. 0,0,0);
  198. ReportError(
  199. LogSevError,
  200. L"This is a reported string error message.\r\n", 0,0,0);
  201. ReportError(
  202. LogSevError,
  203. L"This is a reported string error message %1.\r\n", 0,0,
  204. L"with an embedded message",0,0,0);
  205. ReportError(
  206. LogSevError,
  207. L"This is a reported string error message %1.\r\n", 0,0,
  208. L"with an embedded message %1", 0,0,
  209. L"in an embedded message",0,0,0);
  210. SetuplogTerminate();
  211. }
  212. LONG
  213. MainWndProc (
  214. IN HWND hwnd,
  215. IN UINT message,
  216. IN WPARAM wParam,
  217. IN LPARAM lParam
  218. )
  219. {
  220. HDC hdc;
  221. PAINTSTRUCT ps;
  222. RECT rect;
  223. switch (message) {
  224. case WM_CREATE:
  225. #if 0
  226. ViewSetupActionLog (hwnd, NULL, NULL);
  227. ViewSetupActionLog (hwnd, L"d:\\WinNt40\\SetUpLog.TXT", L"My Heading");
  228. ViewSetupActionLog (hwnd, L"MyFile", NULL);
  229. #endif
  230. PostQuitMessage (0);
  231. break;
  232. case WM_PAINT:
  233. hdc = BeginPaint (hwnd, &ps);
  234. GetClientRect (hwnd, &rect);
  235. DrawText (hdc, L"Hello, Windows!", -1, &rect,
  236. DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  237. EndPaint (hwnd, &ps);
  238. break;
  239. case WM_DESTROY:
  240. PostQuitMessage (0);
  241. break;
  242. default:
  243. return DefWindowProc (hwnd, message, wParam, lParam);
  244. }
  245. return 0;
  246. }
  247. static BOOL
  248. InitMainWindow (
  249. )
  250. {
  251. WNDCLASS wc;
  252. //
  253. // Initialize the window class.
  254. //
  255. hModule = GetModuleHandle (NULL);
  256. if (TRUE || FindWindow (pszAppName, NULL) == NULL) {
  257. wc.style = CS_HREDRAW | CS_VREDRAW;
  258. wc.lpfnWndProc = MainWndProc;
  259. wc.cbClsExtra = 0;
  260. wc.cbWndExtra = 0;
  261. wc.hInstance = (HINSTANCE) hModule;
  262. wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  263. wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  264. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  265. wc.lpszMenuName = pszAppName;
  266. wc.lpszClassName = pszAppName;
  267. if (!RegisterClass (&wc)) {
  268. return FALSE;
  269. }
  270. }
  271. //
  272. // Create the window and display it.
  273. //
  274. hWndMain = CreateWindow (
  275. pszAppName,
  276. L"The Hello Program",
  277. WS_OVERLAPPEDWINDOW,
  278. CW_USEDEFAULT, 0,
  279. CW_USEDEFAULT, 0,
  280. NULL, NULL,
  281. (HINSTANCE) hModule,
  282. NULL
  283. );
  284. if (!hWndMain) {
  285. return FALSE;
  286. }
  287. ShowWindow (hWndMain, SW_SHOWNORMAL);
  288. UpdateWindow (hWndMain);
  289. return TRUE;
  290. }
  291. INT WINAPI WinMain(
  292. HINSTANCE hInstance,
  293. HINSTANCE hPrevInstance,
  294. LPSTR lpCmdLine,
  295. INT nCmdShow)
  296. {
  297. MSG msg;
  298. CreateLog ();
  299. // Initialize everything
  300. //
  301. if (!InitMainWindow ()) {
  302. assert(FALSE);
  303. return FALSE;
  304. }
  305. // Process messages
  306. //
  307. while (GetMessage (&msg, NULL, 0, 0)) {
  308. TranslateMessage (&msg);
  309. DispatchMessage (&msg);
  310. }
  311. return (msg.wParam);
  312. }