Consistently use "superuser" instead of "super user"
[pgsql.git] / src / bin / pgevent / pgevent.c
blob807a2c93120d61f9c479ad4fda9f9a557b3c31e3
1 /*-------------------------------------------------------------------------
3 * pgevent.c
4 * Defines the entry point for pgevent dll.
5 * The DLL defines event source for backend
8 * IDENTIFICATION
9 * src/bin/pgevent/pgevent.c
11 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
15 #include <olectl.h>
17 /* Global variables */
18 HANDLE g_module = NULL; /* hModule of DLL */
21 * The event source is stored as a registry key.
22 * The maximum length of a registry key is 255 characters.
23 * http://msdn.microsoft.com/en-us/library/ms724872(v=vs.85).aspx
25 char event_source[256] = DEFAULT_EVENT_SOURCE;
27 /* Prototypes */
28 HRESULT DllInstall(BOOL bInstall, LPCWSTR pszCmdLine);
29 STDAPI DllRegisterServer(void);
30 STDAPI DllUnregisterServer(void);
31 BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved);
34 * DllInstall --- Passes the command line argument to DLL
37 HRESULT
38 DllInstall(BOOL bInstall,
39 LPCWSTR pszCmdLine)
41 if (pszCmdLine && *pszCmdLine != '\0')
42 wcstombs(event_source, pszCmdLine, sizeof(event_source));
45 * This is an ugly hack due to the strange behavior of "regsvr32 /i".
47 * When installing, regsvr32 calls DllRegisterServer before DllInstall.
48 * When uninstalling (i.e. "regsvr32 /u /i"), on the other hand, regsvr32
49 * calls DllInstall and then DllUnregisterServer as expected.
51 * This strange behavior forces us to specify -n (i.e. "regsvr32 /n /i").
52 * Without -n, DllRegisterServer called before DllInstall would mistakenly
53 * overwrite the default "PostgreSQL" event source registration.
55 if (bInstall)
56 DllRegisterServer();
57 return S_OK;
61 * DllRegisterServer --- Instructs DLL to create its registry entries
64 STDAPI
65 DllRegisterServer(void)
67 HKEY key;
68 DWORD data;
69 char buffer[_MAX_PATH];
70 char key_name[400];
72 /* Set the name of DLL full path name. */
73 if (!GetModuleFileName((HMODULE) g_module, buffer, sizeof(buffer)))
75 MessageBox(NULL, "Could not retrieve DLL filename", "PostgreSQL error", MB_OK | MB_ICONSTOP);
76 return SELFREG_E_TYPELIB;
80 * Add PostgreSQL source name as a subkey under the Application key in the
81 * EventLog registry key.
83 _snprintf(key_name, sizeof(key_name),
84 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
85 event_source);
86 if (RegCreateKey(HKEY_LOCAL_MACHINE, key_name, &key))
88 MessageBox(NULL, "Could not create the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
89 return SELFREG_E_TYPELIB;
92 /* Add the name to the EventMessageFile subkey. */
93 if (RegSetValueEx(key,
94 "EventMessageFile",
96 REG_EXPAND_SZ,
97 (LPBYTE) buffer,
98 strlen(buffer) + 1))
100 MessageBox(NULL, "Could not set the event message file.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
101 return SELFREG_E_TYPELIB;
104 /* Set the supported event types in the TypesSupported subkey. */
105 data = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
107 if (RegSetValueEx(key,
108 "TypesSupported",
110 REG_DWORD,
111 (LPBYTE) &data,
112 sizeof(DWORD)))
114 MessageBox(NULL, "Could not set the supported types.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
115 return SELFREG_E_TYPELIB;
118 RegCloseKey(key);
119 return S_OK;
123 * DllUnregisterServer --- Instructs DLL to remove only those entries created through DllRegisterServer
126 STDAPI
127 DllUnregisterServer(void)
129 char key_name[400];
132 * Remove PostgreSQL source name as a subkey under the Application key in
133 * the EventLog registry key.
136 _snprintf(key_name, sizeof(key_name),
137 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
138 event_source);
139 if (RegDeleteKey(HKEY_LOCAL_MACHINE, key_name))
141 MessageBox(NULL, "Could not delete the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
142 return SELFREG_E_TYPELIB;
144 return S_OK;
148 * DllMain --- is an optional entry point into a DLL.
151 BOOL WINAPI
152 DllMain(HANDLE hModule,
153 DWORD ul_reason_for_call,
154 LPVOID lpReserved
157 if (ul_reason_for_call == DLL_PROCESS_ATTACH)
158 g_module = hModule;
159 return TRUE;