1 /*-------------------------------------------------------------------------
4 * Defines the entry point for pgevent dll.
5 * The DLL defines event source for backend
9 * src/bin/pgevent/pgevent.c
11 *-------------------------------------------------------------------------
21 /* Global variables */
22 HANDLE g_module = NULL; /* hModule of DLL */
25 * The event source is stored as a registry key.
26 * The maximum length of a registry key is 255 characters.
27 * http://msdn.microsoft.com/en-us/library/ms724872(v=vs.85).aspx
29 char event_source[256] = "PostgreSQL";
32 HRESULT DllInstall(BOOL bInstall, __in_opt LPCWSTR pszCmdLine);
33 STDAPI DllRegisterServer(void);
34 STDAPI DllUnregisterServer(void);
35 BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved);
38 * DllInstall --- Passes the command line argument to DLL
42 DllInstall(BOOL bInstall,
43 __in_opt LPCWSTR pszCmdLine)
47 if (pszCmdLine && *pszCmdLine != '\0')
48 wcstombs_s(&ret, event_source, sizeof(event_source),
49 pszCmdLine, sizeof(event_source));
52 * This is an ugly hack due to the strange behavior of "regsvr32 /i".
54 * When installing, regsvr32 calls DllRegisterServer before DllInstall.
55 * When uninstalling (i.e. "regsvr32 /u /i"), on the other hand, regsvr32
56 * calls DllInstall and then DllUnregisterServer as expected.
58 * This strange behavior forces us to specify -n (i.e. "regsvr32 /n /i").
59 * Without -n, DllRegisterServer called before DllInstall would mistakenly
60 * overwrite the default "PostgreSQL" event source registration.
68 * DllRegisterServer --- Instructs DLL to create its registry entries
72 DllRegisterServer(void)
76 char buffer[_MAX_PATH];
79 /* Set the name of DLL full path name. */
80 if (!GetModuleFileName((HMODULE) g_module, buffer, sizeof(buffer)))
82 MessageBox(NULL, "Could not retrieve DLL filename", "PostgreSQL error", MB_OK | MB_ICONSTOP);
83 return SELFREG_E_TYPELIB;
87 * Add PostgreSQL source name as a subkey under the Application key in the
88 * EventLog registry key.
90 _snprintf(key_name, sizeof(key_name),
91 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
93 if (RegCreateKey(HKEY_LOCAL_MACHINE, key_name, &key))
95 MessageBox(NULL, "Could not create the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
96 return SELFREG_E_TYPELIB;
99 /* Add the name to the EventMessageFile subkey. */
100 if (RegSetValueEx(key,
107 MessageBox(NULL, "Could not set the event message file.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
108 return SELFREG_E_TYPELIB;
111 /* Set the supported event types in the TypesSupported subkey. */
112 data = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
114 if (RegSetValueEx(key,
121 MessageBox(NULL, "Could not set the supported types.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
122 return SELFREG_E_TYPELIB;
130 * DllUnregisterServer --- Instructs DLL to remove only those entries created through DllRegisterServer
134 DllUnregisterServer(void)
139 * Remove PostgreSQL source name as a subkey under the Application key in
140 * the EventLog registry key.
143 _snprintf(key_name, sizeof(key_name),
144 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
146 if (RegDeleteKey(HKEY_LOCAL_MACHINE, key_name))
148 MessageBox(NULL, "Could not delete the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
149 return SELFREG_E_TYPELIB;
155 * DllMain --- is an optional entry point into a DLL.
159 DllMain(HANDLE hModule,
160 DWORD ul_reason_for_call,
164 if (ul_reason_for_call == DLL_PROCESS_ATTACH)