]> granicus.if.org Git - postgresql/blob - src/bin/pgevent/pgevent.c
1fcde866d6cb3a204a3d6c02c877bbc3f385495d
[postgresql] / src / bin / pgevent / pgevent.c
1 /*-------------------------------------------------------------------------
2  *
3  * pgevent.c
4  *              Defines the entry point for pgevent dll.
5  *              The DLL defines event source for backend
6  *
7  *
8  * IDENTIFICATION
9  *        src/bin/pgevent/pgevent.c
10  *
11  *-------------------------------------------------------------------------
12  */
13
14
15 #include <windows.h>
16 #include <olectl.h>
17 #include <string.h>
18
19 /* Global variables */
20 HANDLE          g_module = NULL;        /* hModule of DLL */
21
22 /* Prototypes */
23 STDAPI
24 DllRegisterServer(void);
25 STDAPI          DllUnregisterServer(void);
26 BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved);
27
28 /*
29  * DllRegisterServer --- Instructs DLL to create its registry entries
30  */
31
32 STDAPI
33 DllRegisterServer(void)
34 {
35         HKEY            key;
36         DWORD           data;
37         char            buffer[_MAX_PATH];
38
39         /* Set the name of DLL full path name. */
40         if (!GetModuleFileName((HMODULE) g_module, buffer, sizeof(buffer)))
41         {
42                 MessageBox(NULL, "Could not retrieve DLL filename", "PostgreSQL error", MB_OK | MB_ICONSTOP);
43                 return SELFREG_E_TYPELIB;
44         }
45
46         /*
47          * Add PostgreSQL source name as a subkey under the Application key in the
48          * EventLog registry key.
49          */
50         if (RegCreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\PostgreSQL", &key))
51         {
52                 MessageBox(NULL, "Could not create the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
53                 return SELFREG_E_TYPELIB;
54         }
55
56         /* Add the name to the EventMessageFile subkey. */
57         if (RegSetValueEx(key,
58                                           "EventMessageFile",
59                                           0,
60                                           REG_EXPAND_SZ,
61                                           (LPBYTE) buffer,
62                                           strlen(buffer) + 1))
63         {
64                 MessageBox(NULL, "Could not set the event message file.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
65                 return SELFREG_E_TYPELIB;
66         }
67
68         /* Set the supported event types in the TypesSupported subkey. */
69         data = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
70
71         if (RegSetValueEx(key,
72                                           "TypesSupported",
73                                           0,
74                                           REG_DWORD,
75                                           (LPBYTE) &data,
76                                           sizeof(DWORD)))
77         {
78                 MessageBox(NULL, "Could not set the supported types.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
79                 return SELFREG_E_TYPELIB;
80         }
81
82         RegCloseKey(key);
83         return S_OK;
84 }
85
86 /*
87  * DllUnregisterServer --- Instructs DLL to remove only those entries created through DllRegisterServer
88  */
89
90 STDAPI
91 DllUnregisterServer(void)
92 {
93         /*
94          * Remove PostgreSQL source name as a subkey under the Application key in
95          * the EventLog registry key.
96          */
97
98         if (RegDeleteKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\PostgreSQL"))
99         {
100                 MessageBox(NULL, "Could not delete the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
101                 return SELFREG_E_TYPELIB;
102         }
103         return S_OK;
104 }
105
106 /*
107  * DllMain --- is an optional entry point into a DLL.
108  */
109
110 BOOL            WINAPI
111 DllMain(HANDLE hModule,
112                 DWORD ul_reason_for_call,
113                 LPVOID lpReserved
114 )
115 {
116         if (ul_reason_for_call == DLL_PROCESS_ATTACH)
117                 g_module = hModule;
118         return TRUE;
119 }