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, 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,
45 if (pszCmdLine && *pszCmdLine != '\0')
46 wcstombs(event_source, pszCmdLine, sizeof(event_source));
49 * This is an ugly hack due to the strange behavior of "regsvr32 /i".
51 * When installing, regsvr32 calls DllRegisterServer before DllInstall.
52 * When uninstalling (i.e. "regsvr32 /u /i"), on the other hand, regsvr32
53 * calls DllInstall and then DllUnregisterServer as expected.
55 * This strange behavior forces us to specify -n (i.e. "regsvr32 /n /i").
56 * Without -n, DllRegisterServer called before DllInstall would mistakenly
57 * overwrite the default "PostgreSQL" event source registration.
65 * DllRegisterServer --- Instructs DLL to create its registry entries
69 DllRegisterServer(void)
73 char buffer[_MAX_PATH];
76 /* Set the name of DLL full path name. */
77 if (!GetModuleFileName((HMODULE) g_module, buffer, sizeof(buffer)))
79 MessageBox(NULL, "Could not retrieve DLL filename", "PostgreSQL error", MB_OK | MB_ICONSTOP);
80 return SELFREG_E_TYPELIB;
84 * Add PostgreSQL source name as a subkey under the Application key in the
85 * EventLog registry key.
87 _snprintf(key_name, sizeof(key_name),
88 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
90 if (RegCreateKey(HKEY_LOCAL_MACHINE, key_name, &key))
92 MessageBox(NULL, "Could not create the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
93 return SELFREG_E_TYPELIB;
96 /* Add the name to the EventMessageFile subkey. */
97 if (RegSetValueEx(key,
104 MessageBox(NULL, "Could not set the event message file.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
105 return SELFREG_E_TYPELIB;
108 /* Set the supported event types in the TypesSupported subkey. */
109 data = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
111 if (RegSetValueEx(key,
118 MessageBox(NULL, "Could not set the supported types.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
119 return SELFREG_E_TYPELIB;
127 * DllUnregisterServer --- Instructs DLL to remove only those entries created through DllRegisterServer
131 DllUnregisterServer(void)
136 * Remove PostgreSQL source name as a subkey under the Application key in
137 * the EventLog registry key.
140 _snprintf(key_name, sizeof(key_name),
141 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
143 if (RegDeleteKey(HKEY_LOCAL_MACHINE, key_name))
145 MessageBox(NULL, "Could not delete the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP);
146 return SELFREG_E_TYPELIB;
152 * DllMain --- is an optional entry point into a DLL.
156 DllMain(HANDLE hModule,
157 DWORD ul_reason_for_call,
161 if (ul_reason_for_call == DLL_PROCESS_ATTACH)