forked from hiyohiyo/CrystalDiskInfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventLog.cpp
108 lines (93 loc) · 2.45 KB
/
EventLog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*---------------------------------------------------------------------------*/
// Author : hiyohiyo
// Mail : [email protected]
// Web : https://crystalmark.info/
// License : The MIT License
/*---------------------------------------------------------------------------*/
#include "stdafx.h"
#include "EventLog.h"
#define REGISTRY_KEY _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\CrystalDiskInfo")
BOOL InstallEventSource()
{
HKEY hk = NULL;
DWORD level;
DWORD length = MAX_PATH;
TCHAR fileName[MAX_PATH];
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_KEY, 0, KEY_ALL_ACCESS, &hk) != ERROR_SUCCESS)
{
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_KEY, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hk, NULL) != ERROR_SUCCESS)
{
return FALSE;
}
}
if(RegQueryValueEx(hk, _T("EventMessageFile"), NULL, NULL,
(LPBYTE)fileName, &length) != ERROR_SUCCESS)
{
::GetModuleFileName(NULL, fileName, MAX_PATH);
if(RegSetValueEx(hk, _T("EventMessageFile"), 0,REG_EXPAND_SZ,
(LPBYTE)fileName, (DWORD)(_tcslen(fileName) + 1) * 2) != ERROR_SUCCESS)
{
RegCloseKey(hk);
return FALSE;
}
}
if(RegQueryValueEx(hk, _T("TypesSupported"), NULL, NULL, (LPBYTE)&level,
(LPDWORD)&length) != ERROR_SUCCESS)
{
level = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
length = sizeof(DWORD);
if(RegSetValueEx(hk, _T("TypesSupported"), 0, REG_DWORD, (LPBYTE)&level, length) != ERROR_SUCCESS)
{
RegCloseKey(hk);
return FALSE;
}
}
if(RegCloseKey(hk) == ERROR_SUCCESS)
{
return TRUE;
}
else
{
return FALSE;
}
}
BOOL WriteEventLog(DWORD eventId, WORD eventType, PTCHAR source, CString message)
{
HANDLE hEventLog = NULL;
DWORD flag = 0;
BOOL result = FALSE;
hEventLog = RegisterEventSource(NULL, source);
if(hEventLog == NULL)
{
return FALSE;
}
LPCTSTR str[1];
str[0] = message.GetString();
if(600 <= eventId && eventId < 700)
{
flag = 0x80000000; // Warning
}
else
{
flag = 0x40000000; // Informational
}
result = ReportEventW(hEventLog, eventType, 0, eventId + flag, NULL,
1, 0, (LPCWSTR *)str, NULL);
if(! result)
{
return FALSE;
}
return DeregisterEventSource(hEventLog);
}
BOOL UninstallEventSource()
{
if(SHDeleteKey(HKEY_LOCAL_MACHINE, REGISTRY_KEY) == ERROR_SUCCESS)
{
return TRUE;
}
else
{
return FALSE;
}
}