2 * ReactOS Win32 Applications
3 * Copyright (C) 2007 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * COPYRIGHT : See COPYING in the top level directory
21 * PROJECT : Event Log Viewer
23 * PROGRAMMER: Marc Piulachs (marc.piulachs at codexchange [dot] net)
38 #pragma warning(disable: 4996) /* 'strdup' was declared deprecated */
39 #define _CRT_SECURE_NO_DEPRECATE /* all deprecated unsafe string functions */
42 static const LPWSTR EVENT_SOURCE_APPLICATION
= L
"Application";
43 static const LPWSTR EVENT_SOURCE_SECURITY
= L
"Security";
44 static const LPWSTR EVENT_SOURCE_SYSTEM
= L
"System";
45 static const WCHAR szWindowClass
[] = L
"EVENTVWR"; /* the main window class name*/
47 //MessageFile message buffer size
48 #define EVENT_MESSAGE_EVENTTEXT_BUFFER 1024*10
49 #define EVENT_MESSAGE_FILE_BUFFER 1024*10
50 #define EVENT_DLL_SEPARATOR L";"
51 #define EVENT_MESSAGE_FILE L"EventMessageFile"
52 #define EVENT_CATEGORY_MESSAGE_FILE L"CategoryMessageFile"
53 #define EVENT_PARAMETER_MESSAGE_FILE L"ParameterMessageFile"
55 #define MAX_LOADSTRING 255
58 HINSTANCE hInst
; /* current instance */
59 WCHAR szTitle
[MAX_LOADSTRING
]; /* The title bar text */
60 WCHAR szTitleTemplate
[MAX_LOADSTRING
]; /* The logged-on title bar text */
61 HWND hwndMainWindow
; /* Main window */
62 HWND hwndListView
; /* ListView control */
63 HWND hwndStatus
; /* Status bar */
64 WCHAR szStatusBarTemplate
[MAX_LOADSTRING
]; /* The status bar text */
65 PEVENTLOGRECORD
*g_RecordPtrs
= NULL
;
66 DWORD g_TotalRecords
= 0;
68 LPWSTR lpSourceLogName
= NULL
;
69 LPWSTR lpComputerName
= NULL
;
71 /* Forward declarations of functions included in this code module: */
72 ATOM
MyRegisterClass(HINSTANCE hInstance
);
73 BOOL
InitInstance(HINSTANCE
, int);
74 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
75 INT_PTR CALLBACK
About(HWND
, UINT
, WPARAM
, LPARAM
);
76 INT_PTR CALLBACK
EventDetails(HWND
, UINT
, WPARAM
, LPARAM
);
77 static INT_PTR CALLBACK
StatusMessageWindowProc (HWND
, UINT
, WPARAM
, LPARAM
);
81 wWinMain(HINSTANCE hInstance
,
82 HINSTANCE hPrevInstance
,
88 INITCOMMONCONTROLSEX iccx
;
90 UNREFERENCED_PARAMETER(hPrevInstance
);
91 UNREFERENCED_PARAMETER(lpCmdLine
);
93 /* Whenever any of the common controls are used in your app,
94 * you must call InitCommonControlsEx() to register the classes
95 * for those controls. */
96 iccx
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
97 iccx
.dwICC
= ICC_LISTVIEW_CLASSES
;
98 InitCommonControlsEx(&iccx
);
100 /* Initialize global strings */
101 LoadStringW(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
102 LoadStringW(hInstance
, IDS_APP_TITLE_EX
, szTitleTemplate
, MAX_LOADSTRING
);
103 LoadStringW(hInstance
, IDS_STATUS_MSG
, szStatusBarTemplate
, MAX_LOADSTRING
);
104 MyRegisterClass(hInstance
);
106 /* Perform application initialization: */
107 if (!InitInstance(hInstance
, nCmdShow
))
112 hAccelTable
= LoadAccelerators(hInstance
, MAKEINTRESOURCE(IDC_EVENTVWR
));
114 /* Main message loop: */
115 while (GetMessageW(&msg
, NULL
, 0, 0))
117 if (!TranslateAcceleratorW(msg
.hwnd
, hAccelTable
, &msg
))
119 TranslateMessage(&msg
);
120 DispatchMessage(&msg
);
124 return (int)msg
.wParam
;
127 static void FreeRecords(void)
134 for (iIndex
= 0; iIndex
< g_TotalRecords
; iIndex
++)
135 HeapFree(GetProcessHeap(), 0, g_RecordPtrs
[iIndex
]);
136 HeapFree(GetProcessHeap(), 0, g_RecordPtrs
);
141 EventTimeToSystemTime(DWORD EventTime
,
142 SYSTEMTIME
*pSystemTime
)
144 SYSTEMTIME st1970
= { 1970, 1, 0, 1, 0, 0, 0, 0 };
152 uUCT
.ft
.dwHighDateTime
= 0;
153 uUCT
.ft
.dwLowDateTime
= EventTime
;
154 SystemTimeToFileTime(&st1970
, &u1970
.ft
);
155 uUCT
.ll
= uUCT
.ll
* 10000000 + u1970
.ll
;
156 FileTimeToLocalFileTime(&uUCT
.ft
, &ftLocal
);
157 FileTimeToSystemTime(&ftLocal
, pSystemTime
);
168 c
= s
+ wcslen(s
) - 1;
169 while (c
>= s
&& iswspace(*c
))
177 GetEventMessageFileDLL(IN LPCWSTR lpLogName
,
178 IN LPCWSTR SourceName
,
179 IN LPCWSTR EntryName
,
180 OUT LPWSTR ExpandedName
)
183 BYTE szModuleName
[MAX_PATH
];
184 WCHAR szKeyName
[MAX_PATH
];
186 HKEY hSourceKey
= NULL
;
187 BOOL bReturn
= FALSE
;
189 wcscpy(szKeyName
, L
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\");
190 wcscat(szKeyName
, lpLogName
);
192 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
196 &hAppKey
) == ERROR_SUCCESS
)
198 if (RegOpenKeyExW(hAppKey
,
202 &hSourceKey
) == ERROR_SUCCESS
)
205 if (RegQueryValueExW(hSourceKey
,
209 (LPBYTE
)szModuleName
,
210 &dwSize
) == ERROR_SUCCESS
)
212 /* Returns a string containing the requested substituted environment variable. */
213 ExpandEnvironmentStringsW((LPCWSTR
)szModuleName
, ExpandedName
, MAX_PATH
);
223 L
"Registry access failed!",
225 MB_OK
| MB_ICONINFORMATION
);
228 if (hSourceKey
!= NULL
)
229 RegCloseKey(hSourceKey
);
232 RegCloseKey(hAppKey
);
239 GetEventCategory(IN LPCWSTR KeyName
,
240 IN LPCWSTR SourceName
,
241 IN EVENTLOGRECORD
*pevlr
,
242 OUT LPWSTR CategoryName
)
244 HANDLE hLibrary
= NULL
;
245 WCHAR szMessageDLL
[MAX_PATH
];
246 LPVOID lpMsgBuf
= NULL
;
248 if (GetEventMessageFileDLL (KeyName
, SourceName
, EVENT_CATEGORY_MESSAGE_FILE
, szMessageDLL
))
250 hLibrary
= LoadLibraryExW(szMessageDLL
,
252 DONT_RESOLVE_DLL_REFERENCES
| LOAD_LIBRARY_AS_DATAFILE
);
253 if (hLibrary
!= NULL
)
255 /* Retrieve the message string. */
256 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
258 pevlr
->EventCategory
,
259 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
261 EVENT_MESSAGE_FILE_BUFFER
,
264 /* Trim the string */
267 /* Copy the category name */
268 wcscpy(CategoryName
, lpMsgBuf
);
272 LoadStringW(hInst
, IDS_NONE
, CategoryName
, MAX_PATH
);
275 if (hLibrary
!= NULL
)
276 FreeLibrary(hLibrary
);
278 /* Free the buffer allocated by FormatMessage */
286 LoadStringW(hInst
, IDS_NONE
, CategoryName
, MAX_PATH
);
293 GetEventMessage(IN LPCWSTR KeyName
,
294 IN LPCWSTR SourceName
,
295 IN EVENTLOGRECORD
*pevlr
,
296 OUT LPWSTR EventText
)
299 HANDLE hLibrary
= NULL
;
300 WCHAR SourceModuleName
[1000];
301 WCHAR ParameterModuleName
[1000];
302 LPWSTR lpMsgBuf
= NULL
;
303 WCHAR szStringIDNotFound
[MAX_LOADSTRING
];
309 /* TODO : GetEventMessageFileDLL can return a comma separated list of DLLs */
310 if (GetEventMessageFileDLL (KeyName
, SourceName
, EVENT_MESSAGE_FILE
, SourceModuleName
))
312 /* Get the event message */
313 szMessage
= (LPWSTR
)((LPBYTE
)pevlr
+ pevlr
->StringOffset
);
315 /* Allocate space for parameters */
316 szArguments
= malloc(sizeof(LPVOID
) * pevlr
->NumStrings
);
322 for (i
= 0; i
< pevlr
->NumStrings
; i
++)
324 if (wcsstr(szMessage
, L
"%%"))
326 if (GetEventMessageFileDLL(KeyName
, SourceName
, EVENT_PARAMETER_MESSAGE_FILE
, ParameterModuleName
))
328 /* Not yet support for reading messages from parameter message DLL */
332 szArguments
[i
] = szMessage
;
333 szMessage
+= wcslen(szMessage
) + 1;
336 szDll
= wcstok(SourceModuleName
, EVENT_DLL_SEPARATOR
);
337 while ((szDll
!= NULL
) && (!bDone
))
339 hLibrary
= LoadLibraryExW(szDll
,
341 DONT_RESOLVE_DLL_REFERENCES
| LOAD_LIBRARY_AS_DATAFILE
);
342 if (hLibrary
== NULL
)
344 /* The DLL could not be loaded try the next one (if any) */
345 szDll
= wcstok(NULL
, EVENT_DLL_SEPARATOR
);
349 /* Retrieve the message string. */
350 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
|
351 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
352 FORMAT_MESSAGE_FROM_HMODULE
|
353 FORMAT_MESSAGE_ARGUMENT_ARRAY
,
356 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
359 (va_list*)szArguments
) == 0)
361 /* We haven't found the string , get next DLL (if any) */
362 szDll
= wcstok(NULL
, EVENT_DLL_SEPARATOR
);
368 /* The ID was found and the message was formated */
371 /* Trim the string */
372 TrimNulls((LPWSTR
)lpMsgBuf
);
374 /* Copy the event text */
375 wcscpy(EventText
,lpMsgBuf
);
379 FreeLibrary(hLibrary
);
385 LoadStringW(hInst
, IDS_EVENTSTRINGIDNOTFOUND
, szStringIDNotFound
, MAX_LOADSTRING
);
386 swprintf(EventText
, szStringIDNotFound
, (pevlr
->EventID
& 0xFFFF), SourceName
);
391 /* No more dlls to try, return result */
395 LoadStringW(hInst
, IDS_EVENTSTRINGIDNOTFOUND
, szStringIDNotFound
, MAX_LOADSTRING
);
396 swprintf(EventText
, szStringIDNotFound
, (pevlr
->EventID
& 0xFFFF), SourceName
);
403 GetEventType(IN WORD dwEventType
,
404 OUT LPWSTR eventTypeText
)
408 case EVENTLOG_ERROR_TYPE
:
409 LoadStringW(hInst
, IDS_EVENTLOG_ERROR_TYPE
, eventTypeText
, MAX_LOADSTRING
);
411 case EVENTLOG_WARNING_TYPE
:
412 LoadStringW(hInst
, IDS_EVENTLOG_WARNING_TYPE
, eventTypeText
, MAX_LOADSTRING
);
414 case EVENTLOG_INFORMATION_TYPE
:
415 LoadStringW(hInst
, IDS_EVENTLOG_INFORMATION_TYPE
, eventTypeText
, MAX_LOADSTRING
);
417 case EVENTLOG_AUDIT_SUCCESS
:
418 LoadStringW(hInst
, IDS_EVENTLOG_AUDIT_SUCCESS
, eventTypeText
, MAX_LOADSTRING
);
420 case EVENTLOG_AUDIT_FAILURE
:
421 LoadStringW(hInst
, IDS_EVENTLOG_AUDIT_FAILURE
, eventTypeText
, MAX_LOADSTRING
);
423 case EVENTLOG_SUCCESS
:
424 LoadStringW(hInst
, IDS_EVENTLOG_SUCCESS
, eventTypeText
, MAX_LOADSTRING
);
427 LoadStringW(hInst
, IDS_EVENTLOG_UNKNOWN_TYPE
, eventTypeText
, MAX_LOADSTRING
);
433 GetEventUserName(EVENTLOGRECORD
*pelr
,
438 WCHAR szDomain
[1024];
441 DWORD cbDomain
= 1024;
443 /* Point to the SID. */
444 lpSid
= (PSID
)((LPBYTE
)pelr
+ pelr
->UserSidOffset
);
447 if (pelr
->UserSidLength
> 0)
449 if (LookupAccountSidW(NULL
,
457 wcscpy(pszUser
, szName
);
467 ShowStatusMessageThread(IN LPVOID lpParameter
)
469 HWND
*phWnd
= (HWND
*)lpParameter
;
473 hWnd
= CreateDialogParam(hInst
,
474 MAKEINTRESOURCE(IDD_PROGRESSBOX
),
476 StatusMessageWindowProc
,
483 ShowWindow(hWnd
, SW_SHOW
);
485 /* Message loop for the Status window */
486 while (GetMessage(&Msg
, NULL
, 0, 0))
488 TranslateMessage(&Msg
);
489 DispatchMessage(&Msg
);
497 QueryEventMessages(LPWSTR lpMachineName
,
502 EVENTLOGRECORD
*pevlr
;
503 DWORD dwRead
, dwNeeded
, dwThisRecord
, dwTotalRecords
= 0, dwCurrentRecord
= 0, dwRecordsToRead
= 0, dwFlags
, dwMaxLength
;
505 LPWSTR lpComputerName
;
507 BOOL bResult
= TRUE
; /* Read succeeded. */
510 WCHAR szWindowTitle
[MAX_PATH
];
511 WCHAR szStatusText
[MAX_PATH
];
512 WCHAR szLocalDate
[MAX_PATH
];
513 WCHAR szLocalTime
[MAX_PATH
];
514 WCHAR szEventID
[MAX_PATH
];
515 WCHAR szEventTypeText
[MAX_PATH
];
516 WCHAR szCategoryID
[MAX_PATH
];
517 WCHAR szUsername
[MAX_PATH
];
518 WCHAR szEventText
[EVENT_MESSAGE_FILE_BUFFER
];
519 WCHAR szCategory
[MAX_PATH
];
520 WCHAR szData
[MAX_PATH
];
523 LVITEMW lviEventItem
;
525 dwFlags
= EVENTLOG_FORWARDS_READ
| EVENTLOG_SEQUENTIAL_READ
;
527 lpSourceLogName
= lpLogName
;
528 lpComputerName
= lpMachineName
;
530 /* Open the event log. */
531 hEventLog
= OpenEventLogW(lpMachineName
,
533 if (hEventLog
== NULL
)
536 L
"Could not open the event log.",
538 MB_OK
| MB_ICONINFORMATION
);
542 /* Disable listview redraw */
543 SendMessage(hwndListView
, WM_SETREDRAW
, FALSE
, 0);
545 /* Clear the list view */
546 (void)ListView_DeleteAllItems (hwndListView
);
549 GetOldestEventLogRecord(hEventLog
, &dwThisRecord
);
551 /* Get the total number of event log records. */
552 GetNumberOfEventLogRecords (hEventLog
, &dwTotalRecords
);
553 g_TotalRecords
= dwTotalRecords
;
555 g_RecordPtrs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwTotalRecords
* sizeof(PVOID
));
557 /* If we have at least 1000 records show the waiting dialog */
558 if (dwTotalRecords
> 1000)
560 CloseHandle(CreateThread(NULL
,
562 ShowStatusMessageThread
,
568 while (dwCurrentRecord
< dwTotalRecords
)
570 pevlr
= HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD
) * dwTotalRecords
);
571 g_RecordPtrs
[dwCurrentRecord
] = pevlr
;
573 bResult
= ReadEventLog(hEventLog
, // Event log handle
574 dwFlags
, // Sequential read
575 0, // Ignored for sequential read
576 pevlr
, // Pointer to buffer
577 sizeof(EVENTLOGRECORD
), // Size of buffer
578 &dwRead
, // Number of bytes read
579 &dwNeeded
); // Bytes in the next record
580 if((!bResult
) && (GetLastError () == ERROR_INSUFFICIENT_BUFFER
))
582 HeapFree(GetProcessHeap(), 0, pevlr
);
583 pevlr
= HeapAlloc(GetProcessHeap(), 0, dwNeeded
);
584 g_RecordPtrs
[dwCurrentRecord
] = pevlr
;
586 ReadEventLogW(hEventLog
, // event log handle
587 dwFlags
, // read flags
588 0, // offset; default is 0
589 pevlr
, // pointer to buffer
590 dwNeeded
, // size of buffer
591 &dwRead
, // number of bytes read
592 &dwNeeded
); // bytes in next record
597 LoadStringW(hInst
, IDS_NOT_AVAILABLE
, szUsername
, MAX_PATH
);
598 LoadStringW(hInst
, IDS_NOT_AVAILABLE
, szEventText
, MAX_PATH
);
599 LoadStringW(hInst
, IDS_NONE
, szCategory
, MAX_PATH
);
601 // Get the event source name.
602 lpSourceName
= (LPWSTR
)((LPBYTE
)pevlr
+ sizeof(EVENTLOGRECORD
));
604 // Get the computer name
605 lpComputerName
= (LPWSTR
)((LPBYTE
)pevlr
+ sizeof(EVENTLOGRECORD
) + (wcslen(lpSourceName
) + 1) * sizeof(WCHAR
));
607 // This ist the data section of the current event
608 lpData
= (LPSTR
)((LPBYTE
)pevlr
+ pevlr
->DataOffset
);
610 // Compute the event type
611 EventTimeToSystemTime(pevlr
->TimeWritten
, &time
);
613 // Get the username that generated the event
614 GetEventUserName(pevlr
, szUsername
);
616 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &time
, NULL
, szLocalDate
, MAX_PATH
);
617 GetTimeFormatW(LOCALE_USER_DEFAULT
, TIME_NOSECONDS
, &time
, NULL
, szLocalTime
, MAX_PATH
);
619 GetEventType(pevlr
->EventType
, szEventTypeText
);
620 GetEventCategory(lpLogName
, lpSourceName
, pevlr
, szCategory
);
622 swprintf(szEventID
, L
"%u", (pevlr
->EventID
& 0xFFFF));
623 swprintf(szCategoryID
, L
"%u", pevlr
->EventCategory
);
625 lviEventItem
.mask
= LVIF_IMAGE
| LVIF_TEXT
| LVIF_PARAM
;
626 lviEventItem
.iItem
= 0;
627 lviEventItem
.iSubItem
= 0;
628 lviEventItem
.lParam
= (LPARAM
)pevlr
;
629 lviEventItem
.pszText
= szEventTypeText
;
631 switch (pevlr
->EventType
)
633 case EVENTLOG_ERROR_TYPE
:
634 lviEventItem
.iImage
= 2;
637 case EVENTLOG_AUDIT_FAILURE
:
638 lviEventItem
.iImage
= 2;
641 case EVENTLOG_WARNING_TYPE
:
642 lviEventItem
.iImage
= 1;
645 case EVENTLOG_INFORMATION_TYPE
:
646 lviEventItem
.iImage
= 0;
649 case EVENTLOG_AUDIT_SUCCESS
:
650 lviEventItem
.iImage
= 0;
653 case EVENTLOG_SUCCESS
:
654 lviEventItem
.iImage
= 0;
658 lviEventItem
.iItem
= ListView_InsertItem(hwndListView
, &lviEventItem
);
660 ListView_SetItemText(hwndListView
, lviEventItem
.iItem
, 1, szLocalDate
);
661 ListView_SetItemText(hwndListView
, lviEventItem
.iItem
, 2, szLocalTime
);
662 ListView_SetItemText(hwndListView
, lviEventItem
.iItem
, 3, lpSourceName
);
663 ListView_SetItemText(hwndListView
, lviEventItem
.iItem
, 4, szCategory
);
664 ListView_SetItemText(hwndListView
, lviEventItem
.iItem
, 5, szEventID
);
665 ListView_SetItemText(hwndListView
, lviEventItem
.iItem
, 6, szUsername
); //User
666 ListView_SetItemText(hwndListView
, lviEventItem
.iItem
, 7, lpComputerName
); //Computer
667 MultiByteToWideChar(CP_ACP
,
673 ListView_SetItemText(hwndListView
, lviEventItem
.iItem
, 8, szData
); //Event Text
675 dwRead
-= pevlr
->Length
;
676 pevlr
= (EVENTLOGRECORD
*)((LPBYTE
) pevlr
+ pevlr
->Length
);
684 EndDialog(hwndDlg
, 0);
687 i
= swprintf(szWindowTitle
, szTitleTemplate
, szTitle
, lpLogName
); /* i = number of characters written */
688 /* lpComputerName can be NULL here if no records was read */
689 dwMaxLength
= sizeof(szWindowTitle
) / sizeof(WCHAR
) - i
;
691 GetComputerNameW(szWindowTitle
+i
, &dwMaxLength
);
693 _snwprintf(szWindowTitle
+i
, dwMaxLength
, L
"%s", lpComputerName
);
695 swprintf(szStatusText
, szStatusBarTemplate
, lpLogName
, dwTotalRecords
);
697 // Update the status bar
698 SendMessageW(hwndStatus
, SB_SETTEXT
, (WPARAM
)0, (LPARAM
)szStatusText
);
700 // Set the window title
701 SetWindowTextW(hwndMainWindow
, szWindowTitle
);
703 // Resume list view redraw
704 SendMessageW(hwndListView
, WM_SETREDRAW
, TRUE
, 0);
706 // Close the event log.
707 CloseEventLog(hEventLog
);
716 QueryEventMessages(lpComputerName
,
722 // FUNCTION: MyRegisterClass()
724 // PURPOSE: Registers the window class.
728 // This function and its usage are only necessary if you want this code
729 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
730 // function that was added to Windows 95. It is important to call this function
731 // so that the application will get 'well formed' small icons associated
735 MyRegisterClass(HINSTANCE hInstance
)
739 wcex
.cbSize
= sizeof(WNDCLASSEX
);
742 wcex
.lpfnWndProc
= WndProc
;
745 wcex
.hInstance
= hInstance
;
746 wcex
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_EVENTVWR
));
747 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
748 wcex
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
749 wcex
.lpszMenuName
= MAKEINTRESOURCE(IDC_EVENTVWR
);
750 wcex
.lpszClassName
= szWindowClass
;
751 wcex
.hIconSm
= LoadIcon(wcex
.hInstance
, MAKEINTRESOURCE(IDI_SMALL
));
753 return RegisterClassExW(&wcex
);
758 // FUNCTION: InitInstance(HINSTANCE, int)
760 // PURPOSE: Saves instance handle and creates main window
764 // In this function, we save the instance handle in a global variable and
765 // create and display the main program window.
768 InitInstance(HINSTANCE hInstance
,
775 hInst
= hInstance
; // Store instance handle in our global variable
777 hwndMainWindow
= CreateWindowW(szWindowClass
,
779 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
,
780 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0,
790 hwndStatus
= CreateWindowExW(0, // no extended styles
791 STATUSCLASSNAMEW
, // status bar
793 WS_CHILD
| WS_BORDER
| WS_VISIBLE
, // styles
794 0, 0, 0, 0, // x, y, cx, cy
795 hwndMainWindow
, // parent window
796 (HMENU
)100, // window ID
797 hInstance
, // instance
798 NULL
); // window data
800 // Create our listview child window. Note that I use WS_EX_CLIENTEDGE
801 // and WS_BORDER to create the normal "sunken" look. Also note that
802 // LVS_EX_ styles cannot be set in CreateWindowEx().
803 hwndListView
= CreateWindowExW(WS_EX_CLIENTEDGE
,
806 LVS_SHOWSELALWAYS
| WS_CHILD
| WS_VISIBLE
| LVS_REPORT
,
816 // After the ListView is created, we can add extended list view styles.
817 (void)ListView_SetExtendedListViewStyle (hwndListView
, LVS_EX_FULLROWSELECT
);
819 // Create the ImageList
820 hSmall
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
),
821 GetSystemMetrics(SM_CYSMICON
),
826 // Add event type icons to ImageList
827 ImageList_AddIcon (hSmall
, LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_INFORMATIONICON
)));
828 ImageList_AddIcon (hSmall
, LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_WARNINGICON
)));
829 ImageList_AddIcon (hSmall
, LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_ERRORICON
)));
831 // Assign ImageList to List View
832 (void)ListView_SetImageList (hwndListView
, hSmall
, LVSIL_SMALL
);
834 // Now set up the listview with its columns.
835 lvc
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
837 LoadStringW(hInstance
,
840 sizeof(szTemp
) / sizeof(WCHAR
));
841 lvc
.pszText
= szTemp
;
842 (void)ListView_InsertColumn(hwndListView
, 0, &lvc
);
845 LoadStringW(hInstance
,
848 sizeof(szTemp
) / sizeof(WCHAR
));
849 lvc
.pszText
= szTemp
;
850 (void)ListView_InsertColumn(hwndListView
, 1, &lvc
);
853 LoadStringW(hInstance
,
856 sizeof(szTemp
) / sizeof(WCHAR
));
857 lvc
.pszText
= szTemp
;
858 (void)ListView_InsertColumn(hwndListView
, 2, &lvc
);
861 LoadStringW(hInstance
,
864 sizeof(szTemp
) / sizeof(WCHAR
));
865 lvc
.pszText
= szTemp
;
866 (void)ListView_InsertColumn(hwndListView
, 3, &lvc
);
869 LoadStringW(hInstance
,
872 sizeof(szTemp
) / sizeof(WCHAR
));
873 lvc
.pszText
= szTemp
;
874 (void)ListView_InsertColumn(hwndListView
, 4, &lvc
);
877 LoadStringW(hInstance
,
880 sizeof(szTemp
) / sizeof(WCHAR
));
881 lvc
.pszText
= szTemp
;
882 (void)ListView_InsertColumn(hwndListView
, 5, &lvc
);
885 LoadStringW(hInstance
,
888 sizeof(szTemp
) / sizeof(WCHAR
));
889 lvc
.pszText
= szTemp
;
890 (void)ListView_InsertColumn(hwndListView
, 6, &lvc
);
893 LoadStringW(hInstance
,
896 sizeof(szTemp
) / sizeof(WCHAR
));
897 lvc
.pszText
= szTemp
;
898 (void)ListView_InsertColumn(hwndListView
, 7, &lvc
);
901 LoadStringW(hInstance
,
904 sizeof(szTemp
) / sizeof(WCHAR
));
905 lvc
.pszText
= szTemp
;
906 (void)ListView_InsertColumn(hwndListView
, 8, &lvc
);
908 ShowWindow(hwndMainWindow
, nCmdShow
);
909 UpdateWindow(hwndMainWindow
);
911 QueryEventMessages(lpComputerName
, // Use the local computer.
912 EVENT_SOURCE_APPLICATION
); // The event log category
919 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
921 // PURPOSE: Processes messages for the main window.
923 // WM_COMMAND - process the application menu
924 // WM_PAINT - Paint the main window
925 // WM_DESTROY - post a quit message and return
929 WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
937 CheckMenuRadioItem(GetMenu(hWnd
),
945 switch (((LPNMHDR
)lParam
)->code
)
948 hdr
= (NMHDR FAR
*)lParam
;
949 if (hdr
->hwndFrom
== hwndListView
)
951 LPNMITEMACTIVATE lpnmitem
= (LPNMITEMACTIVATE
)lParam
;
953 if (lpnmitem
->iItem
!= -1)
956 MAKEINTRESOURCE(IDD_EVENTDETAILDIALOG
),
966 // Parse the menu selections:
967 switch (LOWORD(wParam
))
969 case ID_LOG_APPLICATION
:
970 if (QueryEventMessages(lpComputerName
, // Use the local computer.
971 EVENT_SOURCE_APPLICATION
)) // The event log category
973 CheckMenuRadioItem(GetMenu(hWnd
),
981 case ID_LOG_SECURITY
:
982 if (QueryEventMessages(lpComputerName
, // Use the local computer.
983 EVENT_SOURCE_SECURITY
)) // The event log category
985 CheckMenuRadioItem(GetMenu(hWnd
),
994 if (QueryEventMessages(lpComputerName
, // Use the local computer.
995 EVENT_SOURCE_SYSTEM
)) // The event log category
997 CheckMenuRadioItem(GetMenu(hWnd
),
1010 DialogBox(hInst
, MAKEINTRESOURCE(IDD_ABOUTBOX
), hWnd
, About
);
1015 L
"Help not implemented yet!",
1017 MB_OK
| MB_ICONINFORMATION
);
1021 DestroyWindow(hWnd
);
1025 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
1031 // Gets the window rectangle
1032 GetClientRect(hWnd
, &rect
);
1034 // Relocate the listview
1035 MoveWindow(hwndListView
,
1042 // Resize the statusbar;
1043 SendMessage(hwndStatus
, message
, wParam
, lParam
);
1052 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
1059 // Message handler for about box.
1061 About(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1063 UNREFERENCED_PARAMETER(lParam
);
1068 return (INT_PTR
)TRUE
;
1072 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
1074 EndDialog(hDlg
, LOWORD(wParam
));
1075 return (INT_PTR
)TRUE
;
1080 return (INT_PTR
)FALSE
;
1084 DisplayEvent(HWND hDlg
)
1086 WCHAR szEventType
[MAX_PATH
];
1087 WCHAR szTime
[MAX_PATH
];
1088 WCHAR szDate
[MAX_PATH
];
1089 WCHAR szUser
[MAX_PATH
];
1090 WCHAR szComputer
[MAX_PATH
];
1091 WCHAR szSource
[MAX_PATH
];
1092 WCHAR szCategory
[MAX_PATH
];
1093 WCHAR szEventID
[MAX_PATH
];
1094 WCHAR szEventText
[EVENT_MESSAGE_EVENTTEXT_BUFFER
];
1095 WCHAR szEventData
[MAX_PATH
];
1096 BOOL bEventData
= FALSE
;
1098 EVENTLOGRECORD
* pevlr
;
1101 // Get index of selected item
1102 iIndex
= (int)SendMessage (hwndListView
, LVM_GETNEXTITEM
, -1, LVNI_SELECTED
| LVNI_FOCUSED
);
1104 li
.mask
= LVIF_PARAM
;
1108 (void)ListView_GetItem(hwndListView
, &li
);
1110 pevlr
= (EVENTLOGRECORD
*)li
.lParam
;
1114 ListView_GetItemText(hwndListView
, iIndex
, 0, szEventType
, sizeof(szEventType
) / sizeof(WCHAR
));
1115 ListView_GetItemText(hwndListView
, iIndex
, 1, szDate
, sizeof(szDate
) / sizeof(WCHAR
));
1116 ListView_GetItemText(hwndListView
, iIndex
, 2, szTime
, sizeof(szTime
) / sizeof(WCHAR
));
1117 ListView_GetItemText(hwndListView
, iIndex
, 3, szSource
, sizeof(szSource
) / sizeof(WCHAR
));
1118 ListView_GetItemText(hwndListView
, iIndex
, 4, szCategory
, sizeof(szCategory
) / sizeof(WCHAR
));
1119 ListView_GetItemText(hwndListView
, iIndex
, 5, szEventID
, sizeof(szEventID
) / sizeof(WCHAR
));
1120 ListView_GetItemText(hwndListView
, iIndex
, 6, szUser
, sizeof(szUser
) / sizeof(WCHAR
));
1121 ListView_GetItemText(hwndListView
, iIndex
, 7, szComputer
, sizeof(szComputer
) / sizeof(WCHAR
));
1123 bEventData
= !(pevlr
->DataLength
== 0);
1125 if (pevlr
->DataLength
> 0)
1127 MultiByteToWideChar(CP_ACP
,
1129 (LPCSTR
)((LPBYTE
)pevlr
+ pevlr
->DataOffset
),
1135 GetEventMessage(lpSourceLogName
, szSource
, pevlr
, szEventText
);
1137 EnableWindow(GetDlgItem(hDlg
, IDC_BYTESRADIO
), bEventData
);
1138 EnableWindow(GetDlgItem(hDlg
, IDC_WORDRADIO
), bEventData
);
1140 SetDlgItemTextW(hDlg
, IDC_EVENTDATESTATIC
, szDate
);
1141 SetDlgItemTextW(hDlg
, IDC_EVENTTIMESTATIC
, szTime
);
1143 SetDlgItemTextW(hDlg
, IDC_EVENTUSERSTATIC
, szUser
);
1144 SetDlgItemTextW(hDlg
, IDC_EVENTSOURCESTATIC
, szSource
);
1145 SetDlgItemTextW(hDlg
, IDC_EVENTCOMPUTERSTATIC
, szComputer
);
1146 SetDlgItemTextW(hDlg
, IDC_EVENTCATEGORYSTATIC
, szCategory
);
1147 SetDlgItemTextW(hDlg
, IDC_EVENTIDSTATIC
, szEventID
);
1148 SetDlgItemTextW(hDlg
, IDC_EVENTTYPESTATIC
, szEventType
);
1149 SetDlgItemTextW(hDlg
, IDC_EVENTTEXTEDIT
, szEventText
);
1150 SetDlgItemTextW(hDlg
, IDC_EVENTDATAEDIT
, szEventData
);
1155 L
"No Items in ListView",
1157 MB_OK
| MB_ICONINFORMATION
);
1164 StatusMessageWindowProc(IN HWND hwndDlg
,
1169 UNREFERENCED_PARAMETER(wParam
);
1182 // Message handler for event details box.
1184 EventDetails(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1186 UNREFERENCED_PARAMETER(lParam
);
1191 // Show event info on dialog box
1193 return (INT_PTR
)TRUE
;
1196 switch (LOWORD(wParam
))
1200 EndDialog(hDlg
, LOWORD(wParam
));
1201 return (INT_PTR
)TRUE
;
1204 SendMessage(hwndListView
, WM_KEYDOWN
, VK_UP
, 0);
1206 // Show event info on dialog box
1208 return (INT_PTR
)TRUE
;
1211 SendMessage(hwndListView
, WM_KEYDOWN
, VK_DOWN
, 0);
1213 // Show event info on dialog box
1215 return (INT_PTR
)TRUE
;
1217 case IDC_BYTESRADIO
:
1218 return (INT_PTR
)TRUE
;
1221 return (INT_PTR
)TRUE
;
1225 L
"Help not implemented yet!",
1227 MB_OK
| MB_ICONINFORMATION
);
1228 return (INT_PTR
)TRUE
;
1236 return (INT_PTR
)FALSE
;