...
1
2
3
4
5
6
7 package eventlog
8
9 import (
10 "errors"
11
12 "golang.org/x/sys/windows"
13 "golang.org/x/sys/windows/registry"
14 )
15
16 const (
17
18 Info = windows.EVENTLOG_INFORMATION_TYPE
19 Warning = windows.EVENTLOG_WARNING_TYPE
20 Error = windows.EVENTLOG_ERROR_TYPE
21 )
22
23 const addKeyName = `SYSTEM\CurrentControlSet\Services\EventLog\Application`
24
25
26
27
28
29
30
31 func Install(src, msgFile string, useExpandKey bool, eventsSupported uint32) error {
32 appkey, err := registry.OpenKey(registry.LOCAL_MACHINE, addKeyName, registry.CREATE_SUB_KEY)
33 if err != nil {
34 return err
35 }
36 defer appkey.Close()
37
38 sk, alreadyExist, err := registry.CreateKey(appkey, src, registry.SET_VALUE)
39 if err != nil {
40 return err
41 }
42 defer sk.Close()
43 if alreadyExist {
44 return errors.New(addKeyName + `\` + src + " registry key already exists")
45 }
46
47 err = sk.SetDWordValue("CustomSource", 1)
48 if err != nil {
49 return err
50 }
51 if useExpandKey {
52 err = sk.SetExpandStringValue("EventMessageFile", msgFile)
53 } else {
54 err = sk.SetStringValue("EventMessageFile", msgFile)
55 }
56 if err != nil {
57 return err
58 }
59 err = sk.SetDWordValue("TypesSupported", eventsSupported)
60 if err != nil {
61 return err
62 }
63 return nil
64 }
65
66
67
68 func InstallAsEventCreate(src string, eventsSupported uint32) error {
69 return Install(src, "%SystemRoot%\\System32\\EventCreate.exe", true, eventsSupported)
70 }
71
72
73 func Remove(src string) error {
74 appkey, err := registry.OpenKey(registry.LOCAL_MACHINE, addKeyName, registry.SET_VALUE)
75 if err != nil {
76 return err
77 }
78 defer appkey.Close()
79 return registry.DeleteKey(appkey, src)
80 }
81
View as plain text