1 /*-------------------------------------------------------------------------
4 * Defines the entry point for pgevent dll.
5 * The DLL defines event source for backend
9 * src/bin/pgevent/pgevent.c
11 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
17 /* Global variables */
18 static 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 static char event_source
[256] = DEFAULT_EVENT_SOURCE
;
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
38 DllInstall(BOOL bInstall
,
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.
61 * DllRegisterServer --- Instructs DLL to create its registry entries
65 DllRegisterServer(void)
69 char buffer
[_MAX_PATH
];
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",
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
,
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
,
114 MessageBox(NULL
, "Could not set the supported types.", "PostgreSQL error", MB_OK
| MB_ICONSTOP
);
115 return SELFREG_E_TYPELIB
;
123 * DllUnregisterServer --- Instructs DLL to remove only those entries created through DllRegisterServer
127 DllUnregisterServer(void)
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",
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
;
148 * DllMain --- is an optional entry point into a DLL.
152 DllMain(HANDLE hModule
,
153 DWORD ul_reason_for_call
,
157 if (ul_reason_for_call
== DLL_PROCESS_ATTACH
)